SOLID Design Principles C#

In this article, we will learn SOLID principles in C#:

 

  • What is Single Responsibility Principle (SRP)

  • What is Open Closed Principle (OCP)

  • What is Liskov Substitution Principle (LSP)

  • What is Interface Segregation Principle (ISP)

  • What is Dependency Inversion Principle (DIP)

 

1. What is Single Responsibility Principle (SRP)?

The single responsibility principle states that every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class.

All its services should be narrowly aligned with that responsibility

As per SRP, there should not be more than one reason for a class to change, or a class should always handle single functionality. If you put more than one functionality in one Class in C# it introduce coupling between two functionality and even if you change one functionality there is chance you broke coupled functionality, which require another round of testing to avoid any surprise on production environment. More On CSharp

2. What is Open Closed Principle (OCP)?

In object-oriented programming, the open/closed principle states “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”; that is, such an entity can allow its behaviour to be extended without modifying its source code.

Open Closed principle helps you write code that is extensible and cleaner.

3. What is Liskov substitution principle (LSP) ?

The Liskov substitution principle (LSP) is a particular definition of a subtyping relation, called (strong) behavioral subtyping, that was initially introduced by Barbara Liskov in a 1987 conference keynote address entitled Data abstraction and hierarchy

  • if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program

In order to follow this principle we need to make sure that the subtypes respect the parent class.

4. What is Interface Segregation principle (ISP) ?

The interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use.

  • ISP splits interfaces which are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them.
  • ISP is intended to keep a system decoupled and thus easier to refactor, change, and redeploy.

ISP is one of the five SOLID principles of Object-Oriented Design, similar to the High Cohesion Principle of GRASP

5. What is Dependency Inversion principle (DIP) ?

The Dependency Inversion principle refers to a specific form of decoupling software modules.It states:

  • High-level modules should not depend on low-level modules. Both should depend on abstractions.
  • Abstractions should not depend on details. Details should depend on abstractions.

The Dependency Inversion principle (DIP) helps us to develop loosely couple code by ensuring that high-level modules depend on abstractions rather than concrete implementations of lower-level modules

Thanks for visiting !!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s