C
C#11mo 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 K11mo ago
GitHub
ExpenovaApp/Source/ExpenseService at main · MbarkT3STO/ExpenovaApp
Contribute to MbarkT3STO/ExpenovaApp development by creating an account on GitHub.
jiniux
jiniux11mo 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 K11mo 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
Accord11mo 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
More Posts
❔ WPF Change ViewsIm trying to switch views with MVVM pattern❔ Executing a method every n seconds but only during certain conditionsWhat would be the best way to do this? The easiest way I can think of would be: ```cs Task.Run(MainA❔ GtkSharp: Creating a GClosureSimply put, I am trying to complete the following code: ```csharp AccelGroup binds = new AccelGroup(✅ Confused by constant errors regarding class extension.Hi all, for my uni course, I need to code a game. I have chosen TicTacToe, but it needs to use diffe❔ How would you embed a Git commit hash in an assembly?I want to stamp a commit hash into a binary so I can display it in the UI. I'm fine with pulling stu❔ Is it possible to run integration tests with typescript as client and .net api as server?My hope is to test a client written in typescript which calls a .net api in the same solution.✅ .csproj – how to generate nodes dynamically by iterating over a string array ?how can i generate xml code in the .csproj file? i dont want to repeat my actions 10000 times... so ❔ AWS multiple file upload Using API in Razor PageI am trying to send the API request from the razor page. The Endpoint is not hitting the API Control✅ Override (abstract) getter-only property with a setterHello! I was wondering if there is anyway to add a setter to an overridden getter-only property? Cu❔ Proper way to do something in C# (migrating an old Visual Basic library)So I'm rewriting an ancient library and used an automatic transpiler to convert it all from VB.NET t