Click here to Skip to main content
13,139,569 members (54,246 online)
Click here to Skip to main content
Add your own
alternative version

Stats

6.9K views
7 bookmarked
Posted 16 Oct 2014

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

, 16 Oct 2014
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Gaurav Aroraa
Architect
India India
There is no age limit for learning and its never late, to start.

You may also be interested in...

Comments and Discussions

 
GeneralMy Vote 5 Pin
Shemeemsha29-Oct-14 20:12
memberShemeemsha29-Oct-14 20:12 
GeneralRe: My Vote 5 Pin
Gaurav.Kumar.Arora29-Oct-14 21:09
professionalGaurav.Kumar.Arora29-Oct-14 21:09 
Suggestion?? Pin
ArchAngel12328-Oct-14 12:26
memberArchAngel12328-Oct-14 12:26 
AnswerRe: ?? Pin
Gaurav.Kumar.Arora28-Oct-14 20:55
professionalGaurav.Kumar.Arora28-Oct-14 20:55 
GeneralRe: ?? Pin
ArchAngel12329-Oct-14 6:14
memberArchAngel12329-Oct-14 6:14 
GeneralRe: ?? Pin
Gaurav.Kumar.Arora29-Oct-14 18:55
professionalGaurav.Kumar.Arora29-Oct-14 18:55 
GeneralFollowing blog post Pin
Shuby Arora27-Oct-14 11:23
memberShuby Arora27-Oct-14 11:23 
AnswerRe: Following blog post Pin
Gaurav.Kumar.Arora27-Oct-14 11:28
memberGaurav.Kumar.Arora27-Oct-14 11:28 
AnswerRe: Following blog post Pin
Shuby Arora27-Oct-14 11:35
memberShuby 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 17 Oct 2014
Article Copyright 2014 by Gaurav Aroraa
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid