Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Forked from benfoster/gist:3848727
Created October 8, 2012 12:11
Show Gist options
  • Save tugberkugurlu/3852195 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/3852195 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)
);
}
}
@txavier
Copy link

txavier commented Jan 17, 2017

Do you have example code of this working? I seem to be having a hard time getting this implemented in my DDD project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment