Note that there are some explanatory texts on larger screens.

plurals
  1. POC# - Validation using DataAnnotations in MVVM and Displaying Meaningful Error Messages in View
    primarykey
    data
    text
    <p>Suppose I have a domain model like this:</p> <pre><code>public class MyDomainEntity { public double SomeVar { get; set; } } </code></pre> <p>I have a view which I would like to display SomeVar as a percentage. So my view model looks like:</p> <pre><code>public class MyViewEntity { public double SomeVarAsPercentage { get; set; } } </code></pre> <p>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.</p> <p>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:</p> <pre><code>public class MyDomainEntity { [Range(0.0, 1,0)] public double SomeVar { get; set; } } </code></pre> <p>And I can perform validation using </p> <pre><code>MyDomainEntity r = new MyDomainEntity(); ... //mapping code from view-model to domain model ... IList&lt;ValidationResult&gt; results = new List&lt;ValidationResult&gt;(); 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 } </code></pre> <p>However, because the validation is done in the domain model, the error message looks like:</p> <pre><code>"The field SomeVar must be between 0 and 1" </code></pre> <p>whereas the above error message needs to be translated into the view-model to read:</p> <pre><code>"The field SomeVarAsPercentage must be between 0 and 100" </code></pre> <p>in order to be meaningful to the user. </p> <p>The question here is:</p> <ol> <li>Does DataAnnotations have the facility to do this "error message translation"?</li> <li>Should I not be doing validation in the domain model in this case?</li> <li>Perhaps DataAnnotations as a validation mechanism is not sufficient or appropriate here? If this is the case, please suggest some other frameworks</li> </ol> <p><strong>Update:</strong></p> <p>I thought about this problem for a bit more and it seems like there is a couple of approaches</p> <p><strong>Approach 1: Validation in View-Model</strong></p> <p>Move the validation to the view-model, like this:</p> <pre><code>public class MyViewEntity { [Range(0.0, 100.0)] public double SomeVarAsPercentage { get; set; } } </code></pre> <p>So DataAnnotations can generate the desired error message.</p> <p>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:</p> <pre><code>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; } } </code></pre> <p>I have to put the Range rule in two places but both are really the same rule.</p> <p>Advantage: simple<br/> Disadvantage: A bit WET</p> <p><strong>Approach 2: Validate in Domain Model, with a slightly modified validate method</strong></p> <p>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).</p> <p>Advantage: more DRY, validation rule only appears once in the domain model<br/> Disadvantage: more complex</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload