SOLID Principles: Five Guidelines for Building Maintainable Software
Design Patterns Explained: From Theory to Practice: Article 1
Software development does not end when an application is released. Requirements change, new features are introduced, integrations are added, and business rules evolve.
The challenge is making those changes without breaking existing functionality or making the code increasingly difficult to maintain.
As applications grow, classes can accumulate unrelated responsibilities, dependencies become tightly coupled, and small changes can have unintended consequences.
The SOLID principles offer five guidelines for managing these design challenges. They help developers organise responsibilities, design useful abstractions and manage dependencies in object-oriented software.
To make the discussion practical, we'll use a hypothetical ticket-management application built with C# and .NET throughout the article.
What are the SOLID principles?
SOLID is an acronym representing five object-oriented design principles:
- S - Single Responsibility Principle
- O - Open/Closed Principle
- L - Liskov Substitution Principle
- I - Interface Segregation Principle
- D - Dependency Inversion Principle
Each principle addresses a different aspect of software design, but they reinforce one another.
They are not design patterns themselves. Rather, they provide guidance for recognising and addressing common design problems. Understanding them gives us a useful foundation for the design patterns explored later in this series.
1. Single Responsibility Principle (SRP)
A class should have only one reason to change.
The Single Responsibility Principle encourages us to give a class one clearly defined responsibility and encapsulate that responsibility within the class.
Consider a TicketService in our ticket-management application. As the application grows, the service might become responsible for:
- Creating and updating tickets.
- Saving tickets to a database.
- Sending email notifications.
- Generating ticket reports.
- Formatting report output.
Initially, putting everything in one class might seem convenient. The problem becomes apparent when different requirements start affecting the same class.
For example, a change to the email notification format should not require modifying the class responsible for managing ticket workflows. Similarly, changing the database implementation should not affect report formatting.
These are separate reasons for change.
A better design would separate ticket management, persistence, notifications and reporting into components with clearly defined responsibilities. The TicketService could coordinate the ticket workflow while collaborating with other components for the remaining tasks.
Why does this matter?
A class with too many responsibilities becomes increasingly difficult to understand and maintain.
A change to one area can accidentally affect another, and testing the class often requires setting up dependencies that are unrelated to the behaviour being tested.
Separating responsibilities makes it easier to reason about individual components and change them independently.
A common misunderstanding
SRP does not mean every class should contain only one method, or that every small operation deserves its own class.
The goal is not to maximise the number of classes. It is to group behaviour that belongs together and separate behaviour that changes for different reasons.
A class with several closely related methods can be perfectly reasonable.
The question to ask: Are these responsibilities likely to change independently?
2. Open/Closed Principle (OCP)
Software entities should be open for extension but closed for modification.
The Open/Closed Principle encourages us to introduce new behaviour without repeatedly modifying existing, stable code.
Imagine that our ticket-management application supports different Service Level Agreements (SLAs). Customers on different plans have different response-time targets.
An initial implementation might contain conditional logic inside TicketService to determine the applicable response target:
- Standard customers receive one response target.
- Premium customers receive another.
- Enterprise customers receive a third.
As the application grows, every new customer plan requires another change to the service.
One alternative is to introduce an abstraction representing an SLA policy:
public interface ISlaPolicy
{
TimeSpan GetResponseTarget(Ticket ticket);
}
Different policies can implement this contract. For example, a standard policy and a premium policy can provide their own response targets.
The ticket workflow can work with the abstraction rather than knowing the details of every policy.
When a new policy is introduced, we add another implementation instead of continually expanding the existing service's conditional logic.
Why does this matter?
As applications evolve, developers need to introduce new functionality while keeping existing functionality stable.
Reducing the changes required in established components can lower the risk of introducing regressions.
It can also make the system easier to extend because new behaviour can be introduced in a focused part of the application.
A common misunderstanding
The Open/Closed Principle does not mean existing code must never be modified.
Bugs still need to be fixed, and requirements sometimes genuinely require changes to existing behaviour. Nor should developers create abstractions for every feature they might possibly need in the future.
The principle is most useful when there is a genuine variation point: an area of the application where different behaviours are expected or where new requirements are reasonably likely.
The question to ask: Can I introduce this new behaviour without unnecessarily changing the existing workflow?
3. Liskov Substitution Principle (LSP)
Objects of a subtype should be usable wherever objects of their base type are expected, without breaking the behaviour that clients rely on.
The Liskov Substitution Principle focuses on the behaviour of related types.
Inheritance allows one class to extend another. However, simply inheriting from a base class does not guarantee that the derived class can be used safely in its place.
Consider a ticket-exporting component in our application.
Suppose the application defines a contract that promises to export any valid ticket. A CSV implementation should therefore be able to export open, in-progress and resolved tickets.
Now imagine that the implementation rejects resolved tickets:
public interface ITicketExporter
{
byte[] Export(Ticket ticket);
}
public sealed class CsvTicketExporter : ITicketExporter
{
public byte[] Export(Ticket ticket)
{
if (ticket.Status == TicketStatus.Resolved)
{
throw new InvalidOperationException(
"Resolved tickets cannot be exported.");
}
// Export the ticket.
return [];
}
}
The method signature is correct. The implementation satisfies the compiler's type requirements.
However, it has introduced an additional restriction that the original contract did not promise. A client expecting to export any valid ticket may now fail when it receives this implementation.
This is a behavioural compatibility problem.
If exporting only open tickets is genuinely required, that should be represented by a more specific contract. An implementation should not claim to fulfil a broader contract while silently supporting fewer cases.
Why does this matter?
Substitution allows software to work with different implementations without needing to understand their internal details.
When implementations behave inconsistently, client code must introduce special checks and exceptions for individual types. That weakens the benefits of abstraction and makes the system harder to extend safely.
A common misunderstanding
LSP is not simply about whether a class compiles or whether its methods have matching signatures.
C# enforces certain type-level constraints, but the compiler cannot automatically guarantee that an implementation honours every behavioural expectation.
Developers must also consider valid inputs, expected outputs, exceptions and the conditions clients rely on. Tests can help verify these behavioural contracts.
The question to ask: Can a client use this implementation wherever it expects the original abstraction, without special-case logic?
4. Interface Segregation Principle (ISP)
Clients should not be forced to depend on methods they do not use.
The Interface Segregation Principle encourages developers to create focused interfaces rather than large interfaces that attempt to support every possible operation.
Imagine that our application exposes a single ITicketManagement interface containing methods for:
- Reading tickets.
- Creating and updating tickets.
- Assigning tickets.
- Deleting tickets.
- Generating reports.
A component that only needs to display tickets now depends on an interface containing many operations it never uses.
This creates unnecessary coupling. Changes to unrelated methods in the large interface can affect implementations and clients that do not need those methods.
Instead, the interface can be divided into smaller, more focused contracts:
public interface ITicketReader
{
Task<Ticket?> GetByIdAsync(
Guid id,
CancellationToken cancellationToken);
}
public interface ITicketWriter
{
Task SaveAsync(
Ticket ticket,
CancellationToken cancellationToken);
}
public interface ITicketAssigner
{
Task AssignAsync(
Guid ticketId,
Guid agentId,
CancellationToken cancellationToken);
}
A reporting component can depend on ITicketReader, while a component responsible for assigning tickets can depend on ITicketAssigner.
A class may implement several interfaces when it genuinely supports those responsibilities. The important point is that each client only needs to depend on the capabilities it actually uses.
Why does this matter?
Smaller, focused interfaces make dependencies clearer. They also reduce the amount of behaviour that a class must implement merely to satisfy a contract.
This is particularly useful when working with systems that integrate with different services or providers, where not every implementation supports the same capabilities.
A common misunderstanding
Interface Segregation does not mean creating an interface for every method.
Too many small interfaces can introduce unnecessary complexity and make it harder to understand the overall design.
An interface should represent a meaningful capability or contract, not simply exist to reduce the number of methods in another interface.
The question to ask: Does this client genuinely need every operation exposed by this interface?
5. Dependency Inversion Principle (DIP)
High-level classes should not depend on low-level classes. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.
The Dependency Inversion Principle concerns the direction of dependencies in an application.
Consider a TicketService responsible for resolving tickets. It needs to retrieve a ticket, update its status and persist the change.
One approach is for the service to depend directly on a specific database implementation.
The problem is that business logic now knows about a lower-level technical detail: how data is stored.
If the application changes its persistence technology, the business logic may also need to change.
DIP encourages us to introduce an abstraction that describes what the business logic needs, rather than making it depend on a particular implementation.
For example, the application could define an ITicketRepository interface:
public interface ITicketRepository
{
Task<Ticket?> FindByIdAsync(
Guid id,
CancellationToken cancellationToken);
Task SaveAsync(
Ticket ticket,
CancellationToken cancellationToken);
}
The TicketService can depend on that interface:
public sealed class TicketService(
ITicketRepository ticketRepository)
{
public async Task ResolveAsync(
Guid ticketId,
CancellationToken cancellationToken)
{
var ticket = await ticketRepository.FindByIdAsync(
ticketId,
cancellationToken);
if (ticket is null)
{
throw new KeyNotFoundException("Ticket not found.");
}
ticket.Resolve();
await ticketRepository.SaveAsync(
ticket,
cancellationToken);
}
}
An infrastructure component provides the implementation using Entity Framework Core and the chosen database.
The dependency now points towards the abstraction rather than directly towards the infrastructure implementation.
How this works in .NET
The implementation can be registered with the built-in dependency injection container:
builder.Services.AddScoped<
ITicketRepository,
EfTicketRepository>();
When the application creates TicketService, the dependency injection container supplies the registered repository implementation.
The service does not need to know how that implementation is created.
Why does this matter?
High-level business logic becomes less dependent on implementation details.
This makes it easier to replace infrastructure components, isolate business logic during testing and maintain a clearer separation between application and infrastructure concerns.
Dependency Inversion vs Dependency Injection
These terms are closely related, but they are not interchangeable.
Dependency Inversion is a design principle. It describes how dependencies should be structured.
Dependency Injection is a technique for providing a class with the dependencies it needs, rather than having that class construct them directly.
In .NET, constructor injection and the built-in dependency injection container are common ways to implement this approach.
Dependency injection can help achieve dependency inversion, but simply registering every class in a DI container does not automatically produce a well-designed architecture.
The question to ask: Does my business logic depend on an abstraction, or does it know too much about the implementation details?
How the five principles work together
Although each principle addresses a different problem, they reinforce one another.
Consider our ticket-management application:
- SRP helps prevent the ticket workflow from becoming responsible for notifications, persistence and reporting.
- OCP helps introduce new SLA policies without repeatedly modifying the existing workflow.
- LSP ensures that different implementations honour the behavioural contracts their clients rely on.
- ISP ensures that components depend only on the capabilities they actually need.
- DIP ensures that important business logic is not tightly coupled to infrastructure details.
Together, these ideas encourage focused responsibilities, clear contracts and manageable dependencies.
They are not five independent rules to memorise and apply mechanically. They are different ways of thinking about the same broader challenge: how to keep software understandable as it changes.
When should you apply SOLID?
SOLID is useful, but applying every principle mechanically can be counterproductive.
For a small application or a straightforward feature, a simple implementation may be the most maintainable choice.
Introducing several interfaces, abstractions and additional classes before there is a real need for them can make the software harder to understand.
A practical approach is to start with a clear implementation, pay attention to where changes repeatedly cause problems, and introduce abstractions when they solve an actual design issue.
During code reviews, consider asking:
- Does this class have unrelated reasons to change?
- Does adding new behaviour require modifying several established components?
- Can implementations be substituted without changing client expectations?
- Are clients depending on operations they never use?
- Does business logic depend directly on infrastructure details?
These questions are often more valuable than simply asking whether a class follows SOLID.
Conclusion
The SOLID principles provide a foundation for thinking about maintainable object-oriented software.
They encourage developers to manage responsibilities, design useful abstractions and reduce unnecessary coupling.
However, good software design is not about applying as many principles or patterns as possible. It is about making deliberate decisions that keep the system understandable as it evolves.
Understanding SOLID also makes it easier to understand why design patterns exist. Many patterns provide established ways of addressing the kinds of design challenges these principles help us recognise.
In the next article, we'll begin exploring the creational design patterns, starting with the Factory Method pattern.
The goal of this series is not just to learn the names and definitions of design patterns, but to understand the problems they solve, how they work, and when their added complexity is justified.
Further reading
- Dependency injection in .NET — Microsoft Learn
- Dependency injection guidelines — Microsoft Learn
- Architectural principles — Microsoft Learn
- Dive Into Design Patterns — Alexander Shvets
- Design Patterns in C#, 2nd Edition — Vaskaran Sarcar
Comments
Join the discussionNo comments yet. Be the first to comment.