# LightInject
**Repository Path**: lindexi/LightInject
## Basic Information
- **Project Name**: LightInject
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2016-12-01
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
[]()
[]()
[]()
## Installing ##
**LightInject** provides two distribution models via NuGet
### Binary ###
PM> Install-Package LightInject
This adds a reference to the LightInject.dll in the target project.
### Source ###
PM> Install-Package LightInject.Source
This will install a single file (LightInject.cs) into the current project.
### Creating a container###
var container = new LightInject.ServiceContainer();
The container implements IDisposable and should be disposed after usage has completed. It can also be used inside of a using statement for a constrained scope.
### Default services###
public interface IFoo {}
public class Foo : IFoo {}
---
container.Register();
var instance = container.GetInstance();
Assert.IsInstanceOfType(instance, typeof(Foo));
### Named services ###
public class Foo : IFoo {}
public class AnotherFoo : IFoo {}
---
container.Register();
container.Register("AnotherFoo");
var instance = container.GetInstance("AnotherFoo");
Assert.IsInstanceOfType(instance, typeof(AnotherFoo));
If only one named registration exists, **LightInject** is capable of resolving this as the default service.
container.Register("AnotherFoo");
var instance = container.GetInstance();
Assert.IsInstanceOfType(instance, typeof(AnotherFoo));
### Unresolved services ###
LightInject can resolve services that are not registered with the container using the *RegisterFallback* method.
var container = new ServiceContainer();
container.RegisterFallback((type, s) => true, request => new Foo());
var foo = container.GetInstance();
The first argument to the *RegisterFallback* method makes it possible to possible to decide if the service can be "late-resolved".
The second argument is a *ServiceRequest* instance that provides the requested service type and service name.
### IEnumerable<T> ###
When we register multiple services with the same service type, **LightInject** is capable of resolving these services as an [IEnumerable<T>](http://msdn.microsoft.com/en-us/library/9eekhta0.aspx).
public class Foo : IFoo {}
public class AnotherFoo : IFoo {}
---
container.Register();
container.Register("AnotherFoo");
var instances = container.GetInstance>()
Assert.AreEqual(2, instances.Count());
Alternatively using the **GetAllInstances** method.
var instances = container.GetAllInstances();
Assert.AreEqual(2, instances.Count());
In addition, **LightInject** supports the following [IEnumerable<T>](http://msdn.microsoft.com/en-us/library/9eekhta0.aspx) sub-types.
* Array
* ICollection<T>
* IList<T>
* IReadOnlyCollection<T> (Net 4.5 and Windows Runtime);
* IReadOnlyList<T> (Net 4.5 and Windows Runtime)
By default, **LightInject** will resolve all services that are compatible with the requested element type.
container.Register();
container.Register();
var instances = container.GetAllInstances();
Assert.AreEqual(2, instances.Count());
This behavior can be overridden using the **EnableVariance** container option.
var container = new ServiceContainer(new ContainerOptions { EnableVariance = false });
container.Register();
container.Register();
var instances = container.GetAllInstances();
Assert.AreEqual(1, instances.Count());
### Values ###
Registers the value as a constant.
container.RegisterInstance("SomeValue");
var value = container.GetInstance();
Assert.AreEqual("SomeValue, value);
##Lifetime##
The default behavior in **LightInject** is to treat all objects as transients unless otherwise specified.
container.Register();
var firstInstance = container.GetInstance();
var secondInstance = container.GetInstance();
Assert.AreNotSame(firstInstance, secondInstance);
###PerScopeLifetime###
Ensures that only one instance of a given service can exists within a scope.
The container will call the **Dispose** method on all disposable objects created within the scope.
container.Register(new PerScopeLifetime());
using(container.BeginScope())
{
var firstInstance = container.GetInstance();
var secondInstance = container.GetInstance();
Assert.AreSame(firstInstance, secondInstance);
}
**Note:** *An **InvalidOperationException** is thrown if a service registered with the **PerScopeLifetime** is requested outside the scope.*
###PerContainerLifetime###
Ensures that only one instance of a given service can exist within the container.
The container will call the Dispose method on all disposable objects when the container itself is disposed.
using(container = new ServiceContainer())
{
container.Register(new PerContainerLifetime());
var firstInstance = container.GetInstance();
var secondInstance = container.GetInstance();
Assert.AreSame(firstInstance, secondInstance);
}
###PerRequestLifeTime###
A new instance is created for each request and the container calls **Dispose** when the scope ends.
This lifetime is used when the conrete class implements **IDisposable**.
container.Register(new PerRequestLifeTime());
using(container.BeginScope())
{
var firstInstance = container.GetInstance();
var secondInstance = container.GetInstance();
Assert.AreNotSame(firstInstance, secondInstance);
}
>**Note:** *An **InvalidOperationException** is thrown if a service registered with the **PerRequestLifeTime** is requested outside the scope.*
###Custom lifetime###
A custom lifetime is created by implementing the **ILifetime** interface
internal interface ILifetime
{
object GetInstance(Func