Dependency Injection (DI) in C-Sharp

Hello friends, in this article we will learn about Constructor injection and Dependency Injection (DI) in C-Sharp.

In our last article we were learn about Inversion of control (IOC) for implementing the Dependency Inversion Principle (DIP) .

Dependency injection is a wrapper of the Inversion of Control (IOC) principle.

The DI principle is a technique which is used to remove dependencies within Hight level Classes by enabling these dependencies injected externally using a separate framework or component.

In simple lines, Dependency Injection is used for injecting the concrete low level class objects into a high level class that is using abstraction (interface) inside. It remove the coupling between classes and move mapping of abstraction and concrete implementation outside the dependent high level class.

Dependency injection can be done in three ways.

1) Constructor injection

2) Method injection

3) Property injection

We will learn Constructor injection ahead in this article, Method injection and Property injection in our upcoming articles.

Lets proceed with Dependency Injection (DI). Here we will create constructor of ErrorLogManager class and pass object of the concrete class into its constructor as shown code bellow.

This technique is called Constructor injection in terms of Dependency Injection (DI).

public class ErrorLogManager { IErrorLog service = null; public ErrorLogManager(IErrorLog obj) { this.service = obj; } public void Notify(string msg) { if(!string.IsNullOrEmpty(msg)) { this.service.WriteLog(msg); } } }

By making such constructor we can do object mapping outside ErrorLogManager high level class.

Now we are able to pass object of any concrete class to the constructor of Hight level class as follows.

public class program { static void Main(string[] args) { //DI ErrorLogManager errMgr = new ErrorLogManager(new SMSService()); errMgr.Notify("SMS Message goes here."); errMgr = new ErrorLogManager(new EmailService()); errMgr.Notify("Email Message goes here."); errMgr = new ErrorLogManager(new ErrorLogWritter()); errMgr.Notify("Email Message goes here."); } }

The entire code will looks as follows.

namespace SOLID { public interface IErrorLog { void WriteLog(string error); } public class ErrorLogWritter: IErrorLog { public void WriteLog(string error) { System.IO.File.WriteAllText(@"c:\ErrorLog.log", error); } } public class SMSService: IErrorLog { public void WriteLog(string error) { // } } public class EmailService: IErrorLog { public void WriteLog(string error) { // } } public class ErrorLogManager { IErrorLog service = null; public ErrorLogManager(IErrorLog obj) { this.service = obj; } public void Notify(string msg) { if(!string.IsNullOrEmpty(msg)) { this.service.WriteLog(msg); } } } public class program { static void Main(string[] args) { //DI ErrorLogManager errMgr = new ErrorLogManager(new SMSService()); errMgr.Notify("SMS Message goes here."); errMgr = new ErrorLogManager(new EmailService()); errMgr.Notify("Email Message goes here."); errMgr = new ErrorLogManager(new ErrorLogWritter()); errMgr.Notify("Email Message goes here."); } } }

Here Dependency Injection technique is applied manually by code.

We can also use third party tools for Dependency Injection frameworks like Ninject, Unity, Spring.NET, Managed Extensibility Framework (MEF) etc.

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories