Using a Common PetaPoco Connection with StructureMap

This is my current implementation of StructureMap in Global.asax:

var container = (IContainer)IOCContainer.Initialize(); DependencyResolver.SetResolver(new StructureMapDependencyResolver(container)); 

Below is the code mentioned above:

 public static class IOCContainer { public static IContainer Initialize() { ObjectFactory.Initialize(x => { x.Scan(scan => { scan.TheCallingAssembly(); scan.WithDefaultConventions(); scan.AddAllTypesOf<IController>(); }); x.For<IConfigRepository>().Use<ConfigRepository>(); }); return ObjectFactory.Container; } } public class StructureMapDependencyResolver : IDependencyResolver { public StructureMapDependencyResolver(IContainer container) { _container = container; } public object GetService(Type serviceType) { if (serviceType.IsAbstract || serviceType.IsInterface) { return _container.TryGetInstance(serviceType); } else { return _container.GetInstance(serviceType); } } public IEnumerable<object> GetServices(Type serviceType) { return _container.GetAllInstances<object>() .Where(s => s.GetType() == serviceType); } private readonly IContainer _container; } 

I read that using a shared connection might improve performance a bit, so I was wondering how to use this in my MVC application. I think I would have to pass the newly created PetaPoco.Database object to the designers of my repositories ??

thanks

+4
source share
3 answers

I went with this if someone wants to know:

 x.For<PetaPoco.Database>().Singleton().Use(() => new PetaPoco.Database("DBConnection")); 
+2
source

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:

 // Open a connection (can be nested) public void OpenSharedConnection() { if (_sharedConnectionDepth == 0) { _sharedConnection = _factory.CreateConnection(); _sharedConnection.ConnectionString = _connectionString; _sharedConnection.Open(); if (KeepConnectionAlive) _sharedConnectionDepth++;// Make sure you call Dispose } _sharedConnectionDepth++; } 
+3
source

When you launch T4, which you get with PetaPoco, you will get the following available.

 {yourmodel}.ConnectionStringDB.GetInstance(); 

I look at instace, if he is, then he uses it, if he does not create another.

I could be wrong about that, though .. I can't completely remember.

0
source

All Articles