Unity Application Block, a lightweight dependency injection container, can build loosely coupled applications that are extensible and easier to test and maintain Similar to Castle Windsor and StructureMap, Unity Application Block is also an IoC (Inversion of Control) container. The Unity Application Block from Microsoft is a light weight extensible dependency injection container. It provides support for constructor injection, property injection and also method call injection. Incidentally, the Unity Application Block was introduced as part of the Enterprise Library. In case you are not familiar with what Dependency Injection and Inversion of Control is all about, here’s a quick explanation. Dependency Injection is a realization of the IoC principle. Both inversion of control and dependency injection are ways that enable you to break the dependencies between the components in your application. The Dependency Injection principle states that the high level modules in an application should not depend on the low level modules; rather, both should depend on abstractions. Unity Application Block design goals The Unity Application Block is a Dependency Injection (DI) container. Note that the Unity Application Block doesn’t have any dependency on the Enterprise Library configuration system. Hence, you can use it as a stand – alone dependency injection container sans the Enterprise Library being installed in your system. The design goals of the Unity Application Block include the following: Promoting a modular design through decoupling Providing a fast, extensible, light weight dependency injection container Provide support for extensibility through extensions Provide support for attribute driven injection Provide support for an intuitive API to connect to and work with the dependency injection container Getting started In this section we will explore on how we can get started using the Unity Application Block in our applications. The first step should be to get the Unity Application Block installed in your system. The easiest way to install this library is through NuGet. For the purposes of this illustration we will use a console application project here. To build the first application using the Unity Application Block, follow these steps: Open Visual Studio IDE Create a Console Project and save it with a name Right click on the project in the Solution Explorer Window Select “Manage NuGet Packages…” Install Unity NuGet Package Manager That’s all you would need to do to set the stage for you to start using Unity. You are now ready to use Unity Application Block in your project. Creating and resolving object dependencies using Unity container You can use the Unity container to resolve dependencies on a particular object easily as shown in the code snippet that follows. IUnityContainer container = new UnityContainer(); container.RegisterType<IRespository, ProductRepository>(); container.RegisterType<ILogger, DatabaseLogger>(); When you register the type of an object with the Unity container, you can specify the lifetime. If you don’t specify any, the default lifetime is used. A lifetime manager controls the lifetime of a registered object. The types of the lifetime managers supported by the Unity Application Block include: TransientLifetimeManager, ContainerControlledLifetimeManager, HierarchicalLifetimeManager, PerThreadLifetimeManager and ExternallyControlledLifetimeManager. Consider the following interface called ILogger. public interface ILogger { string GetLogTypeName(); } The ILogger interface contains the declaration of one method named, GetLogTypeName(). The FileLoger, DatabaseLogger and EventLogger classes (given below) implement the ILogger interface. public class FileLogger : ILogger { public string GetLogTypeName() { return "File Logger"; } } public class DatabaseLogger: ILogger { public string GetLogTypeName() { return "Database Logger"; } } public class EventLogger: ILogger { public string GetLogTypeName() { return "Event Logger"; } } The following code listing shows how you can resolve dependencies using UnityContainer. static void Main(string[] args) { IUnityContainer container = new UnityContainer(); container.RegisterType<ILogger, FileLogger>(); ILogger iLogger = container.Resolve(); string logType = iLogger.GetLogTypeName(); Console.WriteLine(logType); Console.Read(); } Note that the “Container” in Unity Application Block is the object that can be used to create and inject dependencies. You can register types or type mappings with the Unity container using the RegisterType method. The Resolve() method is used to return a concrete instance of the type that is registered for the generic type mentioned using T. In the code example given above, the Resolve() method will return an instance of the FileLogger class. An alternative approach to specify the Unity integration is through configuration. Assuming that you have specified a container named IDGContainer in your Unity configuration, the following code snippet illustrates how you can call the LoadConfiguration method on the container instance in your code. string containerName = "IDGContainer"; IUnityContainer container = new UnityContainer().LoadConfiguration(containerName); Related content how-to How to use FastEndpoints in ASP.NET Core Take advantage of the free open-source FastEndpoints library to build fast and lean APIs in your ASP.NET Core applications. By Joydip Kanjilal Jul 11, 2024 7 mins Microsoft .NET C# Development Libraries and Frameworks how-to How to use Refit to consume APIs in ASP.NET Core Take advantage of Refit REST library to simplify API consumption and make your code cleaner, more efficient, and easier to maintain. By Joydip Kanjilal Jul 04, 2024 10 mins C# Microsoft .NET Software Deployment how-to When to use an abstract class vs. interface in C# Understanding the differences between an abstract class and interface is key to designing loosely coupled and extensible applications. By Joydip Kanjilal Jun 20, 2024 10 mins Small and Medium Business Microsoft .NET C# how-to 6 security best practices for ASP.NET Core Learn the best practices and built-in safeguards for preventing attacks and protecting sensitive data in your ASP.NET Core web applications. By Joydip Kanjilal Jun 07, 2024 6 mins C# Microsoft .NET Web Development Resources Videos