StructureMap DBServiceRegistry and MVC-mini-profiler?

Viewed 384

If I use this code in each Repository class then I get SQL profiling to work but I want to move that code from each class into the class where StructureMap handles the DB.

Example of a Repository class:

public DB CreateNewContext()
    {
        var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
        var profiledConnection = ProfiledDbConnection.Get(sqlConnection);
        return DataContextUtils.CreateDataContext<DB>(profiledConnection);
    }

    public SqlRecipeRepository(DB dataContext)
    {         
        _db = CreateNewContext();
    }

Now I want the dataContext variable to be the profiled version and so come from my DBServiceRegistry class.

Here is the DBServiceRegistry class:

public class DBServiceRegistry : Registry
{
    public DBServiceRegistry()
    {
        var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["GetMeCooking.Data.Properties.Settings.server"].ConnectionString);
        var profiledConnection = ProfiledDbConnection.Get(sqlConnection);
        For<DB>().HybridHttpOrThreadLocalScoped().Use(() => DataContextUtils.CreateDataContext<DB>(profiledConnection));

        //Original method just had this:
        //For<DB>().HybridHttpOrThreadLocalScoped().Use(() => new DB());

    }
}

This code does not cause any errors but I don't get the SQL profiling, what am I doing wrong?

1 Answers
Related