tags:

views:

122

answers:

3

My controllers now implement an interface and inject a repository implementation using the Unity framework. My question is, are these injected implementations singletons? Does that mean all of my users hitting the repository (from the controller) use the same instance?

A: 

No, do not make repositories singleton, special in a web application.

The controller factory create the repository (using Unity) and inject them in the controller.

ema
I definitely do not want singletons. I was asking if the implementations injected by Unity were singletons.
RailRhoad
Don't know about unity but with other framework you can control the "lifestyle" of the object specifing if they are singleton or transient during the registration
ema
A: 

Taken from the MSDN Unity docs...

The lifetime of the object it builds will correspond to the lifetime you specify in the parameters of the method. If you do not specify a value for the lifetime, the type is registered for a transient lifetime, which means that a new instance will be created on each call to Resolve...

Include an instance of the ContainerControlledLifetimeManager class in the parameters to the RegisterType method to instruct the container to register a singleton mapping.

So basically, the injection is transient unless you specify it as a singleton.

RailRhoad
A: 

It depends on whether you carry some ORM session along with your repository instance. If your repository is just a bunch of static methods than you shouldn't care and you can make it a singleton. Otherwise you want to preserve the state only within one web request so that other requests threads don't interfere with it.

Unity, unlike other IoC frameworks. doesn't come with singleton per web request lifetime manager, but you can easily implement it, see here for instance: http://stackoverflow.com/questions/1151201/singleton-per-web-request-in-unity

deadbeef