When using Domain-Centric architectures such as Clean Architecture, we need to decide where the business logic will go. There are two main approaches to this: Anemic Domain Model and Rich Domain Model. Understanding the differences between these two models is crucial for making informed decisions about your software architecture.
An Anemic Domain Model is characterized by:
class Order {public int Id { get; set; }public DateTime OrderDate { get; set; }public decimal TotalAmount { get; set; }}
Figure: Example - Anemic model where the Order class is just a data container with no behavior.
A Rich Domain Model, on the other hand, embeds business logic in the model (within Aggegates/Entities/Value Objects/Services). This approach makes the domain the heart of your system, as opposed to being database or UI-centric.
A Rich Domain Model is characterized by:
class Order {public int Id { get; private set; }public DateTime OrderDate { get; private set; }public decimal TotalAmount { get; private set; }public Order(DateTime orderDate) {OrderDate = orderDate;TotalAmount = 0;}public void AddItem(decimal itemPrice) {if (itemPrice <= 0){throw new ArgumentException("Item price must be greater than zero.");}TotalAmount += itemPrice;}}
Figure: Example - Rich model where the Order class encapsulates data and business logic.
In both cases the Application is still responsible for communicating with external systems via abstractions implemented in the Infrastructure Layer.
Projects with complex domains are much better suited to a Rich Domain model and Domain Driven Design (DDD) approach. DDD is not an all-or-nothing commitment; you can introduce the concepts gradually where they make sense in your application.
One side-effect of pushing logic into our Domain layer is that we can now start to write unit tests against our domain models. This is easy to do as our Domain is independent of our infrastructure or persistence layer.
By understanding the differences between anemic and rich domain models, you can make informed decisions about your software architecture and ensure that your project scales effectively with complexity.
When using Clean Architecture we consider the Application Layer is part of the 'Core' of our system. It needs to interact with the Domain Layer for the system to function. This will happen in two slightly different ways depending on the underlying model.