C# / .NET Compendium PLEN
.NET / C# / SOLID, Clean Code, DI

SOLID - principles of object-oriented design

SOLID is an acronym for five principles of object-oriented programming, formulated mainly by Robert C. Martin ("Uncle Bob"), which help you write code that is easier to maintain, test and extend.

S - Single Responsibility Principle

A class should have only one reason to change - that is, be responsible for only one thing.

// Bad - the class does too much
public class User
{
    public void SaveToDatabase() { /* ... */ }
    public void SendEmail() { /* ... */ }
    public void ValidateData() { /* ... */ }
}

// Good - responsibilities separated
public class User { /* only user data */ }
public class UserRepository { public void SaveToDatabase(User user) { /* ... */ } }
public class EmailService { public void SendEmail(User user) { /* ... */ } }
public class UserValidator { public void Validate(User user) { /* ... */ } }

O - Open/Closed Principle

Code should be open for extension but closed for modification. Instead of changing existing code, you add new code.

// Bad - you have to modify it for every new type
public class PaymentProcessor
{
    public void Process(string type)
    {
        if (type == "card") { /* ... */ }
        else if (type == "blik") { /* ... */ }
    }
}

// Good - you add a new class instead of modifying existing code
public interface IPaymentMethod { void Process(); }
public class CardPayment : IPaymentMethod { public void Process() { /* ... */ } }
public class BlikPayment : IPaymentMethod { public void Process() { /* ... */ } }

L - Liskov Substitution Principle

Objects of a derived class must be substitutable for objects of the base class without breaking the correctness of the program.

// Bad - violates LSP, because Ostrich "breaks" the behaviour of Bird
public class Bird { public virtual void Fly() { /* ... */ } }
public class Ostrich : Bird
{
    public override void Fly() { throw new Exception("An ostrich can't fly!"); }
}

// Good - a separate hierarchy for flightless birds
public class Bird { public virtual void Move() { /* ... */ } }
public class FlyingBird : Bird { public virtual void Fly() { /* ... */ } }
public class Ostrich : Bird { /* nie ma Fly() */ }

I - Interface Segregation Principle

It's better to have many small, specialised interfaces than one big one - a client should not be forced to implement methods it doesn't need.

// Bad - one "fat" interface
public interface IWorker
{
    void Work();
    void Eat();
}

// Dobrze - rozdzielone interfejsy
public interface IWorkable { void Work(); }
public interface IEatable { void Eat(); }
public class Robot : IWorkable { public void Work() { /* ... */ } } // doesn't have to implement Eat()

D - Dependency Inversion Principle

High-level modules should not depend on low-level modules - both should depend on abstractions (interfaces), not on concrete implementations.

// Bad - direct dependency on a concrete implementation
public class OrderService
{
    private MySqlDatabase _db = new MySqlDatabase();
}

// Good - dependency on an abstraction, injected from outside
public interface IDatabase { void Save(object data); }
public class OrderService
{
    private readonly IDatabase _db;
    public OrderService(IDatabase db) { _db = db; } // MySQL, Postgres, cokolwiek
}

The practical point of SOLID is not applying it dogmatically on every line of code (it's easy to slide into excessive abstraction), but keeping these principles in the back of your mind when code starts to be hard to test, rigid against change, or when one change in one place breaks something in another - that is usually a sign that one of these principles is being violated.

Buy me a coffee