C
C#16mo ago
M B V R K

❔ DDD: How to apply a Domain specification on a Infrastructure EF Core?

Hello everyone, I'm currently working on an app utilizing DDD (Domain Driven Design) and Microservices. One key element in DDD that I'm focusing on is Specifications. Specifically, I'm dealing with the ExpenseService, consisting of 5 projects: ExpenseService.API ExpenseService.Application ExpenseService.Domain.Shared ExpenseService.Domain ExpenseService.Infrastructure Normally, Specifications are defined and implemented within the Domain layer. In this context, I have a foundational class named Specification:
namespace ExpenseService.Domain.Specifications;

public abstract class Specification<T> : ISpecification<T> where T : class
{
public abstract Expression<Func<T, bool>> ToExpression();

public bool IsSatisfiedBy(T entity)
{
var predicate = ToExpression().Compile();
return predicate(entity);
}
}
namespace ExpenseService.Domain.Specifications;

public abstract class Specification<T> : ISpecification<T> where T : class
{
public abstract Expression<Func<T, bool>> ToExpression();

public bool IsSatisfiedBy(T entity)
{
var predicate = ToExpression().Compile();
return predicate(entity);
}
}
Furthermore, I've crafted a specific Specification for a Domain Entity known as Expense:
namespace ExpenseService.Domain.Specifications;

public class ValidExpenseSpecification : Specification<Expense>
{
public override Expression<Func<Expense, bool>> ToExpression()
{
return expense => expense.Amount > 0
&& expense.Date > DateTime.MinValue
&& !string.IsNullOrEmpty(expense.Description);
}
}
namespace ExpenseService.Domain.Specifications;

public class ValidExpenseSpecification : Specification<Expense>
{
public override Expression<Func<Expense, bool>> ToExpression()
{
return expense => expense.Amount > 0
&& expense.Date > DateTime.MinValue
&& !string.IsNullOrEmpty(expense.Description);
}
}
The Challenge: The challenge I'm facing arises from the separation of Domain entities from their Infrastructure counterparts. For instance, while I employ EF Core and PostgreSQL in the Infrastructure to manage entities, I've created equivalent Domain entities in the Infrastructure layer (e.g., ExpenseEntity corresponding to the Expense domain entity). Consequently, the ToExpression() method exclusively targets Domain entities and doesn't function with their Infrastructure equivalents. I'm seeking suggestions on how to tackle this. I mean, How to make the Domain specifications to be applied on the Infrastructure EF Core entities ?
4 Replies
M B V R K
M B V R KOP16mo ago
GitHub
ExpenovaApp/Source/ExpenseService at main · MbarkT3STO/ExpenovaApp
Contribute to MbarkT3STO/ExpenovaApp development by creating an account on GitHub.
jiniux
jiniux16mo ago
do you want to use the specification to perform a query? i suggest you to look into CQRS before using the repository pattern in bad ways because you may end up creating a leaky abstraction and overfetching data for instance, if you want to query the database to show some data on the webpage, you should directly go through EF core instead of using an aggregate root aggregate roots should only be fetched (and you can use the repository pattern for that eventually) when performing an operation that mutates the state of your system because aggregate roots should be used to guarantee data integrity and to enforce your business rules
M B V R K
M B V R KOP16mo ago
First of all, thanks a lot for your response, I appreciate.
do you want to use the specification to perform a query?
Yes, I want to use specifications to check/enforce some business rules, and to perform some quiries. About CQRS I'm planning to use it too in the Application layer Pleaase fell free to share with me your experience if you have a better approaches
Accord
Accord16mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server