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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:29:46+00:00 2026-05-20T10:29:46+00:00

Update 18th December 2012 Since this question seems to be getting quite a few

  • 0

Update 18th December 2012

Since this question seems to be getting quite a few views, I should point out that the accepted answer is not the solution I used, but it does provide the links and resources to build a solution, but, to my mind, not the ideal solution. My answer contains replacements for standard parts of the MVC framework; and you should only use those if you are comfortable checking that they still work for future versions (some private code was ripped out of the official sources, because there wasn’t enough extensibility in the base classes).

I can confirm, however, that these two classes also work for Asp.Net MVC 4 as well as 3.

It is also possible to repeat a similar implementation for the Asp.Net Web API framework as well, which I have done recently.

End update

I have a type that has a lot of ‘standard’ validation (required etc) but also a bit of custom validation as well.

Some of this validation requires grabbing hold of a service object and looking up some lower-level (i.e. ‘beneath’ the Model layer) meta data using one of the other properties as a key. The meta data then controls whether one or more properties are required as well as valid formats for those properties.

To be more concrete – the type is a Card Payment object, simplified to two of the properties in question as follows:

public class CardDetails
{
  public string CardTypeID { get; set; }
  public string CardNumber { get; set; }
}

I then have a service:

public interface ICardTypeService
{
  ICardType GetCardType(string cardTypeID);
}

ICardType then contains different bits of information – the two here that are crucial being:

public interface ICardType
{
  //different cards support one or more card lengths
  IEnumerable<int> CardNumberLengths { get; set; }
  //e.g. - implementation of the Luhn algorithm
  Func<string, bool> CardNumberVerifier { get; set; }
}

My controllers all have the ability to resolve an ICardTypeService using a standard pattern i.e.

 var service = Resolve<ICardTypeService>();

(Although I should mention that the framework behind this call is proprietary)

Which they gain via the use of a common interface

public interface IDependant
{
  IDependencyResolver Resolver { get; set; }
}

My framework then takes care of assigning the most-specific dependency resolver available for the controller instance when it is constructed (either by another resolver, or by the MVC standard controller factory). That Resolve method in the last-but one code block is a simple wrapper around this Resolver member.

So – if I can grab the selected ICardType for the payment that is received from the browser, I can then perform initial checks on card number length etc. The issue is, how to resolve the service from within my override of IsValid(object, ValidationContext) override of ValidationAttribute?

I need to pass through the current controller’s dependency resolver to the validation context. I see that ValidationContext both implements IServiceProvider and has an instance of IServiceContainer – so clearly I should be able to create a wrapper for my service resolver that also implements one of those (probably IServiceProvider).

I’ve already noted that in all places where a ValidationContext is produced by the MVC framework, the service provider is always passed null.

So at what point in the MVC pipeline should I be looking to override the core behaviour and inject my service provider?

I should add that this will not be the only scenario in which I need to do something like this – so ideally I’d like something which I can apply to the pipeline so that all ValidationContexts are configured with the current service provider for the current controller.

  • 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-20T10:29:47+00:00Added an answer on May 20, 2026 at 10:29 am

    Have you thought about creating a model validator, using a modelValidatorProvider, instead of using validation attributes? This way you’re not dependant on ValidationAttribute but can create your own validation implementation (this will work in addition the existing DataAnnotations validation).

    http://msdn.microsoft.com/en-us/library/system.web.mvc.modelvalidatorprovider.aspx

    http://dotnetslackers.com/articles/aspnet/Experience-ASP-NET-MVC-3-Beta-the-New-Dependency-Injection-Support-Part2.aspx#s10-new-support-for-validator-provider

    http://dotnetslackers.com/articles/aspnet/Customizing-ASP-NET-MVC-2-Metadata-and-Validation.aspx#s2-validation

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

Sidebar

Related Questions

Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update: Please read this question in the context of design principles, elegance, expression of
Update : this is more-or-less a dupe , and it turns out to be
Update : Clarified and expanded, since the original question was simplified too far I
Update: this question has been answered (see below). I'll leave it up in case
Update: Now that it's 2016 I'd use PowerShell for this unless there's a really
Update: Thanks for the suggestions guys. After further research, I’ve reformulated the question here:
UPDATE : I got my O3D situation straightened out, but I have yet to
Update: As someone pointed out I was missing an s in my Route registration.
UPDATE - No need to answer this now, I have solved below. Hi, I'm

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.