Menu

1 / 2

SOLID Principles in C# — I Refactored 60 Ugly Lines, One Rule at a Time

DEV Community: csharp·kirandeepjassal-crypto·5 months ago
#PLCIXV7H
#dev#real#solid#order#principle
Reading 0:00
15s threshold

SOLID is a default, not a religion.

Most articles teach the five principles with toy examples — a Square that violates LSP, a Logger that violates SRP. You finish having memorised five acronyms with no idea what to do on Monday morning when your real order service has 11 reasons to change and a switch statement on payment type.

So I took a real, ugly 60-line order-processing service that breaks every SOLID rule, and refactored it one principle at a time.

The starting point

public class OrderService { public void PlaceOrder ( Order order ) { // validate, apply discount, charge Stripe, // open SqlConnection, send SMTP email, // append to a log file // 60 lines. 6 jobs. 3 hard-coded providers. } }

Enter fullscreen mode

Exit fullscreen mode

Untestable without a real Stripe key, a real database, and an SMTP server.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.