2
votes

Suppose I have a domain model like this:

public class MyDomainEntity
{
    public double SomeVar { get; set; }
}

I have a view which I would like to display SomeVar as a percentage. So my view model looks like:

public class MyViewEntity
{
    public double SomeVarAsPercentage { get; set; }
}

And I have a mapping class which maps between the view-model and the domain model which simply divide and multiply the variable by 100.

Now I would like to have some validation done on the domain model. The business rule in this case dictates that SomeVar in the domain model must be between 0.0 and 1.0 inclusive. This is easily done with System.ComponentModel.DataAnnotations.RangeAttribute:

public class MyDomainEntity
{
    [Range(0.0, 1,0)]
    public double SomeVar { get; set; }
}

And I can perform validation using

MyDomainEntity r = new MyDomainEntity();

...
//mapping code from view-model to domain model
...

IList<ValidationResult> results = new List<ValidationResult>();
ValidationContext context = new ValidationContext(r, null, null);
Validator.TryValidateObject(r, context, results, true);
foreach (ValidationResult e in results)
{
    //I would like to display the error messages, if any, to the user
}

However, because the validation is done in the domain model, the error message looks like:

"The field SomeVar must be between 0 and 1"

whereas the above error message needs to be translated into the view-model to read:

"The field SomeVarAsPercentage must be between 0 and 100"

in order to be meaningful to the user.

The question here is:

  1. Does DataAnnotations have the facility to do this "error message translation"?
  2. Should I not be doing validation in the domain model in this case?
  3. Perhaps DataAnnotations as a validation mechanism is not sufficient or appropriate here? If this is the case, please suggest some other frameworks

Update:

I thought about this problem for a bit more and it seems like there is a couple of approaches

Approach 1: Validation in View-Model

Move the validation to the view-model, like this:

public class MyViewEntity
{
    [Range(0.0, 100.0)]
    public double SomeVarAsPercentage { get; set; }
}

So DataAnnotations can generate the desired error message.

However doing validation in the view-model (as apposed to in the domain model) is more WET. Suppose my form can switch between two views: one where SomeVar is displayed as a percentage, the other where SomeVar is displayed "as is" but only to two decimal places. Then I have to do:

public class MyViewEntity1
{
    [Range(0.0, 100.0)]
    public double SomeVarAsPercentage { get; set; }
}


public class MyViewEntity2
{
    [Range(0.0, 1.0)]
    public double SomeVarToTwoDp { get; set; }
}

I have to put the Range rule in two places but both are really the same rule.

Advantage: simple
Disadvantage: A bit WET

Approach 2: Validate in Domain Model, with a slightly modified validate method

Extend the DataAnnotations validation framework so that it not only returns the error property name, but the rule that it violated and an error message factory. So the mapping between the View-Model entity and the Domain Model entity will generate a meaningful error message using the error message factory and the rule (which needs to be mapped too).

Advantage: more DRY, validation rule only appears once in the domain model
Disadvantage: more complex

1

1 Answers

1
votes

I would say that you should be validating in both places - SomeVarAsPercentage in the ViewModel, display your ViewModel validation error message as appropriate, and throw away invalid inputs (i.e. don't pass them to the model).

Then, in your model, you can validate SomeVar if it gets through the VM validation stage. The model can report errors (in its terms) to the VM, which can translate them into user domain terms, just like the VM does for values.

You would need to validate in both places because the validation type is different - a simple percentage validation in the VM, but potentially a more involved validation in the model, taking into account other values etc.