SOLID principles
SOLID is five design principles (Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion) that let new features arrive as new code instead of edits to tested code. Here they are applied step by step to one checkout example.
20.1 The problem: one class, every job
new Checkout().Pay("upi", 499);new Checkout().Pay("netbanking", 899); class Checkout{ public void Pay(string type, decimal amount) { if (type == "upi") Console.WriteLine($"UPI: {amount}"); else if (type == "card") Console.WriteLine($"Card: {amount}"); else throw new NotSupportedException(type); Console.WriteLine("SMS sent"); }}$ dotnet run Checkout.csUPI: 499SMS sentUnhandled exception.System.NotSupportedException: netbanking at Checkout.Pay(String type, ...)- Checkout does three jobs: choose the type, make the payment, send the SMS, so it has many reasons to change.
- Every new payment method means opening this tested class and adding another else if.
- One mistake there breaks the whole checkout. Goal: new feature = new code.
20.2 S: Single Responsibility
- A class should have only one reason to change.
- The SMS job moves into SmsNotifier; Checkout receives it through the constructor.
- Changing the SMS format no longer touches Checkout.
new Checkout(new SmsNotifier()).Pay("upi", 499); class Checkout(SmsNotifier sms){ public void Pay(string type, decimal amount) { Console.WriteLine($"{type}: {amount}"); sms.Send(amount); }}class SmsNotifier{ public void Send(decimal amount) => Console.WriteLine($"SMS: {amount} paid");}$ dotnet run Checkout.csupi: 499SMS: 499 paid20.3 O: Open/Closed
- Open for extension, closed for modification.
- IPaymentMethod has only Pay; Checkout takes an IPaymentMethod instead of a string.
- Checkout calls method.Pay(amount) without knowing UPI or card: the if/else chain is gone.
interface IPaymentMethod{ void Pay(decimal amount);} class Checkout(SmsNotifier sms){ public void Pay(IPaymentMethod method, decimal amount) { method.Pay(amount); sms.Send(amount); }}- Adding NetBanking = one new class; not a single line of Checkout changes.
var checkout = new Checkout(new SmsNotifier());checkout.Pay(new UpiPayment(), 499);checkout.Pay(new NetBankingPayment(), 899); class UpiPayment : IPaymentMethod{ public void Pay(decimal amount) => Console.WriteLine($"UPI: {amount}");}class NetBankingPayment : IPaymentMethod{ public void Pay(decimal amount) => Console.WriteLine($"NetBanking: {amount}");}$ dotnet run Checkout.csUPI: 499SMS: 499 paidNetBanking: 899SMS: 899 paid- Step 1
1.Start: Checkout.Pay(string type) with if/else + SMS
- Step 2
2.S: move SMS into SmsNotifier
- Step 3
3.O: IPaymentMethod, one class per payment method
- Step 4
4.L + I: Refund only in a separate IRefundable
- Step 5
5.D: Checkout takes INotifier, chosen outside
- Step 6
6.New feature = new class, Checkout untouched
20.4 L: Liskov Substitution (the refund trap)
- New requirement: refunds, so Refund is added to IPaymentMethod.
- CodPayment (Cash on Delivery) cannot refund online, so it throws.
- Any implementation put in place of the interface must work without surprises; CodPayment breaks the promise.
IPaymentMethod[] payments = [new UpiPayment(), new CodPayment()];foreach (var p in payments) p.Refund(499); class CodPayment : IPaymentMethod{ public void Pay(decimal amount) => Console.WriteLine($"COD: {amount}"); public void Refund(decimal amount) => throw new NotSupportedException( "No refund for COD");}$ dotnet run Refund.csUPI refund: 499Unhandled exception.System.NotSupportedException: No refund for COD at CodPayment.Refund(Decimal amount)20.5 I: Interface Segregation
- Do not force a class to implement methods it does not use.
- Move Refund into a small IRefundable; CodPayment implements only IPaymentMethod.
- Putting COD into a refund list is now a compile error (CS0029): the bug is caught before running.
interface IPaymentMethod{ void Pay(decimal amount);}interface IRefundable{ void Refund(decimal amount);}class CodPayment : IPaymentMethod{ public void Pay(decimal amount) => Console.WriteLine($"COD: {amount}");}// UpiPayment : IPaymentMethod, IRefundable20.6 D: Dependency Inversion
- Checkout still depends on the concrete SmsNotifier, and the business now wants WhatsApp.
- INotifier interface; Checkout takes an INotifier.
- Which notifier is used is decided outside (top level); Checkout does not change.
INotifier notifier = new WhatsAppNotifier();var checkout = new Checkout(notifier);checkout.Pay(new UpiPayment(), 499); interface INotifier { void Send(decimal amount); } class Checkout(INotifier notifier){ public void Pay(IPaymentMethod method, decimal amount) { method.Pay(amount); notifier.Send(amount); }}$ dotnet run Checkout.csUPI: 499WhatsApp: 499 paid- Step 1
1.High level: Checkout holds the business rule
- Step 2
2.Abstractions: IPaymentMethod, INotifier
- Step 3
3.Details: UpiPayment, WhatsAppNotifier implement them
- Step 4
4.Top level creates objects and passes them into the constructor (DI)
Both sides depend on interfaces; ASP.NET Core's DI container does the wiring automatically
20.7 Summary: the five letters
| Letter | Principle | In the checkout example |
|---|---|---|
| S | Single Responsibility: one reason to change | SMS moved to SmsNotifier |
| O | Open/Closed: extend without modifying | NetBanking = new class implementing IPaymentMethod |
| L | Liskov Substitution: implementations keep the contract | CodPayment throwing on Refund breaks it |
| I | Interface Segregation: small, focused interfaces | Refund moved to IRefundable |
| D | Dependency Inversion: depend on abstractions | Checkout takes INotifier via the constructor |
20.8 Common mistakes
- Thinking SOLID means an interface for every class and a class for every method: S means one reason to change, not one method.
- Adding an interface for a class with only one implementation and no testing need: over-engineering.
- Treating SOLID as a rule book: apply it when a real change or testing need arrives.
- Implementations throwing NotSupportedException: a sign Liskov (and usually ISP) is broken.
20.9 5 practical SOLID rules
- Two reasons to change? Split the class.
- if/else on a type? Think interface + new class.
- NotSupportedException in an implementation = L broken.
- Keep interfaces small and focused.
- Take dependencies through the constructor; create objects only at the top level.
20.10 Interview tips
- Always answer SOLID with one example (payment checkout), letter by letter.
- S: one reason to change; O: a new payment method is a new class, Checkout is not edited.
- L: any implementation can replace the interface without surprises; I: small interfaces like IRefundable so COD writes no fake Refund.
- D: high-level code depends on interfaces and gets dependencies through the constructor, which keeps it testable (ASP.NET Core DI).