❔ 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:
Furthermore, I've crafted a specific Specification for a Domain
Entity known as Expense
:
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
You can take a look on the whole project here: https://github.com/MbarkT3STO/ExpenovaApp/tree/main/Source/ExpenseService
GitHub
ExpenovaApp/Source/ExpenseService at main · MbarkT3STO/ExpenovaApp
Contribute to MbarkT3STO/ExpenovaApp development by creating an account on GitHub.
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
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 approachesLooks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.