31st Mar 2020

SOLID Principles in C#

.NET

Written By, Uditsing Khati

Overview

SOLID Principles is a coding standard that all developers should have a clear concept for writing code in a proper way and avoid a bad coding architecture. These are the five pillars of coding guidelines of programming languages. It was promoted by Robert C Martin and is used across the object-oriented design models.

dark-mode-app

Why are SOLID principles required?

When a developer develops software with poor coding structure, the code can become inflexible and more brittle, small changes in the code can result in bugs. To overcome these, we should follow SOLID Principles.

S – Single responsibility principle

A class should have one and only one job. Single responsibility principle, so you will have many small classes each of them doing only one thing or task.

Advantages:

  • Testing – A class with one responsibility will have far fewer test cases for this class.
  • Lower coupling – Less functionality in a single class will have fewer dependencies.
  • Organization – Smaller, well-organized classes are easier to search than monolithic ones.

Now we’ll take an example to understand this principle.

Here, the Music class takes 2 responsibilities, one is to take responsibility for playing music and another one is to pause music.

So according to SRP, one class should take one responsibility so we should write one different class for playing music so that any change in playing music should not affect the music class.

O – Open closed principle

Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. This principle suggests that the class should be easily extended but there is no need to change its core implementations.

We can make sure that our code is compliant with the open/closed principle by utilizing inheritance and/or implementing interfaces that enable classes to polymorphically substitute for each other.

Example

Here we have written too many ‘if‘ clauses and if we want to introduce another new action like ‘repeat’, then you need to write another ‘if‘. This class should be open for extension but closed for modification. But how to do that?

So if you want to introduce a new action, then just inherit from Music. So Music is open for extension but closed for modification.

L – Liskov substitution principle

Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it. To put it more simply, if class A is a subtype of class B, then we should be able to replace B with A without disrupting the behavior of our program.

Example

Let’s refactor and make it comply with LSP by having a generic base class for both Banana and Watermelon.

I – Interface segregation principle

A client should never be forced to implement an interface that it doesn’t use or clients shouldn’t be forced to depend on methods they do not use.

Example:

For example we have one DB to store all car details.

And all types of car classes will inherit this interface for saving data. Right now this is good for you, right? Now suppose you are asked to fetch the details of a car by using the car name.
What you will do in that case is just add one method to this interface, right?

Here we only want to show the car details from DB. So, the solution based on the interface segregation principle is to give this responsibility to another interface.

Now, we use IAddCarOperation interface to add car details and the IGetCarOperation interface used to get a car’s details.

D – Dependency Inversion principle

Entities must depend on abstractions, not on concretions. It states that the high-level module must not depend on the low-level module, but it should depend on abstractions.

We say in simple word dependency injection is used simply by ‘injecting’ any dependencies of a class through the class constructor as an input parameter.

Now here we can create the LogManager instance from within the Car class.
This is a violation of the dependency inversion principle.
If we wanted to use a different kind of logger, we would have to modify the Car class.

Let’s see how it can be fixed by using dependency injection.

Conclusion

SOLID principles are the most important set of principles that we should learn and implement as programmers. By using these, we can reduce complexity, execution time and can complete our work with fewer exceptions, in very less time and an understandable manner. I must say you should also follow these principles while coding.

Written By,

.Net developer at Yudiz Solutions Pvt. Ltd