Skip to content

SOLID Principles

Single Responsibility Principle

  • A class has a single responsibility
  • Avoids bugs
  • Avoids merge conflict

Open Close Principle

  • Open to extension, closed for modification
  • Approaches
  • Decorator Pattern
    • Instead of modifying an implementation, use polymorphism for a custom implementation
    • Dependency injection at run time
  • Extension Methods

Liskov Substation Principal

  • A child class can do everything a parent class can
  • Wrong:
    • Adult -> Child class
  • Correct:
    • Human -> Adult
    • Human -> Child

Interface Segregation Principle

  • Avoid bloated interfaces
    • Interfaces with methods unused by all implementors
  • Solution make finer grain interfaces

Dependency Inversion

  • High level modules should not depend on low level modules, they should both depend on abstraction
  • Wrong Service -> Repository
  • Correct Service -> IRepository (interface)
    • Inject Repository instance when instantiating Service