We're in the process of evaluating SimpleInjector and LightInject to replace our current Unity implementation in our application. Unity has a method that allows a hierarchy of containers with a call to CreateChildContainer(). Is there an equivalent for SimpleInjector and LightInject?
Asked
Active
Viewed 526 times
3
-
Simple Injector has the LifetimeScope lifestyle for this. Can you give some specific examples of what you'd like to achieve? – Steven Feb 13 '14 at 06:24
-
Where can I get a list of restrictions for both of these frameworks? i.e. list of what these frameworks don't yet do compared to Unity. – Ray Feb 13 '14 at 17:32
-
You can take a look at [this feature comparison](http://featuretests.apphb.com/DependencyInjection.html), which is based on [this blog](http://blog.ashmind.com/2008/08/19/comparing-net-di-ioc-frameworks-part-1/). – Steven Feb 13 '14 at 18:47
1 Answers
3
LightInject uses the PerScopeLifetime and the PerRequestLifetime for this.
PerScopeLifetime creates just one instance of a given service per scope and disposes the service instance at the end of the scope if it implements IDisposable.
PerRequestLifetime creates new instances for each request (GetInstance) and disposes all instances at the end of the request.
The scope itself is started using the BeginScope method.
PerScopeLifetime
container.Register<IFoo, Foo>(new PerScopeLifetime());
using(container.BeginScope())
{
var firstInstance = container.GetInstance<IFoo>();
var secondInstance = container.GetInstance<IFoo>();
Assert.AreSame(firstInstance, secondInstance);
} //<- Instances implementing IDisposable are disposed here.
PerRequestLifetime
container.Register<IFoo, Foo>(new PerRequestLifetime());
using(container.BeginScope())
{
var firstInstance = container.GetInstance<IFoo>();
var secondInstance = container.GetInstance<IFoo>();
Assert.AreNotSame(firstInstance, secondInstance);
} //<- Instances implementing IDisposable are disposed here.
Normally the BeginScope method is something that is called by the various extensions available to LightInject.
Examples of this can be found in LightInject.Web, LightInject.Mvc and LightInject.WebApi.

qujck
- 14,388
- 4
- 45
- 74

seesharper
- 3,249
- 1
- 19
- 23
-
That 's pretty much how it works in Simple Injector, except that they are called WebRequestLifestyle and LifetimeScopeLifestyle. – Steven Feb 13 '14 at 07:09
-
Steven, I looked the comparison sheet at http://featuretests.apphb.com/DependencyInjection.html. Can you tell me if any of the info on that page concerning SimpleInjector are wrong? It seems that LightInject has a higher score. I'm wondering now if the page is out-dated. – Ray Feb 17 '14 at 15:50
-
@Ray: The comparison is still accurate. The Simple Injector score is quite low because of two reasons. 1. Some features are supported but didn't fit the generic test suite (that's when you see 'note'). And 2. Many features are deliberately left out, because we feel it steers developers into the wrong direction (such as auto-registering Lazy
and Func – Steven Mar 08 '14 at 14:39), while those features could be implemented rather easily if required. (Side note: don't forget to use the @ sign in front of the name if you ask a question to make sure that person gets notified. I accidentally stumbled upon your question.)