Dependency Inversion Principle (DIP) in C-Sharp

Hello friends in this article, we will learn Inversion of control (IOC) and Dependency Inversion Principle (DIP) in C#. In previous articles of SOLID design and pattern series we were learn, Interface segregation principle (ISP) i.e. I of SOLID principle.

Dependency Inversion Principle, provides us the technique to create loosely coupled concrete classes instead of high level classes.

According to DIP High level classes should not depend upon low level classes. Both should be depend upon abstraction.

Abstraction should not depends upon details and details depend upon abstraction.

To understand lets take an example of two classes one is ErrorLogWritter another is ErrorLogManager as shown below.

public class ErrorLogWritter { public void WriteLog(string error) { System.IO.File.WriteAllText(@"c:\ErrorLog.log", error); } } public class ErrorLogManager { ErrorLogWritter writer = null; public void Notify(string msg) { if(!string.IsNullOrEmpty(msg)) { writer = new ErrorLogWritter(); writer.WriteLog(msg); } } }

In above code ErrorLogWritter class responsibility to write error into the ErrorLog.log file and ErrorLogManager have responsibility to log errors if any error raised with in the entire application.

ErrorLogManager is high level class, which directly depends upon low level ErrorLogWritter concrete class. Here code will work fine.

This design fails the dependency inversion principle.

It will create tightly coupling between classes. With such classes, complex factor increases and understanding the code also increases.

Moreover if client demands to send email for error log and sms error log intimation service etc. Again more complexity increases with two more new classes one for email and other for sms error log service as shown code below.

using System; namespace SOLID { public class ErrorLogWritter { public void WriteLog(string error) { System.IO.File.WriteAllText(@"c:\ErrorLog.log", error); } } public class SMSService { public void WriteLog(string error) { // } } public class EmailService { public void WriteLog(string error) { // } } public class ErrorLogManager { ErrorLogWritter writer = null; EmailService email = null; SMSService sms = null; public void Notify(string msg) { if(!string.IsNullOrEmpty(msg)) { writer = new ErrorLogWritter(); email = new EmailService(); sms = new SMSService(); writer.WriteLog(msg); email.WriteLog(msg); sms.WriteLog(msg); } } } }

How to achieve it ?

Answer is Inversion of control (IOC)

Inversion of control, is the technique to convert tightly coupled classes into loosely coupling. We will achieve it by making higher level class ErrorLogManager to depend on abstractions (Interface)

rather than concrete implementation of lower level class ErrorLogWritter, SMSService and EmailService as shown below.

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

Still here is problem in our code. We are creating objects of concrete classes inside hight level class ErrorLogManager, which is against the Dependency Inversion Principle. We are now at same point, from where we were start developing application.

We can overcome this problem using Dependency injection technique.

We will learn Dependency Injection (DI) in next coming article soon. Till than thanks for reading SOLID design pattern series of articles.

In next article, we will learn Dependency Injection (DI).

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