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 feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos