In C#, there are various design patterns for code plan, design, and maintenance. Dependency Injection is a design pattern in which we write code in a loosely coupled manner. A loosely coupled code is injected at runtime if there is any logic required from that code. We write in a manner that enables the code to be independent when not to be used and can be injected when it is used. It is also called Inversion of Control.
This is a software design pattern that can be followed in any of the programming languages to write the code. In the context of C# let’s understand how we can achieve the benefit of Dependency Injection.
I am using Visual Studio 2017 for this.
Let’s take an example of a Service. For service, we usually Write Interface and implement it in a service class.
Let’s create a service MyService, for that will use the interface-service class approach.
Take an MVC project with Razor
On the root right click and create a folder Services.
Now add a class and name it MyService.cs
Now in the class create an Interface and implement this in your MyService class. Let’s write some code like this
Now we need a package Unity.MVC5 IoC Container to implement this thing in controller. Go to Nuget Package Manager –
Now in Nuget Package Manager GUI, click at Browse Tab and Search for Unity.MVC5. You will get a package named Unity.MVC5 by Paul Hiles. Click at Install in Right, it will install this.
Now if you will see there is a folder in Solution Explorer Named “App_Start”, In this, you will find a file “UnityConfig.cs”. Here you will configure Injection. This the IoC Container that Injects service. Here we map Interface to its class and IoC container maps the dependency injection at Runtime. All we need to do is, register our Service Class and Interface in this.
By Default UnityConfig.cs looks like this
Now to register our Service Class MyService by Registering it’s type (The interface IMyService), we need to write code like this –
Now after Configuring and registering the service in the UnityConfig.cs, we need to Register IoC Container Component in Global.asax.cs Application_Start Event. Like this –
Now in the controller, It can be injected in the constructor of the controller. In this way, this service will be injected in that controller context by using the IoC container we created and because we know the controller is initialized at run time, the dependency is also resolved at runtime in a loosely coupled manner.
Now let’s understand what we are doing and why.
Dependency Injection patterns say that apply a strategy of design in which dependencies must be loosely coupled. One object must not CREATE another object. It should do something that the object can consume another class at runtime without creating new instances. To acquire the runtime dependency of an object, we need an Inversion of Control (IoC) Container in which we register types and the container holds the logic of that type variant entity. Whenever such type is used with the context, the IoC table provides the logic without creating new instance. Means object of one class is not tightly coupled on another object of other class, in spite of this it just hold the reference/functionality of this that is injected with the requesting object at runtime.
For our effort, when the Application will run App_Start event, it will register the service in the IoC container. Now if we pass the interface type in the constructor of any class, then at runtime, the object of that class will not create an object of service, rather it will provide the members of the service by referencing the service.
Let’s do it by creating Controller and injecting for that controller – Let’s take a HomeController –
Go to Solution Explorer -> Right Click at Controller Folder. Click at Add and then Controller.
Name it HomeController.
First, you need to create an object of Interface IMyService (the interface that we take for example).
Keep it private so that can be used internally in the controller class.
Now we need to create a constructor of the controller so that when the object of the controller will be created by the application, it will inject the IoC Container’s Service (for which we created an object of the interface) at that time. Means what we did, we passed the service in the constructor of the controller and now a single object of the constructor will not create other instances of the service for the consuming its functionality. It is called the injected service. Write code in this way –
Now we can use members of service, like we want to use method of the service then we need to do something like this –
So, this is how we can make use of UnityMVC5 IoC container to have a dependency injection pattern in our MVC code.
To read more about dependency injection I recommend these links –