Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8550315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:55:26+00:00 2026-06-11T13:55:26+00:00

Suppose I have a domain model like this: public class MyDomainEntity { public double

  • 0

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:55:27+00:00Added an answer on June 11, 2026 at 1:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have these domain objects: DataPoint ========= public int Id {get;set;} public int
Suppose I have a simple model, such as Record: @Model public class Record {
Suppose i have the following class. public class Location { public Id { get;
suppose I have a domain classes: public class Country { string name; IList<Region> regions;
Suppose I have the following Domain class: class Book { String title String author
Suppose you have the following domain classes class User { static hasMany = [books:Book]
Suppose you have the next class. It contains the systems in which the agent
Suppose I have two session cookies which looks like First one Name: d58ba4091c622661a0d46f03b412ac8b Content:
Suppose you have an application that utilizes the domain-model pattern, DDD and lots of
How to use subdomain like user.test.com in IIS 7. Suppose we have main domain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.