Junior — Middle
How is the structure and interaction of domain model components implemented in your project?
sobes.tech AI
Answer from AI
In my project, the domain model is built around core entities that reflect the business logic. Typically, the structure includes:
- Entities — objects with a unique identifier and state.
- Aggregates — groups of related entities that are modified as a single unit.
- Repositories — interfaces for accessing aggregate data.
- Domain Services — operations that do not belong to a specific entity but are related to business logic.
Interaction occurs through clearly defined interfaces. For example, a controller calls a domain service, which retrieves an aggregate via a repository, modifies its state, and saves it back.
An example of a simplified structure in C#:
public class Order : IEntity
{
public Guid Id { get; private set; }
public List<OrderItem> Items { get; private set; }
public void AddItem(Product product, int quantity) {
// logic for adding a product
}
}
public interface IOrderRepository
{
Order GetById(Guid id);
void Save(Order order);
}
public class OrderService
{
private readonly IOrderRepository _repository;
public void AddProductToOrder(Guid orderId, Product product, int quantity) {
var order = _repository.GetById(orderId);
order.AddItem(product, quantity);
_repository.Save(order);
}
}
This approach helps isolate business logic and simplifies code maintenance.