ASP.NET Tutorial: Open-Closed Principle (OCP) in .NET 6 Core
As a new developer, you’re probably want to write code that’s clear, maintainable, and scalable. The “Open-Closed Principle” from the SOLID principles is one of the guiding principles in this journey. In this article, we’ll break down the Open-Closed Principle and illustrate it in a simple.NET 6 Core example for beginners.
What exactly is the Open-Closed Principle?
The second letter in SOLID is OCP, which urges you to create software components that are open for extension but closed for modification. Simply put, your code should be adaptable to new features or requirements without requiring changes to the existing codebase. You can add new features without changing the old, working code.
Assume you have a toolbox full of various tools. When you need a new tool, you don’t change the ones you already have; instead, you add a new tool to your collection. This is the heart of the Open-Closed Principle: your code, like your toolkit, should be extensible.
What is the significance of OCP?
It decreases the danger of creating issues when adding new functionality. Your existing, functioning code is unaffected.
Scalability means that your codebase can expand without concern of compromising existing features.
Collaboration: Team members can work together to extend the system without interfering with each other’s code.
A Basic.NET 6 Core Example
To further grasp OCP, let’s look at a simple.NET 6 Core example. We’ll extend the Book and Reservation classes from the last article on the Single Responsibility Principle (SRP).
Book Class Original
The original Book class, which represents book data, is as follows:
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;
}
}
Reservation Class Expansion
Let’s now extend the Reservation class without changing the existing code. We’ll add a new feature to calculate a library member discount:
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;
}
public decimal CalculateLateFee()
{
// Calculate late fee logic here
// For example, you could calculate the late fee based on the due date.
TimeSpan overdue = DateTime.Now - DueDate;
if (overdue.TotalDays > 0)
{
decimal lateFee = overdue.TotalDays * 2.0M; // $2 per day late fee
return lateFee;
}
return 0; // No late fee if not overdue
}
public decimal CalculateMemberDiscount()
{
// Calculate the member discount logic here
// For example, library members get a 10% discount on late fees.
decimal lateFee = CalculateLateFee();
return lateFee * 0.10M;
}
}
We enhanced the Reservation class in this example by adding a new method, CalculateMemberDiscount, to calculate a discount for library members. This was accomplished without altering existing code, such as the Book or CalculateLateFee methods.
This follows the Open-Closed Principle because we enhanced the functionality of the class without modifying its original code. Our code is open to expansion but closed to modification, similar to adding a new tool to your toolbox without affecting the old ones.
Conclusion
The Open-Closed Principle encourages you to write code that may be modified to include new features without affecting the existing, functional code. This principle is critical for software project maintenance, scaling, and collaboration.
ASP.NET Core 8 Hosting Recommendation
HostForLIFE.eu
HostForLIFE.eu is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.