Solid Principles in .NET C#

Maulik Patel
5 min readJan 5, 2023

--

This article will cover Solid principles in detail with example.

  1. S — Single Responsibility :

It states class should have single responsibility. It holds methods and property of related functionality in single class. It would be easy for developer to understand and implement functionality.

If there change in logging requirement then class implementing logging functionality will have changes but that class will never have change for security functionality (Token service).

One class should not have more than one functionality. We should have separate class for Logging, Caching and Token services functionality.

Let’s see below screen shot for TokenService class which only have functionality related token generate and validate.

2. O — Open-closed Principle:

It states class should open for extension but close for modification. We have already developed and live functionality then we should avoid modification of methods unless any bug reported. It is live and testing already passed any change might break working functionality. It should extend with new methods.

Let’s take example of Car Services which has different types of cars like sedan, SUV, luxury, hatchback etc. Let’s see violation of open-closed.

Both type cars have different method to return price.

If we have to get price for car then need to pass type and based on type will get price for that type.

Check this function if we have to add new type of car then needs to modify this method. Which violate Open-closed principle, so let’s modify it to make correct.

Add abstract class.

Modify sedan and SUV classes.

Modify getCarPrice method to generic.

Call getPrice method.

In future if we have to add new type Hatchback then add new class which implement CarAbstract class and add new code for calling as above. We don’t need to modify existing SUV, Sedan and getCarPrice method. So it’s open for extension and closed for modification.

3. L — Liskov Substitute:

It says parent class can be substitute with child class without any behavior change. Another word we can replace object of parent class with child class instance without any behavior of base class

we have below implementation which break Liskov.

This break liskov substitute principle as it’s return price for motor cycle but parent class for car.

We have parent object of car which replace by MotorCycle child class which give price of Motorcycle which change behavior of child class.

We have already example ready with open-close principle.

let’s add abstract class.

Motorcycle class will not inherited CarAbstarct class. SuvCar or SedanCar instance replacement will not change behavior. It will return respective car type price.

4. I — Interface Segregation:

It states that client should not be forced to implement interfaces, they don’t use. Interface should have single purpose.

Let see example which break the rule.

We have created interface which has functionality for Invoice and Token. So when we use this interface we forced to implement BuildToken and IsValidToken method which not required for that service.

Here is the solution. It should have separate interface for separate functionality to avoid forced implementation.

5. D — Dependency Inversion:

It states we should not write any tight coupled code because that is very hard to maintain when the application is growing bigger and bigger. Another word high level class should not depend upon a lower level class. They both should depend on abstraction. See below examples where interface and constructor injection used.

Thanks for Reading!

--

--

Maulik Patel

Full stack Software Engineer. C# | .NET core | MSSQL | Azure | Docker | Angular12+