Seyed Morteza Mousavi
Seyed Morteza Mousavi

Reputation: 6963

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?

Upvotes: 11

Views: 6505

Answers (1)

Alisson Reinaldo Silva
Alisson Reinaldo Silva

Reputation: 10715

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.

Upvotes: 8

Related Questions