Hello guys, Today I will introduce you to the Microsoft Unity Framework and also take you through its dependency injection feature. First of all we will see “What is Dependency Injection.?”.In simply Dependency Injection is decouples the module dependencies in your application by creating and injecting the object at runtime.
The dependency of a class can be injected at runtime using the Dependency Injection mechanism. Unity will help us to handle those things very easily.
It is a two-step process.
- Register the dependency
- then resolve the concrete business module class.
RegisterType and RegisterInstance methods of the UnityContainer classes can be used to perform the Register operation. And Resolve method can be used for creating the concrete business class instance.
Today i will show you guys how should we use the NamedRegistration for the handling Dependency Injection. In the real world applications there might be situations like we want to dynamically create objects according to the requirements. When using Unity, Unity knows that which class object is need to be created.
Let’s suppose example like this. According the the user input we need to change the database which is going to save the data. Here i will use SQL and Oracle.
For the explanation we will create a console application and do dependency constructor injection using Unity. Then you need to add the Unity via NuGet. So go to the NuGet package manager and search for the unity and install it.