Unity Container: Property Injection

Using Property Injection with Unity Container in C#

Unity Container is a powerful dependency injection (DI) framework for managing object lifetimes and resolving dependencies in .NET applications. In this article, we will explore Property Injection with Unity Container, a technique that allows you to inject dependencies into properties of objects. We’ll cover the concept, benefits, and provide practical examples in C#.

What is Property Injection?

Property Injection is a form of dependency injection where dependencies are provided to an object’s properties rather than through constructor parameters. This technique allows you to inject services or dependencies into an object after it has been created, making it particularly useful for optional or mutable dependencies.

Benefits of Property Injection:

  1. Flexibility: Property Injection is suitable for injecting optional dependencies that may not be required during object creation but can be set later.
  2. Decoupling: It helps reduce the coupling between components, as dependencies are injected after object creation.
  3. Default Values: Property Injection allows you to provide default values for properties if a dependency is not explicitly set.

Using Property Injection with Unity Container:

To demonstrate Property Injection with Unity Container, follow these steps:

Step 1: Install Unity Container

If you haven’t already installed Unity Container in your project (as described in a previous article), make sure to do so using NuGet Package Manager.

Step 2: Define the Dependencies

Create the interfaces and classes for your dependencies. For example:

public interface ILogger
{
    void Log(string message);
}

public class ConsoleLogger : ILogger
{
    public void Log(string message)
    {
        Console.WriteLine(message);
    }
}

Step 3: Configure Unity Container

Configure Unity Container to resolve your dependencies and inject them into properties. Here’s an example:

using Unity;

class Program
{
    static void Main(string[] args)
    {
        // Create a Unity container
        IUnityContainer container = new UnityContainer();

        // Register the ILogger dependency
        container.RegisterType<ILogger, ConsoleLogger>();

        // Resolve the object with Property Injection
        MyClass myObject = new MyClass();
        container.BuildUp(myObject);

        // Use the object with injected dependency
        myObject.LogMessage("Property Injection Example");

        Console.ReadKey();
    }
}

public class MyClass
{
    [Dependency]
    public ILogger Logger { get; set; }

    public void LogMessage(string message)
    {
        Logger.Log(message);
    }
}

In this example:

  • We register the ILogger dependency with Unity Container.
  • The MyClass class has a property Logger marked with [Dependency], indicating it should be injected with the ILogger dependency.
  • We use container.BuildUp(myObject) to inject the dependency into the property of myObject.
  • Finally, we call the LogMessage method, which uses the injected Logger to log a message.

Conclusion

Property Injection with Unity Container provides flexibility and decoupling in managing dependencies for your .NET applications. It allows you to inject dependencies into properties of objects after creation, making it a valuable technique for various scenarios, including optional or mutable dependencies.