Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I validate inside DDD domain project?

I want to validate my domain model entities using FluentValidation. I have read an answer about validation in DDD that has used FluentValidation for validating his entity. Here is how he validate its entity:

public class ParticipantValidator : AbstractValidator<Participant>
{
    public ParticipantValidator(DateTime today, int ageLimit, List<string> validCompanyCodes, /*any other stuff you need*/)
    {...}

public void BuildRules()
{
         RuleFor(participant => participant.DateOfBirth)
                .NotNull()
                .LessThan(m_today.AddYears(m_ageLimit*-1))
                .WithMessage(string.Format("Participant must be older than {0} years of age.", m_ageLimit));

        RuleFor(participant => participant.Address)
            .NotNull()
            .SetValidator(new AddressValidator());

        RuleFor(participant => participant.Email)
            .NotEmpty()
            .EmailAddress();
        ...
}

}

So my Domain Project is depend on FluentValidation library.

But I think it is bad Idea that my Domain Project depends on third party library. How I can prevent this problem?

like image 250
Seyed Morteza Mousavi Avatar asked Sep 03 '15 16:09

Seyed Morteza Mousavi


1 Answers

As soon as your entities don't depend nor rely on this third party library, it's ok and valid. Actually, the idea behind having the domain layer, is to have a common language between developers and people from the business area, so it should be the most clean and natural possible. Then you could show the domain to a business analyst, make sure they understand it and discuss further. If the validation code has validations which make sense to the business, then it's valid. Actually a business analyst could even see it and make additional observations (e.g this validation rule should be that way). Your validation code looks fine to business.

like image 103
Alisson Reinaldo Silva Avatar answered Oct 06 '22 01:10

Alisson Reinaldo Silva