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

  • Home
  • SEARCH
  • 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 6956379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:51:43+00:00 2026-05-27T14:51:43+00:00

I am trying to implement a validation strategy in my application. I have an

  • 0

I am trying to implement a validation strategy in my application. I have an MVC layer, Service layer, Repository and Domain POCOs. Now at the MVC layer, i use data annotations on my viewmodels to validate user input, allowing me to give the user quick feedback. In the controller, I call ModelState.IsValid to check the input before using automapper to set up a domain object.

Here’s where my trouble is. I pass my domain object into the Service which needs to validate it against my business rules but how do I pass validation errors back to the controller? Examples I have found do one of the following:

  • Throw an exception in the Service layer and catch in the Contoller. But this seems wrong, surely exceptions are for exceptional cases, and we should return something meaningful.
  • Use a ModelStateWrapper and inject the ModelStateDictionary into the Service. But this method ultimately unfolds into a circular dependency (controller depends on service, service depends on controller) and this seems to be a bad code smell.
  • Add a Validate method to the POCO. Problem with this is that a business rule may rely on other POCO objects so surely this should be done in the Service which has access to the required tables and objects.

Is there a simpler method I am missing? I have seen lots of questions regarding this but no concrete solution other than those mentioned above. I am thinking that any method in the Service which does validation could just pass back some key/value object that I can use in the controller but am unsure if this strategy could be problematic later on.

  • 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-05-27T14:51:43+00:00Added an answer on May 27, 2026 at 2:51 pm

    I think quite an elegant way would be to have a validate method on your service that returns a dictionary of model errors from business logic. That way, there is no injection of the ModelState to the service – the service just does validation and returns any errors. It is then up to the controller to merge these ModelState errors back into it’s ViewData.

    So the service validation method might look like:

    public IDictionary<string, string> ValidatePerson(Person person)
    {
        Dictionary<string, string> errors = new Dictionary<string, string>();
    
        // Do some validation, e.g. check if the person already exists etc etc
    
        // Add model erros e.g.:
        errors.Add("Email", "This person already exists");
    }
    

    And then you could use an extension method in your controller to map these errors onto the ModelState, something like:

    public static class ModelStateDictionaryExtensions
    {
        public static void Merge(this ModelStateDictionary modelState, IDictionary<string, string> dictionary, string prefix)
        {
            foreach (var item in dictionary)
            {
                modelState.AddModelError((string.IsNullOrEmpty(prefix) ? "" : (prefix + ".")) + item.Key, item.Value);
            }
        }
    }
    

    And your controller would then use:

    ModelState.Merge(personService.ValidatePerson(person), "");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement validation in my ASP.NET Webforms application. Right now, the only
I'm working on an MVC application and i'm trying to implement some validation. I've
I'm trying to implement a custom validation annotation in Seam. We have a list
I'm trying to implement IP address validation on my page. I have a test
I am trying to implement REST APIs, so in my RoR3 application I have
I'm trying to implement a generic way of adding remote validation to xVal /
I'm trying to implement XOR in javascript in the following way: // XOR validation
While trying to implement an MVC file upload example on Scott Hanselman's blog. I
I am new to WPF and trying to implement validation control on submit form.
I am trying to implement form validation using cakephp models. Here are my code

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.