Roadmap › 1. .NET › Step 11
SO
SOLID Principles with Examples
The five SOLID principles with bad-vs-good C# code, plus DRY, KISS, YAGNI, dependency injection and how SOLID shows up in ASP.NET Core.
What it is & where it is used
SOLID is five object-oriented design principles that make code easier to change, test and extend. The acronym was coined by Michael Feathers around 2004 for principles collected by Robert C. Martin ("Uncle Bob") in the late 1990s and early 2000s.
| Letter | Principle | One-line meaning |
|---|---|---|
| S | Single Responsibility | A class should have one reason to change |
| O | Open/Closed | Open for extension, closed for modification |
| L | Liskov Substitution | Subtypes must be usable wherever the base type is expected |
| I | Interface Segregation | Many small, specific interfaces beat one fat interface |
| D | Dependency Inversion | Depend on abstractions, not concrete classes |
Interviewers almost always ask you to explain each with an example and then show where you applied it.
Versions & history
| Year | Milestone |
|---|---|
| 1987 | Barbara Liskov presents the substitution idea (keynote "Data Abstraction and Hierarchy"); formalised with Jeannette Wing in 1994 |
| 1988 | Bertrand Meyer defines the Open/Closed Principle in Object-Oriented Software Construction |
| 1996–2000 | Robert C. Martin publishes articles on OCP, LSP, DIP, ISP; paper "Design Principles and Design Patterns" (2000) |
| ~2004 | Michael Feathers arranges them into the acronym SOLID |
| 2002 / 2006 | Agile Software Development: Principles, Patterns, and Practices (and the C# edition, 2006) |
| 2017 | Clean Architecture restates SRP as "a module should be responsible to one actor" |
Lifecycle
How SOLID is applied during development:
- Design — split responsibilities (SRP), define interfaces around what consumers need (ISP, DIP).
- Implement — inject dependencies through constructors; keep classes small.
- Extend — add behaviour by adding new classes (OCP) rather than editing switches.
- Test — mock the abstractions; unit-test each class in isolation.
- Review — code review checks for god classes,
newof services inside business logic, and subclasses that throwNotSupportedException(LSP smell). - Refactor — when a class keeps changing for unrelated reasons, split it.
Tools used to develop
- ASP.NET Core DI container (
Microsoft.Extensions.DependencyInjection) — DIP in practice. - Scrutor — assembly scanning and decorators for the built-in container.
- Autofac — advanced DI container.
- xUnit + Moq / NSubstitute / FakeItEasy — mocking abstractions.
- SonarQube / SonarLint, NDepend, Visual Studio code metrics — find large classes, high coupling, complexity.
- ArchUnitNET / NetArchTest — enforce layering rules in tests.