Skip to content

Instantly share code, notes, and snippets.

@benfoster
Created October 7, 2012 15:52
Show Gist options
  • Save benfoster/3848727 to your computer and use it in GitHub Desktop.
Save benfoster/3848727 to your computer and use it in GitHub Desktop.
A custom DataAnnotationsModelValidatorProvider that injects IServiceProvider into ValidationContext
public class CustomDataAnnotationsModelValidatorProvider : DataAnnotationsModelValidatorProvider
{
private readonly IServiceProvider serviceProvider;
public CustomDataAnnotationsModelValidatorProvider(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
DataAnnotationsModelValidatorProvider.RegisterDefaultAdapterFactory(
(metadata, context, attribute) =>
new CustomDataAnnotationsModelValidator(serviceProvider, metadata, context, attribute)
);
DataAnnotationsModelValidatorProvider.RegisterDefaultValidatableObjectAdapterFactory(
(metadata, context) => new CustomValidatableObjectAdapter(serviceProvider, metadata, context)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment