Content
Lifecycles
With classes that implement the interface ILifecycle, its possible to controll the lifetime / reuse of instances created from a LightCore-Container.
LightCore supports out-of-the-box four lifecycle-implementations. The TransientLifecycle, SingletonLifecycle, ThreadSingletonLifecycle and HttpRequestLifecycle.
The lifecycle to use have to be declared on the registration.
Via Code:
var builder = new ContainerBuilder();
builder.Register<IFoo, Foo>().ControlledBy<TransientLifecycle>();
Via Xml:
<Registration Lifecycle="Transient" ContractType="LightCore.TestTypes.IFoo" ImplementationType="LightCore.TestTypes.Foo">
TransientLifecycle
The TransientLifecycle creates every time a new instance of the registered type.
This is the standard lifecycle of LightCore.
var builder = new ContainerBuilder();
builder.Register<ILampenfassung, Glühbirne>().ControlledBy<TransientLifecycle>();
- Important
-
For all other lifecycle it is important to know that he lifetime and reuse possiblity is chained to the container.
If you create a new container, all instances are no more available.
Given that fact, its common to hold one container instance on the application scope / static.
SingletonLifecycle
The SingletonLifecycle is a kind of singleton pattern.
The type registered on SingletonLifecycle, once requested, returns the same instance for all request.
var builder = new ContainerBuilder();
builder.Register<ILampenfassung, Glühbirne>().ControlledBy<SingletonLifecycle>();
ThreadSingletonLifecycle
The ThreadSingletonLifecycle is kind of SingletonLifecycle.
The difference is that every thread have its own instance, and it will be reused on the corresponding threads. One thread = one instance
var builder = new ContainerBuilder();
builder.Register<ILampenfassung, Glühbirne>().ControlledBy<ThreadSingletonLifecycle>();
HttpRequestLifecycle
With the HttpRequestLifecycle one instance is shared from begin to the end of one request, and only in that request. If the request ends, the instance is abandoned.
var builder = new ContainerBuilder();
builder.Register<ILampenfassung, Glühbirne>().ControlledBy<HttpRequestLifecycle>();
- Important
-
To use the HttpRequestLifecycle the LightCore.Integration.Web.dll have to referenced in in your project.
Additional themes to lifecycle
Define a default lifecycle
It is possible to change the default lifecycle. Call the method DefaultControllerBy<TLifecycle>
of the ContainerBuilder.
A untyped variant of this method, which takes a Type instance, is available too.
var builder = new ContainerBuilder();
builder.DefaultControlledBy<SingletonLifecycle>();
Develop a lifecycle your own
If you have a need for a own lifecycle, its easy and possible.
Your new lifecycle class have to implement the interface ILifecycle from the namespace LightCore.Lifecycle.
In the following example, we have developed a fictive lifecycle, which - because of security reason - not accept to reuse an instance more than three times.
public class SecurityLifecycle : ILifecycle
{
private int _count;
private object _instance;
private readonly object _lock = new object();
public object ReceiveInstanceInLifecycle(Func<object> newInstanceResolver)
{
lock(_lock)
{
if (this._instance == null || this._count > 3)
{
this._instance = newInstanceResolver();
this._count = 0;
}
this._count += 1;
return this._instance;
}
}
}
As example, you are free to explore the buildin lifecycles.
Set lifecycle in XML configuration
Its possible to set the lifecycle on the registration of types with XML too.
<?xml version="1.0" encoding="utf-8"?>
<LightCoreConfiguration xmlns="clr-namespace:LightCore.Configuration;assembly=LightCore.Configuration">
<LightCoreConfiguration.TypeAliases>
<TypeAlias Alias="SingletonLifecycle" Type="LightCore.Lifecycle.SingletonLifecycle, LightCore" />
</LightCoreConfiguration.TypeAliases>
<LightCoreConfiguration.Registrations>
<Registration
ContractType="LightCore.TestTypes.ILampenfassung{2}, LightCore.TestTypes"
ImplementationType="LightCore.TestTypes.Gluehbirne{2}, LightCore.TestTypes"
Lifecycle="SingletonLifecycle" />
</LightCoreConfiguration.Registrations>
</LightCoreConfiguration>
Its a good practice to use aliases for the lifecycle, you want to use.
The lifecycles included in LightCore have already aliases registered, so you can use e.g. "Singleton", "ThreadSingleton", etc..
<LightCoreConfiguration.TypeAliases>
<TypeAlias Alias="SingletonLifecycle" Type="LightCore.Lifecycle.SingletonLifecycle, LightCore" />
</LightCoreConfiguration.TypeAliases>
On the type registration, the alias of the lifecycle is declared.
<Registration
ContractType="…"
ImplementationType="…"
Lifecycle="SingletonLifecycle" />
Its possible to change the default lifecycle within the XML configuration.
<LightCoreConfiguration
xmlns="clr-namespace:LightCore.Configuration;assembly=LightCore.Configuration"
DefaultLifecycle="LightCore.Lifecycle.SingletonLifecycle, LightCore">
You can find additional information to XML configuration on: XML Configuration.