Property injection technique of Dependency injection

In our previous articles we were discussed about Dependency Injection technique like Constructor injection and Method injection technique of Dependency injection. Today we are going to learn about Property Injection technique of Dependency Injection.

Lets review our previous code once.

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); } } } }

In constructor injection, we were learn that the dependent class will use one concrete class for the entire lifetime.

In method injection, we can pass the concrete class object in the action method itself.

Problem:

But what if the responsibility of selection of concrete class and invocation of method are in separate places.

Solution:

In such cases we need property injection. We will create an property of Interface type IErrorLog for Dependent class ErrorLogManager and invoke WriteLog method by Interface object with in dependent class as shown below.

public class ErrorLogManager { public IErrorLog ErrorLog { get { return _errorlog; } set { _errorlog = value; } } public void Notify(string msg, IErrorLog obj) { if(!string.IsNullOrEmpty(msg)) { ErrorLog.WriteLog(msg); } } IErrorLog _errorlog = null; }

Now we need to assign the object of the concrete class to the property of the dependent class as shown below.

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

The entire code will looks like 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 { public IErrorLog ErrorLog { get { return _errorlog; } set { _errorlog = value; } } public void Notify(string msg) { if(!string.IsNullOrEmpty(msg)) { ErrorLog.WriteLog(msg); } } IErrorLog _errorlog = null; } public class program { static void Main(string[] args) { //DI ErrorLogManager errMgr = new ErrorLogManager(); errMgr.ErrorLog = new SMSService(); errMgr.Notify("SMS Message goes here."); errMgr = new ErrorLogManager(); errMgr.ErrorLog = new EmailService(); errMgr.Notify("Email Message goes here."); errMgr = new ErrorLogManager(); errMgr.ErrorLog = new ErrorLogWritter(); errMgr.Notify("Error Message goes here."); } } }

In the above code the setter of ErrorLog property of dependent class ErrorLogManager, will take the concrete class objects SMSService, ErrorLogWritter, EmailService and bind it to the interface IErrorLog.

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