SOLID — five design principles for maintainable code.
S — Single Responsibility:
1// Each class has one reason to change2public class UserService { public void CreateUser(User u) { } }3public class EmailService { public void SendEmail(User u) { } }
O — Open/Closed:
1// Open for extension, closed for modification2public abstract class Shape { public abstract double Area(); }3public class Circle : Shape { public override double Area() => ...; }
L — Liskov Substitution:
1// Subtypes must be substitutable for base types2// If S is subtype of T, then objects of T can be3// replaced with objects of S without breaking behavior
I — Interface Segregation:
1// Many specific interfaces > one general interface2public interface IReadable { string Read(); }3public interface IWritable { void Write(string data); }
D — Dependency Inversion:
1// Depend on abstractions, not concretions2public class OrderService3{4 private readonly IOrderRepository _repo; // Abstraction5 public OrderService(IOrderRepository repo) => _repo = repo;6}