11

What's a good way to validate a model when information external to the model is required in order for the validation to take place? For example, consider the following model:

public class Rating {
    public string Comment { get; set; }
    public int RatingLevel { get; set; }
}

The system administrator can then set the RatingLevels for which a comment is required. These settings are available through a settings service.

So, in order to fully validate the model I need information external to it, in this case the settings service.

I've considered the following so far:

  1. Inject the service into the model. The DefaultModelBinder uses System.Activator to create the object so it doesn't go through the normal dependency resolver and I can't inject the service into the model without creating a new model binder (besides which, that doesn't feel like the correct way to go about it).
  2. Inject the service into an annotation. I'm not yet sure this is possible but will investigate further soon. It still feels clumsy.
  3. Use a custom model binder. Apparently I can implement OnPropertyValidating to do custom property validation. This seems the most preferable so far though I'm not yet sure how to do it.

Which method, above or not, is best suited to this type of validation problem?

Kaleb Pederson
  • 45,767
  • 19
  • 102
  • 147

4 Answers4

16

Option 1 doesn't fit. The only way it would work would be to pull in the dependency via the service locator anti-pattern.

Option 2 doesn't work. Although I couldn't see how this was possible because of the C# attribute requirements, it is possible. See the following for references:

Option 3: I didn't know about this earlier, but what appears to be a very powerful way to write validators is to use the ModelValidator class and a corresponding ModelValidatorProvider.

First, you create your custom ModelValidatorProvider:

public class CustomModelValidatorProvider : ModelValidatorProvider
{
    public CustomModelValidatorProvider(/* Your dependencies */) {}

    public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context)
    {
        if (metadata.ModelType == typeof(YourModel))
        {
            yield return new YourModelValidator(...);
        }
    }
}

ASP.NET MVC's IDependencyResolver will attempt to resolve the above provider, so as long as it's registered with your IoC container you won't need to do anything else. And then the ModelValidator:

public class EntryRatingViewModelValidatorMvcAdapter : ModelValidator
{
    public EntryRatingViewModelValidatorMvcAdapter(
            ModelMetadata argMetadata,
            ControllerContext argContext)
                : base(argMetadata, argContext)
    {
        _validator = validator;
    }


    public override IEnumerable<ModelValidationResult> Validate(object container)
    {
        if (/* error condition */)
        {
            yield return new ModelValidationResult
              {
                MemberName = "Model.Member",
                Message = "Rating is required."
              };
        }
    }
}

As the provider is retrieved through the IDependencyResolver and the provider has full control over the returned ModelValidators I was easily able to inject the dependencies and perform necessary validation.

Saanch
  • 1,814
  • 1
  • 24
  • 38
Kaleb Pederson
  • 45,767
  • 19
  • 102
  • 147
  • 1
    I am using MVC5 but I think the above it still relevant. You've said that "MVC's IDependencyResolver will attempt to resolve the provider". But how would you register the provider with the IoC container? (Unity in my case). – Ben Mar 27 '17 at 12:41
  • @Ben In my case, using Ninject, the following binding was sufficient to get my custom validator provider picked up: `kernel.Bind().To();` – Zack Aug 04 '20 at 20:31
5

You could try fluent validation. It supports asp.net mvc and DI so you can inject external services into your validators.

Ben Foster
  • 34,340
  • 40
  • 176
  • 285
1

Assuming that you want both client and server-side validation of the model based upon the values returned from the service, I would opt for 2., Inject the service into an annotation.

I give some sample code in my response to this question about adding validators to a model. The only additional step in your case is that you will need to inject your service into your class inheriting from DataAnnotationsModelValidatorProvider.

Community
  • 1
  • 1
counsellorben
  • 10,924
  • 3
  • 40
  • 38
0

What about just simply using IValidateableObject and in that method determine if validation is appropriate or not and setting the errors there?

How do I use IValidatableObject?

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • In order for the model to validate itself it would need access to the settings service. Unless I use the service-locator anti-pattern I'd need to create a custom-model binder to inject the service into the model so that it could do the validation, none of which feels right. – Kaleb Pederson Oct 12 '11 at 20:44