Skip to main content

Repository : One repository by value object or entity

Medium
DDD
What is it?

This practice is triggered when multiple value objects or entities are managed within a single repository, as it becomes evident through excessive if/else or switch-case statements to handle different object types.

Why apply it?

This guideline is important because managing distinct value objects or entities in separate repositories enhances clarity, promotes single responsibility principle (SRP), and improves the scalability and maintainability of the codebase.

How to fix it?

Refactor the code by creating separate repositories for each value object or entity to ensure that each repository focuses on a single type of object, thus following the SRP.

Violations should be raised when, in classes which names end with 'Repository', the union of parameter types all of methods contains more than one object type.

Read more:

https://martinfowler.com/eaaCatalog/repository.html

Examples

Example 1:

Negative

Incorrect implementation that violates the practice.

namespace Practices.DDD.Repository.RepositoryByEntity
{
internal class OrderRepository
{
public void UpdateOder(Order order)
{
//Update Order
}

public void UpdateOrderStatus(OrderStatus orderStatus)
{
//Update Order Status
}
}
}