BBackend CourseLeroy Labs
0/0
Search the course

SOLID, clean code & Domain-Driven Design

The five SOLID principles with examples from your code, DDD strategic design (bounded contexts, ubiquitous language) and tactical design (aggregates, value objects, domain events, repositories).

● advancedSpring-Boot-StoreHostel-Web-AppSperoTek (in dev)IR3D (Kontentt)Report-Manager-2

SOLID

Principle Meaning In your code
Single Responsibility One reason to change RefreshTokenService handles only refresh tokens; JournalRules only validates
Open/Closed Extend without modifying RM report Strategies; SperoTek event listeners
Liskov Substitution Subtypes honour the contract StubPaymentGateway must behave like a real gateway (same states and errors)
Interface Segregation Small, focused interfaces PostingEngine, AccountDirectory, CompanyDirectory instead of one giant service
Dependency Inversion Depend on abstractions Services depend on PaymentGateway / EmailTransport, not on Stripe or SMTP

Clean code habits that matter most

  • Names that reveal intent: findByIdForUpdate, rejectOtherPendingForRoom. Yours are good.
  • Small functions, early returns, no boolean flag parameters.
  • Make illegal states unrepresentable (factories, enums, sealed types, constraints).
  • Comments explain why, not what. Linking code to decisions (SperoTek’s D-13, AUTH-001) is excellent practice.
  • Fail fast at boundaries (validation, startup checks).

DDD: strategic design

  • Ubiquitous language. Code uses the business’s words: journal, posting, reversal, fiscal period, control account (SperoTek); amendment window, report unit (RM).
  • Bounded contexts. A model is valid within a boundary. Spring Modulith modules are bounded contexts.
  • Context mapping. How contexts relate: published language (events), anti-corruption layer (adapters around Paystack).

DDD: tactical design

Building block Meaning Example
Entity Identity through time User, Booking
Value object Defined by its values, immutable Money, an address, a date range
Aggregate A consistency boundary with a root; outside code references it only by id JournalEntry + lines; Cart + items; Order + items
Domain event Something that happened JournalPosted, ApproversAssignedEvent
Repository Collection-like access to aggregates Spring Data repositories, one per aggregate root
Domain service Logic that belongs to no single entity DefaultPostingEngine, LedgerService
Factory Complex creation with invariants Booking.createBooking(...), Order.fromCart(...)
SperoTek (in dev)JournalEntry aggregate · Money value object · references by UUID across modules

Modifying lines only through the JournalEntry root keeps the balance invariant inside one transaction. Other modules reference entries by id, never by object.

Spring-Boot-StoreCart.addItem · Order.fromCart · Order.isPlacedBy

A rich domain model: behaviour lives with the data, rather than “anemic” entities plus fat services.

Check yourself

1. Adding a new report type by adding a class (no edits) demonstrates…
2. An aggregate is…
3. Money(amount, currency) is a…