This is my current implementation of StructureMap in Global.asax:
var container = (IContainer)IOCContainer.Initialize();
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
Below is the code that is refered to 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 have read that using the shared connection may improve performance a little bit so I was wondering how to use this in my MVC app. I guess I would have to pass a newly created PetaPoco.Database object to the constructors of my repositories??
Thanks