简体   繁体   中英

Business Requests implementation on DDD on EF Core entities

I'm struggling with business rules validation on my entities. What I need to do is that each entity is capable of evaluating it's state following a certain set of business rules. I have an entity like this:

public class Person 
{
    public int PersonId { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
    public string Email { get; set; }
}

Currently, I have a requirement that Emails should be unique throughout all the system users. Each entity will have it's predefined set of rules and should be capable of evaluating each one of them independently. I was wondering if it's a good idea to inject some validation service into the entity that contains a method like Validate() Should I add those validations inside the Entity class or should I inject that service into an upper layer and call the validate method before adding a new Person into DbContext?

Best performance is gained when you put unique email constraint in the database directly. Using EF Core attributes, you need to put Unique attribute above the Email field.

Otherwise,you should keep any eye on the table updates. You likely need to check concurrent access to the db records using solutions like time-stamp.

As far as DDD is concerned, there is a rule. domain layer must be independent on other layers. So if entities located in domain layer dependents on services like validations, it violates the rule.

Application layer is responsible for checking duplicate emails by something like PersonRepository implemented by EF. After checking this sort of validations, you could create an instance of Person .

Validations in ordering service eShopOnContainers would be a useful sample for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM