Click here to Skip to main content
15,881,768 members
Articles / Desktop Programming / MFC

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
16 Oct 2014CPOL3 min read 12.5K   7   9
In our previous posts we learned ‘What is S.O.L.I.D. Programming Principles and a detailed explanation with code of Single Responsibility Principle, Open/closed Principle, Liskov Substitution Principle and Interface Segregation Principle.

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:

Quote:

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.

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
India India
Learning never ends.

Comments and Discussions

 
GeneralMy Vote 5 Pin
Shemeemsha (ഷെമീംഷ)29-Oct-14 20:12
Shemeemsha (ഷെമീംഷ)29-Oct-14 20:12 
GeneralRe: My Vote 5 Pin
Gaurav Aroraa29-Oct-14 21:09
professionalGaurav Aroraa29-Oct-14 21:09 
Suggestion?? Pin
ArchAngel12328-Oct-14 12:26
ArchAngel12328-Oct-14 12:26 
AnswerRe: ?? Pin
Gaurav Aroraa28-Oct-14 20:55
professionalGaurav Aroraa28-Oct-14 20:55 
GeneralRe: ?? Pin
ArchAngel12329-Oct-14 6:14
ArchAngel12329-Oct-14 6:14 
GeneralRe: ?? Pin
Gaurav Aroraa29-Oct-14 18:55
professionalGaurav Aroraa29-Oct-14 18:55 
GeneralFollowing blog post Pin
Shuby Arora27-Oct-14 11:23
Shuby Arora27-Oct-14 11:23 
AnswerRe: Following blog post Pin
Gaurav Aroraa27-Oct-14 11:28
professionalGaurav Aroraa27-Oct-14 11:28 
AnswerRe: Following blog post Pin
Shuby Arora27-Oct-14 11:35
Shuby Arora27-Oct-14 11:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.