Solids Principles of C# : Basics

15 February 2021 at 10:00 by ParTech Media - Post a comment

Michael Feathers had introduced the acronym SOLID to describe five fundamentals principles of design. In Object-Oriented Programming (OOP), this acronym is used to interpret software design.

It makes the concepts easily understandable and maintainable. It also increases the flexibility & extendibility of software design. Developers often use these principles to omit code smells. These principles also help them to refractor their codes.

Let us dive into the SOLID Principles of C# and understand what it is all about and why should we use it.

Table of Contents

  1. Single Responsibility Principle (SRP)
  2. Open-Closed Principle (OCP)
  3. Liskov Substitution Principle (LSP)
  4. Interface Segregation Principle (ISP)
  5. Dependency Inversion Principle (DIP)
  6. Advantages of SOLID Principles
  7. Wrapping Up

Single Responsibility Principle (SRP)

A class or module must take a single responsibility. The class only has one reason to change by having a single job to do. Hence a class can consist of many elements or methods, but every element of a class must be used for only one process.

In other words, a class should have only one responsibility and there should only be one reason to change the class of the software. Creating such classes makes it robust, easy to test, and easy to upgrade.

Open-Closed Principle (OCP)

Without any modification, developers can extend the behavior of a class. This means we can easily extend a module or class, but we don’t have to change its fundamental implementations.

The code written should be open for extension/enhancements and closed for modifications. The core logic of an already written code should not be modified to accommodate new functionality.

We can implement new features by writing new codes. Here, OCP states that we cannot change the existing code in the process of doing the same.

Liskov Substitution Principle (LSP)

LSP is another crucial principle of SOLID. Suppose a module is utilizing a base class, we can easily exchange the reference of a base class with a derived class. We can achieve so without hampering the module’s functionality.

LSP states - if a class is derived from a base class, then the derived class should be perfectly able to replace the base class. So, when the objects are accessed using the base class, then it will not throw any error during runtime.

We can accurately substitute a child class for their parent class. Liskov Substitution Principle states that if we have derived a class B from class A, then we can also substitute the class B for class A.

Interface Segregation Principle (ISP)

The Interface Segregation Principle suggests we should use various specific client interfaces rather than using a single interface that is generic. Having multiple interfaces for different purposes is more effective than combining many functionalities in one interface.

The main idea of this principle is - The clients should not be forced to implement methods that are not used by them, and the contract should be broken into multiple ones.

This principle is required to solve application design-related problems. If we use a single class to execute all the jobs, it becomes a fat class. It also has an overburden. If we try to inherit the methods, it will share the methods which are irrelevant to the derived classes. So, we need to create separate interfaces.

Dependency Inversion Principle (DIP)

The Dependency Inversion Principle states that modules need to depend on abstraction rather than depending on the execution. These abstractions must be independent of details.

It is always a best practice to write loosely coupled code. The higher class should not be dependent on the lower classes. The reason, the two classes, or at least the lower class, in this case, need not communicate the changes that happen inside the class to the higher class.

DIP says that there can’t be a coupling done tightly amongst software components. We often use DI (Dependency Injection) and IoC (Inversion of control) to describe similar design patterns.

We use the Inversion of Control technique in C# to implement DIP. We can easily implement it using an interface or abstract class. Here the entities of lower levels are combined to a single interface. The entities of higher levels are only permitted to use the entities that are currently being implemented in the interface.

Advantages of SOLID Principles

  • Software maintenance is crucial for companies nowadays. With the increasing count of customers, organizations need to upgrade and enhance their software. Designing software with the help of SOLID principles helps them to achieve it.
  • TDD (Test Driven Development) is a key factor during the design and development of large-scale software. To help an organization for testing the individual functionalities, we should use SOLID principles.
  • Enterprise Applications must be flexible and extendable. Implementing SOLID principles helps us achieve this goal.
  • For large scale applications, we need to design them in such a way that the development team can work on different modules at the same time. So parallel application development is crucial for an organization. Concepts of Solid Programming helps us to achieve this goal.

Wrapping Up

Five fundamental SOLID principles are crucial for Object-Oriented software designs. The majority of the SOLID principles suggest adding covers of abstraction between different classes. The goal is to make classes independent of each other. It also creates a loose couple that decreases the rigidity of the code. The code also becomes less fragile. Developers need to keep these principles in their mind during software design & development.