I can only speak for Autofac, like what I use in my project. This may not apply to what you are trying to do, but I could also share it. To get the petapoco database object for each HTTP request, I have this config in the global.asax.cs file
builder.RegisterType<MyProject.ObjectRelationalMapper.PetaPoco.Database>() .As<MyProject.ObjectRelationalMapper.PetaPoco.Database>() .WithParameters( new List<NamedParameter>() {new NamedParameter( "connectionStringName", "MyProjectConnectionString")}) .InstancePerHttpRequest();
MyProject.ObjectRelationalMapper.PetaPoco is just my renameapaced petapoco.cs.
In Autofac, you can specify which version of the constructor to call by specifying which parameters you pass through WithParameters (). When constructing an object, it finds a constructor with the appropriate parameters.
Each time the constructor enters its dependencies, it uses the same petapoco database object in the entire HTTP request, because this is what I told Autofac to execute (InstancePerHttpRequest)
My controller constructor accepts INextMatchService as a dependency, which in turn accepts INextMatchRepository as a dependency:
public NextMatchRepository( Database database, ISessionWrapper sessionWrapper) { this._database = database; this._sessionWrapper = sessionWrapper; }
The type "Database" is MyProject.ObjectRelationalMapper.PetaPoco.Database, which is built in the above code snippet. Now my repository can work with a shared database connection. When you work with Petapoco functions, it checks to see if there is already a connection to use if it increments the counter and uses the object:
source share