How can you cope with validation on complex aggregates inside a domain driven design? Would you consolidate your company rules/validation logic?

I realize argument validation. And That I understand property validation which may be connected to the models themselves and do such things as make sure that their email or local zip code applies or that the name includes a minimum and maximum length.

But how about complex validation which involves multiple models? Where would you typically place these rules &lifier techniques in your architecture? And what designs if any would you use to implement them?

I love Jimmy Bogard's fix for your problem. He's a publish on his blog entitled "Entity validation with site visitors and extension techniques" by which he presents a really elegant method of entity validation that suggest the implementation of the separate class to keep validation code.

public interface IValidator<T>

public class OrderPersistenceValidator : IValidator<Order>

    public IEnumerable<string> BrokenRules(Order entity)

    under ."

        if (string.IsNullOrEmpty(entity.Customer))

            yield return "Must incorporate a customer."

        yield break


Rather than depending on IsValid(xx) calls on your application, consider a little advice from Greg Youthful:

Do not ever let your organizations enter into an invalid condition.

What this essentially means is you transition from considering organizations as pure data containers and much more about objects with actions.

Think about the illustration of an individual's address:

 person.Address = "123 my street"

 person.City = "Houston"

 person.Condition = "Texas"

 person.Zip = 12345

Between some of individuals calls your entity is invalid (since you might have qualities that do not accept one another. Now think about this:


all the calls relevant towards the behavior of altering a previous address are actually an atomic unit. Your entity isn't invalid here.

For this concept of modeling actions instead of condition, you'll be able to achieve one that does not allow invalid organizations.

For any good discussion about this, read this infoq interview: http://world wide

I usualy make use of a specs class, it possesses a method (this really is C# however, you can translate it in a language) :

bool IsVerifiedBy(TEntity candidate)

This process works an entire check from the candidate and it is relations. You should use arguments within the specs class to really make it parametrized, just like a check level...

You may also add a means to know why the candidate didn't verify the specs :

IEnumerable<string> BrokenRules(TEntity canditate)

You can just choose to implement the very first method such as this :

bool IsVerifiedBy(TEntity candidate)

For damaged rules, I usualy write an iterator :

IEnumerable<string> BrokenRules(TEntity candidate)

explaining cleary what's wrong...Inch

  if (someOtherCondition)

      yield return

   string.Format("The total amount shouldn't be  once the condition is ",

        amount, condition)

For localization, you need to use assets, why is this so pass a culture towards the BrokenRules method. I place this classes within the model namespace with names that suggest their use.