kamal
kamal

Reputation: 759

domain validation issues

Just a question, if i am using asp.net mvc as front end for my ddd application, then if i want validation in my domain entities, can i use asp.net mvc validation attribute for that ?

Because i was thinking that our domain should not tied to any specific programming language, so is using mvc validation attribute is awkward .

If i use mvc validation attribute than it will help me to do validation effectively, than writing custom .

Please help me in choosing an correct approach for it.

Upvotes: 0

Views: 311

Answers (2)

jgauffin
jgauffin

Reputation: 101150

You can use validation attributes on your view models, but not on your domain models.

I've written why in another answer (which I know that you've found, but others might be interested): https://stackoverflow.com/a/9765945/70386

Most mappers (like Automapper) work without public properties, so you could validate the view model and then copy over the information to the domain model.

The problem with that solution is that your domain events (and more complex validation logic inside your domain model methods) will probably not be triggered.

Domain models forces you to design your UI after the domain (since CRUD applications don't work very well with domain models). It can feel a bit awkward in the start, but the user experience will be so much higher.

Upvotes: 1

eulerfx
eulerfx

Reputation: 37739

An approach I often take is to use the System.ComponentModel.DataAnnotations attributes to validate ASP.NET MVC view model DTOs and then have the DTOs update or create domain objects accordingly. The domain objects validate themselves without validation attributes, using regular argument checks that throw ArgumentException instances instead. This allows the domain objects to always remain consistent, where as if you resort to a validation framework, you must be careful to execute the validation logic before entities are persisted (either by calling directly or intercepting ORM events). For example:

// this view model class lives in a Models folder in the ASP.NET MVC project
public class PersonViewModel
{
  [Required]  
  public string Name { get; set; }

  [DataType(DataType.EmailAddress)]
  public string Email { get; set; }

  public Person ToPerson()
  {
    return new Person(this.Name, this.Email);
  }

  public void UpdatePerson(Person person)
  {
    person.Name = this.Name;
    person.Email = this.Email;
  }
}

// this domain class normally lives in the domain layer project
public class Person
{
  public Person(string name, string email = null)
  {
    this.Name = name;
    this.Email = email;
  }

  string name;
  string email;

  public string Name
  {
    get { return this.name; }
    set 
    {
        if (string.IsNullOrEmpty(value)) 
          throw new ArgumentException();
        this.name = value;
    }
  }

  public string Email
  {
    get { return this.email; }
    set 
    {
        if (!string.IsNullOrEmpty(value) && !IsValidEmail(value))
          throw new ArgumentException();        
        this.email = value;
    }
  }
}

Also, you can choose to use the validation attributes in System.ComponentModel.DataAnnotations without using ASP.NET MVC. It is a separate assembly.

Upvotes: 1

Related Questions