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 What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos