Liskov Substitution Principle (LSP) in .NET 6 Core

Introduction

As a burgeoning developer delving into the SOLID principles, understanding the Liskov Substitution Principle (LSP) is pivotal for crafting robust and maintainable code. In this article, we'll demystify LSP and illustrate its application in .NET 6 Core through a familiar context—a chain of classes. We'll employ a comparable example to the ones used in our previous discussions on the Single Responsibility Principle (SRP) and the Open-Closed Principle (OCP).

What is the Liskov Substitution Principle (LSP)?

The Liskov Substitution Principle, the "L" in SOLID, asserts that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. In simpler terms, if a class S is a subclass of class T, an object of type T should be replaceable with an object of type S without altering the desirable properties of the program.

Think of a chain where each link should seamlessly fit into the next one. Similarly, in software development, subclasses should seamlessly fit into the structure established by their superclasses.

Why is LSP Important?

  1. Maintainability: Code remains maintainable as new classes can be added without impacting existing functionality.
  2. Flexibility: Subclasses can be extended or modified without affecting the correctness of the program.
  3. Collaboration: Multiple developers can work on different parts of the system with confidence that their contributions won't disrupt existing behavior.

A .NET 6 Core Example Using a Chain of Classes

Let's extend our example by introducing a new class, EBook, which is a subclass of our existing Book class. The challenge is to ensure that this new class seamlessly integrates with the existing chain, following the Liskov Substitution Principle.

Reservation classes used in our previous article on the Single Responsibility Principle (SRP) and Open-Closed Principle (OCP).

 If you haven't read it yet, you can check it out 

Existing Book Class

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    public string ISBN { get; set; }

    public Book(string title, string author, string isbn)
    {
        Title = title;
        Author = author;
        ISBN = isbn;
    }
}

Extended EBook Class

public class EBook : Book
{
    public string Format { get; set; }

    public EBook(string title, string author, string isbn, string format) : base(title, author, isbn)
    {
        Format = format;
    }
}

Applying LSP in a Chain of Classes

Now, let's ensure that our new EBook class seamlessly integrates into the existing chain. We'll use it in conjunction with our Reservation class, as we did with the Book class.

public class Reservation
{
    public Book Book { get; set; }
    public string User { get; set; }
    public DateTime DueDate { get; set; }

    public Reservation(Book book, string user, DateTime dueDate)
    {
        Book = book;
        User = user;
        DueDate = dueDate;
    }

    // ... existing methods

    public void DisplayBookDetails()
    {
        Console.WriteLine($"Title: {Book.Title}, Author: {Book.Author}, ISBN: {Book.ISBN}");

        if (Book is EBook eBook)
        {
            Console.WriteLine($"Format: {eBook.Format}");
        }
    }
}

In this example, we've extended our chain of classes by introducing the EBook subclass. Notice how the Reservation class effortlessly works with both Book and EBook without compromising the correctness of the program. The DisplayBookDetails method showcases the Liskov Substitution Principle in action.

Conclusion

Understanding the Liskov Substitution Principle is paramount for building flexible and extensible software. In our .NET 6 Core example, we've seamlessly integrated a new class into an existing chain, demonstrating the power of LSP. As you continue your coding journey, embrace these principles to create software that evolves gracefully and stands the test of time.

Explore the Single Responsibility Principle (SRP) in .NET Core through this informative article.  Read the full article on the Single Responsibility Principle (SRP) in .NET Core

Explore the Open-Closed Principle (OCP) in .NET Core through this informative article.  Read the full article on Open-Closed Principle (OCP) in .NET 6 Core

😊Please consider liking and following me for more articles and if you find this content helpful.👍


Citiustech Healthcare Technology Pvt Ltd
CitiusTech plays a deep and meaningful role in powering the future of healthcare.