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 7909925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:39:34+00:00 2026-06-03T12:39:34+00:00

I have some questions about how is the best way to implement DDD principles

  • 0

I have some questions about how is the best way to implement DDD principles with best pratices of asp.net mvc. Actually, I would like to know, how have you been doing the validation (in viewmodel or model)?

I have this DomainModel and ViewModel:

public class Product {
   public int Id { get; protected set; }
   public string Name { get; set; }
   public decimal Price { get; set; }
   public int Stock { get; set; }
   public Category Category { get; set; }
   public Supplier Supplier { get; set; }
   public string Code { get; set; } // it has to be unique
}

public class ProductViewModel {
   public int Id { get; set; }
   /* other simple properties */
   public int IdCategory { get; set; }
   public int IdSupplier { get; set; }
   public string Code { get; set; } 
}

Ok. The model is mapped with NHibernate and works fine. I want to know, if it’s better create a validation for ViewModel or the DomainModel? I mean, when I receve the ViewModel on a action of asp.net mvc, I will validate it, but if I add business rules on viewmodel, won’t I doing wrong? I ask this because I know it’s better to add these business validation to my domain, but should I do two validations on my post before persist? Look my action on asp.net mvc:

[Post]
public ActionResult Update(ProductViewModel viewModel) {

  // what kind of validations should I do here? 
  if (invalid) {
    return View(viewModel);
  } 

  // how can I return the errors to my View?

  // Is here any best pratice to transform from ViewModel to Domain instance?
  Product product = ???

  _repository.Save(product);

  return RedirectToAction("Index");
}

Could someone do an example by code?

  • 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-03T12:39:40+00:00Added an answer on June 3, 2026 at 12:39 pm

    I would say that you should perform validations at the two levels. In your view model you will do surface validations such as for example this field is required and this field must be of the following format whereas in your business layer you should be validating business rules such as the username already exists, …

    So let’s take an example of how a typical POST action might look like:

    [HttpPost]
    public ActionResult Update(ProductViewModel viewModel) 
    {
        if (!ModelState.IsValid) 
        {
            // The view model is invalid => redisplay the view in order
            // to show the error messages
            return View(viewModel);
        }    
    
        // at this stage we know that the view model is valid =>
        // now we can map it to a domain model that we want to update:
    
        Product product = Repository.GetModel(viewModel.Id);
    
        // I use AutoMapper (http://automapper.org/) to map between 
        // my domain models and my view models. In this example
        // we are updating only the properties of the domain model
        // that were part of the view model and that the user is allowed
        // to modify in this view => we are merging
        Mapper.Map<ProductViewModel, Product>(viewModel, product);
    
        // now we can process the domain model
        Repository.Update(product);
    
        return RedirectToAction("Index");
    }
    

    and if you wanted to handle domain model errors there are different possibilities. Personally I like the TryXXX pattern:

    string errorMessage;
    if (!Repository.TryUpdate(product, out errorMessage))
    {
        // the business validation failed => add the error message to the modelstate
        // and redisplay the view
        ModelState.AddModelError("", errorMessage);
        return View(viewModel);
    }
    

    Another possibility is to pass the ModelState dictionary to the business layer and let it add the model state error directly. This way you can once again simply test if (!ModelState.IsValid) in the view to know whether something went wrong in the business layer and redisplay the same view to show the error message.

    As far as validation in the view model is concerned there are different ways. You could use the Microsoft’s official way which is by decorating your view model properties with validation attributes. For example:

    public class ProductViewModel 
    {
        [Required]
        public string Foo { get; set; }
    
        ...
    }
    

    Personally I don’t use those attributes. I find them extremely limiting especially when you want to do a little more complex validations with dependent properties and so on. For this reason I use FluentValidation.NET which integrates very nicely with ASP.NET MVC and allows me to easily unit test my validation rules.

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

Sidebar

Related Questions

[Disclaimer: I'm ASP.NET MVC Developer] Hi, I'm looking for some best practices with implementing
I have some questions about three tier architecture. how to implement an application in
Recently started with ASP.NET and MVC and have a few questions on working with
I have some questions about \r\n : newlines are browser dependent? (not how they
I have some questions about vector ; How can I append one vector in
I have some questions about basic CSS that I was unable to understand or
I have some questions about customers about NF mode for DB2. Google had very
I have some questions about multi-threaded programming and multi-core usage. In particular I'm wondering
I have some questions about logging, more specifically about setting it up and making
I have some questions about Perl's map function. Specifically: How does %hash = map

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.