I have a web that was build using ASP.NET MVC 1.0. It uses Structuremap as an IOC container.
The IOC part works nice if I register it on Application_Start like this:
ObjectFactory.Initialize(service =>
{
service.ForRequestedType<IOrderRepository>()
.TheDefaultIsConcreteType<OrderRepository>()
.CacheBy(InstanceScope.PerRequest);
});
I have to use the same backend in a Windows Service.
The service has some timers in it that access OrderRepository simultaneously, so threading is an issue here.
My first idea is to register it in the constructor of the service like this:
public Service1()
{
ObjectFactory.Initialize(service =>
{
service.ForRequestedType<IOrderRepository>()
.TheDefaultIsConcreteType<OrderRepository>()
.CacheBy(InstanceScope.PerRequest);
});
}
Is this the right place and the right parameter for caching?
Reading the documentation of Structuremap, I think the safest way is to use the default setting for Caching:
PerRequest – The default operation. A new instance will be created for each request.
I had the impression that PerRequest meant HttpContext, but thats another entry:
HttpContext – A single instance will be created for each HttpContext. Caches the instances in the
HttpContext.Itemscollection.
Per this article: http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.aspx
It sounds like constructor is the way to go for the first time setup of structure map.