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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:29:57+00:00 2026-05-26T14:29:57+00:00

I have an ASP.NET MVC site, that uses a CommandService. This command service is

  • 0

I have an ASP.NET MVC site, that uses a CommandService. This command service is responsible for executing the commands, but before they are executed each command needs to be validated, so there’s a Validate operation that returns a ValidationResult, that looks like (simplified):

public class ValidationResult
{
  public List<string> ErrorCodes { get; set; }
}

I would like to improve this, because currently a list of strings is returned, like ‘UserDoesNotExist’ or ‘TitleIsMandatory’, and this is not the best approach of course.

It would be better to return something strongly typed. But how can I do that?

Option 1: use one big enum like:

public enum ErrorCode { UserDoesNotExist, TitleIsMandatory}

public class ValidationResult
{
  public List<ErrorCode> ErrorCodes { get; set; }
}

I don’t know if it’s a good idea to create such a big enum and put all domain error codes in it?

Option 2: use classes

public class ErrorCode {}
public class UserDoesNotExist : ErrorCode {}
public class TitleIsMandatory : ErrorCode {}

public class ValidationResult
{
  public List<ErrorCode> ErrorCodes { get; set; }
}

Is cleaner, but harder to use?

What would you do, or did I miss other options?

  • 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-26T14:29:58+00:00Added an answer on May 26, 2026 at 2:29 pm

    So this is the way I solved it. First, the main ValidationResult class looks like:

     public class ValidationResult
    {        
        public List<ValidationResultItem> ValidationResultItems { get; set; }
    
        public bool IsAcceptable
        {
            get { return (ValidationResultItems == null || !ValidationResultItems.Any(vri => !vri.IsAcceptable)); }
        }       
    
        public void Add(ValidationResultItem propertyValidationResultItem)
        {
            ValidationResultItems.Add(propertyValidationResultItem);
        }
    
        public void Add(IEnumerable<ValidationResultItem> validationResultItems)
        {
            ValidationResultItems.AddRange(validationResultItems);
        }       
    }
    

    ValidationResultItem is an abstract class:

    public abstract class ValidationResultItem
    {
        private ResultType _resultType;
    
        protected ValidationResultItem(ResultType resultType, string message)
        {
            ResultType = resultType;
            Message = message;
        }
    
        public bool IsAcceptable { get; private set; }
        public string Message { get; private set; }
        public ResultType ResultType
        {
            get { return _resultType; }
            set { _resultType = value; IsAcceptable = (_resultType != ResultType.Error); }
        } 
    }
    

    and there are two implementations of it:

    public class PropertyValidationResultItem : ValidationResultItem
    {        
        public PropertyValidationResultItem(ResultType resultType, string message, string propertyName, object attemptedValue) : base(resultType, message)
        {            
            PropertyName = propertyName;           
            AttemptedValue = attemptedValue;
        }
    
        public string PropertyName { get; private set; }        
        public object AttemptedValue { get; private set; }             
    }
    

    and

    public abstract class BusinessValidationResultItem : ValidationResultItem
    {
        protected BusinessValidationResultItem(ResultType resultType, string message) : base(resultType, message)
        {
        }
    }
    

    Each command handler has its own implementation of BusinessValidationResultItem, for example:

    public class AddArticleBusinessValidationResultItem : BusinessValidationResultItem
    {
        public enum AddArticleValidationResultCode { UserDoesNotExist, UrlTitleAlreadyExists, LanguageDoesNotExist }
    
        public AddArticleBusinessValidationResultItem(ResultType resultType, string message, AddArticleValidationResultCode code)
            : base(resultType, message)
        {
            Code = code;
        }
    
        public AddArticleValidationResultCode Code { get; set; }
    }
    

    This means that if the client gets a ValidationResult, he can cast the BusinessValidationResultItem to the concrete AddArticleBusinessValidationResultItem and so use the specific enumeration in a switch statement – avoiding magic strings.

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

Sidebar

Related Questions

I have a ASP.NET MVC site that uses forms authentication with custom AuthorizeAttribute on
I have an ASP.NET MVC site that uses both Microsoft Ajax [ Ajax.BeginForm() ]
I have an existing asp.net mvc website that uses basic forms authentication. The site
I have an ASP.NET MVC 3 web site that uses Windows Authentication running under
I am involved in testing an ASP.NET MVC site that uses a WF service
I have a very simple CRUD asp.net-mvc site that uses nhibernate to interface with
I have an ASP.NET MVC site that is hosted on an Amazon EC2 service.
Ok, so this is my dilemma... I have an ASP.NET MVC site that is
I have a small site I developed for a friend that uses ASP.Net MVC
I have a route in my asp.net mvc 2 site that looks like this

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.