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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:48:06+00:00 2026-05-22T20:48:06+00:00

Up to now, most of our validation is performed using validation attributes on our

  • 0

Up to now, most of our validation is performed using validation attributes on our view models.

One additional validation check we need to perform is to validate that a string doesn’t already exist in our database.

Originally I was just handling this check within the controller action and then adding an error into ModelState if required. However, I would rather make use of the built-in validation infrastructure.

One method I tried was implementing IValidateableObject on my viewmodel. This feels a bit wrong as I’m calling DependencyResolver:

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    var viewModel = validationContext.ObjectInstance as EditPostViewModel;
    if (viewModel != null)
    {
        var slug = (Slug)viewModel.Slug;
        var repo = DependencyResolver.Current.GetService<IRepository<Post>>();
        var existing = repo.Get(p => p.Slug == slug && p.Id != viewModel.Id);
        if (existing != null)
            yield return new ValidationResult("Duplicate slug.", new[] { "Slug" });
    }
}

Another approach I thought about was using a custom ValidationAttribute. To me this only makes sense if I can re-use it on multiple viewmodels and to re-use it I need to be able to construct a generic repository interface (as per the above code) as I may need IRepository<Foo> or IRepository<Bar> depending on the Model.

Remote Validation is great but I still need to the validation server side.

So what would people recommend or have used themselves to achieve something similar.

Note that I do have unique database constraints on this column but don’t want to fall back to exception handling to perform this validation.

Solution

I had a look as the asp.net article @Darin suggested. This approach definitely works, but the article is a bit flawed in that whilst it manages to decouple the validation service away from any direct references to ModelState, you instead end up with a circular dependency where controller depends on the validation service and the validation service depends on ModelState (via a wrapper); which doesn’t exist until the controller is created. Nice!

Instead I exposed the IValidationDictionary as a public property on my validation service and set this in my controllers constructor:

slugValidator.ValidationDictionary = new ModelStateWrapper(this.ModelState);

The rest is kind of application specific but essentially I created a validator for each type of “slugable” entity I wanted to validate. These were then injected by my container.

public interface ISlugValidator<TEntity> where TEntity : ISlugable {
    IValidationDictionary ValidationDictionary { get; set; }
    bool ValidateSlug(Guid? entityId, Guid featureId, Slug slug);
}

I call ValidateSlug(...) just before I check ModelState.IsValid in my controller actions.

This is a good solution for the level of validation I need currently and the fact that most of it can be handled using Data annotations. If my validation/business rules get more complex I will probably switch to FluentValidation (this also works well with Depenency Injection) as it’s better suited for externalizing validation logic.

  • 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-22T20:48:07+00:00Added an answer on May 22, 2026 at 8:48 pm

    I would recommend doing this type of validation at the service layer and not bother with data annotations.

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

Sidebar

Related Questions

I been using LDAP for alot of years now and most of the firms
Most projects now need some form of a database. When someone says database, I
What are the most popular architecture with dotnet? Right now i am using ASP.NET
Further to my recent questions, I've now closed most of the connections that our
We are using SCons for all our build need, and we would like to
Up until now I've been deploying Rails apps to our Apache/Passenger setup using a
We need to override a DependencyProperty 's metadata for our subclass. Now I know
Most people talk about progressive enhancement right now as serving browsers with javascript (enhanced
Now I've heard the expression maths is the language if the universe; and most
I am learning ASP.NET MVC now a days and I have found that most

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.