0

Given the following code for my connection factory:

public interface IDbFrontEndConnectionFactory : IDbConnectionFactory
{

}

public class FrontEndDbFactory : IDbFrontEndConnectionFactory
{
    private readonly IAppSettings _settings;

    private readonly IDbConnectionFactory _dbFactory;

    public Func<IDbConnection, IDbConnection> ConnectionFilter { get; set; }

    public FrontEndDbFactory(IDbConnectionFactory dbFactory, IAppSettings settings)
    {
        _dbFactory = dbFactory;
        _settings = settings;
        ConnectionFilter = (Func<IDbConnection, IDbConnection>)(x => x);
    }

    public IDbConnection OpenDbConnection()
    {
        var tenantId = Tenant.GetTenant();
        return OpenTenant(tenantId);
    }

    public IDbConnection OpenTenant(string tenantId = null)
    {
        return tenantId != null
            ? new OrmLiteConnectionFactory(_settings.GetString("TenantId{0}:{1}".Fmt(tenantId, "Frontend"))).OpenDbConnection()
            : _dbFactory.OpenDbConnection();
    }

    public IDbConnection CreateDbConnection()
    {
        return _dbFactory.CreateDbConnection();
    }
}

IoC registration

IDbFrontEndConnectionFactory feFactory = new FrontEndDbFactory(masterDbFactory, fileSettings)
{
    ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
};
container.Register(feFactory);

Global.asax

protected void Application_Start(object sender, EventArgs e)
{
    LogManager.LogFactory = new NLogFactory();
    new AppHost().Init();
}

protected void Application_BeginRequest(object src, EventArgs e)
{
    if (Request.IsLocal)
        Profiler.Start();
}
protected void Application_EndRequest(object src, EventArgs e)
{
    Profiler.Stop();
}

The Repository:

public partial class CampaignRepository : ICampaignRepository
{
    readonly ILog _log = LogManager.GetLogger(typeof(CampaignRepository));

    private readonly IDbFrontEndConnectionFactory _connectionFactory;

    public CampaignRepository(IDbFrontEndConnectionFactory connectionFactory)
    {
        _connectionFactory = connectionFactory;
    }
}

The IoC registration of my repository

container.Register<ICampaignRepository>(c => new CampaignRepository(c.Resolve<IDbFrontEndConnectionFactory>()));

The Service method when I'm inspecting the profiler

public CampaignViewModel Get(GetCampaign request)
{
    if (request == null) throw new ArgumentNullException("request");
    var profiler = Profiler.Current;
    using (profiler.Step("Campaign Service Get"))
    {
        try
        {
            var vm = CampaignRepository.GetCampaignInfo(request.Id).ToViewModel();
            if (vm.Campaign != null) return vm;
            Response.StatusCode = (int)HttpStatusCode.NotFound;
            return null;
        }
        catch (Exception exception)
        {
            _log.Error(request.ToJson(), exception);
            throw;
        }            
    }
}

I was expecting the profiler to show the sql timings, but that's not the case, the connection is not being profiled.

Is there something else that needs to be enabled or corrected for this to work?

Thank you, Stephen

Stephen Patten
  • 6,333
  • 10
  • 50
  • 84

1 Answers1

2

To enable SQL Profiling in MiniProfiler you need to register the OrmLiteConnectionFactory to use MiniProfilers ProfiledDbConnection, e.g:

Container.Register<IDbConnectionFactory>(c =>
    new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider) {
        ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
    });
mythz
  • 141,670
  • 29
  • 246
  • 390
  • Updated my question, registration of proper connection factory – Stephen Patten Nov 25 '14 at 18:54
  • The connection factories, I have a bunch of them, one per tenant... based on your the multi-tenant support from a prior post. Each tenant has 2 connections, because of the permission of the schema owners. – Stephen Patten Nov 25 '14 at 18:57
  • @Stephen Right, but everytime you create a connection factory you will also need to assign the ConnectionFilter. – mythz Nov 25 '14 at 19:31
  • So in apphost, there 2 connection factories (only one shown in the post) and each one is being set once, at construction. Then they are in the IoC and will be delivered when needed. If I were to examine the directly , I should see sql statements, right? I have to figure out the way we're ging to deliver this data to angularjs (no mvc for us :) – Stephen Patten Nov 25 '14 at 20:39
  • @Stephen Your `OpenTenant()` is creating a new `OrmLiteConnectionFactory` without assigning the `ConnectionFilter`. – mythz Nov 25 '14 at 20:41
  • Got it. Still reeling from the Oracle fall-out, I had to speak to my manager and other senior devs this morning about our strategy to resolve the Oracle Provider issues, which are many.. Thanks Demis, and Happy holidays my friend. – Stephen Patten Nov 25 '14 at 20:55