Ticker

6/recent/ticker-posts

Lifetime Manager in Unity Container

Lifetime Manager in Unity Container

Introduction In the Unity Container framework, a Lifetime Manager is responsible for managing the lifetime of objects created and resolved by the container. It determines how long an instance should be kept in memory and when it should be disposed of. By specifying a specific Lifetime Manager, developers can control the behavior of object instantiation and ensure proper resource management.

Built-in Lifetime Managers Unity Container provides several built-in Lifetime Managers that can be used to manage object lifetimes.

  1. TransientLifetimeManager: This is the default lifetime manager in Unity Container. It creates a new instance of the object every time it is resolved, ensuring that each resolution request results in a fresh instance.

Example:

csharp
container.RegisterType<IMyService, MyService>(new TransientLifetimeManager());

Explanation: In this example, the IMyService interface is registered with the MyService implementation using TransientLifetimeManager. Each time IMyService is resolved, a new instance of MyService will be created.

  1. ContainerControlledLifetimeManager: This lifetime manager creates a single instance of the object and returns the same instance for subsequent resolution requests. It maintains a singleton behavior throughout the container's lifetime.

Example:

csharp
container.RegisterType<IMySingletonService, MySingletonService>(new ContainerControlledLifetimeManager());

Explanation: Here, IMySingletonService is registered with MySingletonService using ContainerControlledLifetimeManager. The container will create a single instance of MySingletonService and return it for all resolution requests of IMySingletonService.

  1. HierarchicalLifetimeManager: This lifetime manager is similar to ContainerControlledLifetimeManager, but it creates a new instance for each child container. When a child container is disposed of, the instances created by it are also disposed of.

Example:

csharp
var childContainer = container.CreateChildContainer(); childContainer.RegisterType<IMyScopedService, MyScopedService>(new HierarchicalLifetimeManager());

Explanation: In this example, a child container is created using CreateChildContainer(). The IMyScopedService is registered with MyScopedService using HierarchicalLifetimeManager. Each child container will have its own instance of MyScopedService, and when the child container is disposed of, the instance will also be disposed of.

Custom Lifetime Managers Apart from the built-in lifetime managers, developers can also create custom lifetime managers by implementing the LifetimeManager abstract class provided by Unity Container. This allows for fine-grained control over object instantiation and disposal.

Example:

csharp
public class MyCustomLifetimeManager : LifetimeManager { private object instance; public override object GetValue() { return instance; } public override void SetValue(object newValue) { instance = newValue; } public override void RemoveValue() { instance = null; } }

Explanation: This example demonstrates a custom lifetime manager named MyCustomLifetimeManager that simply stores the instance in a private field. The GetValue() method returns the stored instance, SetValue() sets the instance, and RemoveValue() clears the instance.

To use the custom lifetime manager:

csharp
container.RegisterType<IMyCustomService, MyCustomService>(new MyCustomLifetimeManager());

Conclusion Lifetime Managers in Unity Container are crucial for managing object lifetimes and ensuring proper resource management. By choosing the appropriate lifetime manager, developers can control the behavior of object instantiation and disposal according to their application's requirements. Unity Container provides built-in lifetime managers, such as TransientLifetimeManager, ContainerControlledLifetimeManager, and HierarchicalLifetimeManager. Additionally, developers can create custom lifetime managers by implementing the LifetimeManager abstract class.

Post a Comment

0 Comments