c-sharp
  1. c-sharp-unity-container-c

C# Unity Container

In C#, the Unity Container is a dependency injection container that provides a centralized way to manage the dependencies between objects in your application. It allows you to define a set of mappings between interfaces and implementation classes, and then automatically resolves these dependencies at run-time. In this tutorial, we'll discuss how to use the Unity Container in your C# application.

Syntax

The syntax for creating a Unity Container in C# is as follows:

IUnityContainer container = new UnityContainer();

Once you have created a container, you can register your types and their dependencies using the RegisterType method.

container.RegisterType<IService, ServiceImplementation>();

Finally, you can resolve your dependencies using the Resolve method.

IService service = container.Resolve<IService>();

Example

Let's say we have an interface IAnimal and a class Dog that implements this interface. We can use the Unity Container to register the Dog class when the IAnimal interface is requested.

public interface IAnimal {
    string MakeSound();
}

public class Dog : IAnimal {
    public string MakeSound() {
        return "Woof woof!";
    }
}

We can then use the Unity Container to resolve the interface and get an instance of the Dog class.

IUnityContainer container = new UnityContainer();
container.RegisterType<IAnimal, Dog>();

IAnimal animal = container.Resolve<IAnimal>();
Console.WriteLine(animal.MakeSound()); // Output: "Woof woof!"

Output

When we run the example code above, the output will be:

Woof woof!

This is because we registered the Dog class with the Unity Container and resolved the dependency for the IAnimal interface, which returned an instance of the Dog class and invoked its MakeSound method.

Explanation

In the example above, we defined an interface IAnimal and a class Dog that implements this interface. We then used the Unity Container to register the Dog class when the IAnimal interface is requested.

We created a container instance using the UnityContainer class and registered the Dog class as the implementation of the IAnimal interface using the RegisterType method. We then resolved the dependency for the IAnimal interface using the Resolve method.

Finally, we called the MakeSound method on the resolved instance of the IAnimal interface and printed the output to the console.

Use

The Unity Container is useful for managing the dependencies between objects in your application. It makes it easy to configure your application to use different implementations of your services, depending on your application requirements.

Important Points

  • The Unity Container can register and resolve any type of object, not just interfaces and classes.
  • The Unity Container can be configured to use different lifetimes for your objects, such as a singleton, transient or container-controlled lifetime.
  • The Unity Container can be extended using extension methods or by creating your own custom extensions.

Summary

In this tutorial, we discussed how to use the Unity Container in your C# application. We covered the syntax, example, output, explanation, use, and important points of the Unity Container in C#. With this knowledge, you can now use the Unity Container in your C# application to manage the dependencies between objects and easily configure your application to use different implementations of your services.

Published on: