SOLID Design Principles C#

SOLID  design principles are the most basic yet most important thumb rules to follow during designing a software and it’s classes.

SOLID is an acronym for the first 5 basic Object Oriented Design principles, namley:

  1.  S  stands for Single Responsibility Principle (SRP)
  2.  O stands for Open Closed Principle (OCP)
  3.  L  stands for Liskov Substitution Principle (LSP)
  4.  I  stands for Interface Segregation Principle (ISP)
  5.  D stands for Dependency Injection Principle (DIP)

Now let’s try to look at each principle in detail with the help of examples.

 1. Single Responsibility Principle

SRP is defined as : ‘A class should have only one reason to change‘.

What it essentially means, a class should be designed with a single objective thus making it concise and easy to maintain.

Lets’s look at an example of bad design:

Here, including Customer Name and Address in the Order class is an example of bad design. Reason: Suppose if we want to store the Customer’s Phone number then? and if same customer has more than one order information will be redundant.

Splitting above class on Order and Customer will make the design more cleaner:

Classic example of SRP are : Logger class handling only logging and nothing else.

2. Open Closed Principle

This principle states : states “classes should be open for extension, but closed for modification“.

This means such class can allow its behavior to be extended without modifying the source code.  Open for extension implies its behavior can be extended using inheritance. In this solution, you inherit a class and all its behavior then override methods to change and also the source code of the original class remains same.

Let’s look at a simple example :

In the above example, we inherited Employee class to extend its behavior for the Contract Employee without modifying the source code of the Employee class.

3. Liskov Substitution Principle

This states:  “if S is a subtype of T, then objects of type T may be replaced with objects of type S

This principle is is a way of ensuring that inheritance is used correctly. It emphasizes methods that use base classes must be able to use objects of derived classes without knowing it.

So, derived types must be completely substitutable for the base types. If Manager is derived from the Employee class, then Manager class can be substituted for the Employee class without any error otherwise it’s a violation Liskov’s substitution principles.

In the below example ,we can see the violation of the Liskov’s substitution principle:

Solution to this problem can be removing the methods that cannot be implemented by the derived classes.

Now only birds which can fly can derive from the BirdFlight class.

4. Interface Segregation

This principle states “that clients should not be forced to implement interfaces they don’t use”. So instead of using one fat interface, it is better to use several interfaces having a small group of relevant methods only.

In the above example, all the employees have a salary, but only Leads and Managers have subordinates.

So, we can split the interface into two to get it fixed.

5. Dependency Inversion Principle (DIP)

This principle is about dependencies among components. The definition of DIP is given by Robert C. Martin is as follows:

  1. High-level modules should not depend on low-level modules. Both should depend on abstractions.
  2. Abstractions should not depend on details. Details should depend on abstraction.

The principle says that there should not be a tight coupling among components of software and to avoid that, the components should depend on abstraction. Dependency Injection (DI) and Inversion of Control (IoC) are the implementation of this principle.

Let’s look at the scenario where we need data for our application, which we can fetch from Database or DataService.

Above mentioned interface helps to provide abstraction of the actual source from which data is supplied. Now, we can use Dependency Injection to inject datastore in the class utilizing the data.

That’s all folks for the SOLID.