Method injection technique of Dependency injection (DI)

In our previous article we were learn about Dependency Injection (DI) and How we can use Constructor injection technique of Dependency injection and today we are going to learn Method injection technique of Dependency injection (DI). Here we will again continue with our previous example 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; public void Notify(string msg) { if(!string.IsNullOrEmpty(msg)) { //Mapping of low level class to abstraction service = new ErrorLogWritter(); service.WriteLog(msg); service = new EmailService(); service.WriteLog(msg); service = new SMSService(); service.WriteLog(msg); } } } }

problem in our above code is, We are creating objects of concrete classes inside hight level class ErrorLogManager, which is violate the Dependency Inversion Principle.

To get rid of such problem we will apply Method injection technique of Dependency injection (DI).

In previous example we were applied Constructor injection, but In constructor injection, the dependent class will use the same concrete class for its entire lifetime.

Now if we need to pass separate concrete class on each invocation of the method, we have to pass the dependency in the method only. Which leads us to use Method Constructor. In which we creates a method with two arguments one for message as usual and other with Interface as shown below.

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

We can invoke concrete class methods as follows.

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

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 void Notify(string msg, IErrorLog obj) { if(!string.IsNullOrEmpty(msg)) { this.service = obj; this.service.WriteLog(msg); } } } public class program { static void Main(string[] args) { //DI ErrorLogManager errMgr = new ErrorLogManager(); errMgr.Notify("SMS Message goes here.", new SMSService()); errMgr = new ErrorLogManager(); errMgr.Notify("Email Message goes here.",new EmailService()); errMgr = new ErrorLogManager(); errMgr.Notify("Error Message goes here.", new ErrorLogWritter()); } } }

In next article, we will learn Property injection technique of Dependency injection.

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