Learning The S.O.L.I.D Programming Principles: Dependency inversion principle [Part – VI]

History:

In our previous posts we learned ‘What is S.O.L.I.D. Programing Principles’ and a detailed explalantion with code of
Single Responsibility Principle, Open/closed Principle, Liskov Substitution Principle and Interface Segregation Principle.


S.O.L.I.D. is an acronym introduced by Michael Feathers as:

  1. S for SRP: Single responsibility principle
  2. O for OCP: Open/closed principle
  3. L for LSP: Liskov substitution principle
  4. I for ISP: Interface segregation principle
  5. D for DIP: Dependency inversion principle
  • Single Responsibility Principle says class should have single responsibility. In reference to this I would say “A class should have single responsibility”.
    Lets dive into ocean – can we read this like “a class should not design to do multiple activities”.
  • Open/Closed Principle says “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”.
  • Liskov Substitution Principle says “if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may be substituted for objects of type T)
    without altering any of the desirable properties of that program (correctness, task performed, etc.)”
  • Interface Segregation Principle says “No clients should be forced to implement methods which it does not use and the contract should be broken into small and more specific interfaces.”

Learning S.O.L.I.D is very vast topic and can’t possible to explore in one-shot. I divided this into following parts:

Introduction

In this whole article, we will learn Dependency Inversion Principle in details with example.

Here is a definition from wiki:

“High level moudles should not depends upon low-level modules both should be tide using abstractions”

Dependency Inversion Principle (DIP)

This principle remembers us Decoupling.

Lets explore this with an example:

Rewind code example we discussed Single Responsible Principle, there is a property Type which tells data type, now take a look into
following snippet

public class DataBase : IRule, IRepository
{
	private ISession session;
	public virtual bool IsValid(ServerData data, SourceServerData sourceData)
	{
		//implementation goes here
	}

	public virtual void Save(ServerData data)
	{
		if(data.Type == 1)
		{
			session = new ServerDataSession();
		}
		else
		{
			session = new SourceServerDataSession();
		}
		
		session.Save(data);
	}
}

There is violation of SRP in above, to solve this lets create a separate interface which holds, save method:

interface ISession
{
	void Save<T>(T data);
}

and lets create different saver as per below snippet:

class ServerDataSession:ISession
{
	public void Save<T>(T data)
	{
		//save logic goes here
	}
}
class SourceServerDataSession:ISession
{
	public void Save<T>(T data)
	{
		//save logic goes here
	}
}

Now, what?

We need to supply Session as per Data type, here we are delegating responsibility to someone else. In other words, for above snippet our DataBase class
is delegating its responsibility to others (ServerDataSession,SourceServerDataSession).

Need to modify our DataBase class as:

public class DataBase : IRule, IRepository
{
	private ISession _session;

	public DataBase(ISession session)
	{
		_session = session;
	}
	public virtual bool IsValid(ServerData data, SourceServerData sourceData)
	{
		//implementation goes here
	}

	public virtual void Save(ServerData data)
	{
		_session.Save<ServerData>(data);
	}
}

At this point, our client is free to inject what he/she wants to consume:

IRepository dataBase = new Database(new ServerDataSession());

Conclusion:

Till this article, we learned S.O.L.I.D. programing principles to make our applications/software design better. This is just a start, as soon as you go through the
other Design Patter you will grab more power to make your software more beautify.

We learned:

S.O.L.I.D. is an acronym introduced by Michael Feathers as:

  1. S for SRP: Single responsibility principle
  2. O for OCP: Open/closed principle
  3. L for LSP: Liskov substitution principle
  4. I for ISP: Interface segregation principle
  5. D for DIP: Dependency inversion principle

How to download source-code?

You can download complete souce code of examples used in this article from GitHub: Learning Solid.

4 comments on “Learning The S.O.L.I.D Programming Principles: Dependency inversion principle [Part – VI]”

  1. Pingback: Learning The S.O.L.I.D Programming Principles: Open Closed Principle [Part - III] Gaurav-Arora.com

  2. Pingback: Learning The S.O.L.I.D Programming Principles: Single responsibility principle [Part - II] Gaurav-Arora.com

  3. Pingback: Learning The S.O.L.I.D Programming Principles: Overview [Part - I] Gaurav-Arora.com

Leave A Reply