Created
May 17, 2017 09:13
-
-
Save hidegh/7ef215593bc7f40637befe1d55a457fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.Linq; | |
using System.Web.Http.Metadata; | |
using System.Web.Http.Validation; | |
using System.Web.Http.Validation.Providers; | |
using System.Web.Http.Validation.Validators; | |
namespace My.Web.Code.Binder | |
{ | |
/// <summary> | |
/// Inside Global.asax: | |
/// GlobalConfiguration.Configuration.Services.Clear(typeof(System.Web.Http.Validation.ModelValidatorProvider)); | |
/// GlobalConfiguration.Configuration.Services.Add(typeof(System.Web.Http.Validation.ModelValidatorProvider), new HttpDataAnnotationsModelValidatorProviderWithDependencyResolverSupport()); | |
/// </summary> | |
public class HttpDataAnnotationsModelValidatorProviderWithDependencyResolverSupport : DataAnnotationsModelValidatorProvider | |
{ | |
private readonly IServiceProvider serviceProvider; | |
public HttpDataAnnotationsModelValidatorProviderWithDependencyResolverSupport() : this (new HttpServiceProvider()) | |
{ | |
} | |
public HttpDataAnnotationsModelValidatorProviderWithDependencyResolverSupport(IServiceProvider serviceProvider) | |
{ | |
this.serviceProvider = serviceProvider; | |
RegisterDefaultAdapterFactory( | |
(validatorpProviders, attribute) => | |
new CustomDataAnnotationsModelValidator(serviceProvider, validatorpProviders, attribute) | |
); | |
RegisterDefaultValidatableObjectAdapterFactory( | |
(validatorProviders) => new CustomValidatableObjectAdapter(serviceProvider, validatorProviders) | |
); | |
} | |
// | |
// Internal (private) classes | |
// | |
private class HttpServiceProvider : IServiceProvider | |
{ | |
public object GetService(Type serviceType) | |
{ | |
return System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver.GetService(serviceType); | |
} | |
} | |
private class CustomDataAnnotationsModelValidator : DataAnnotationsModelValidator | |
{ | |
private readonly IServiceProvider serviceProvider; | |
public CustomDataAnnotationsModelValidator(IServiceProvider serviceProvider, IEnumerable<ModelValidatorProvider> validatorProviders, ValidationAttribute attribute) | |
: base(validatorProviders, attribute) | |
{ | |
this.serviceProvider = serviceProvider; | |
} | |
public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container) | |
{ | |
ValidationContext context = new ValidationContext(container ?? metadata.Model, serviceProvider, null); | |
context.DisplayName = metadata.GetDisplayName(); | |
ValidationResult result = Attribute.GetValidationResult(metadata.Model, context); | |
if (result != ValidationResult.Success) | |
{ | |
yield return new ModelValidationResult | |
{ | |
Message = result.ErrorMessage | |
}; | |
} | |
} | |
} | |
private class CustomValidatableObjectAdapter : ValidatableObjectAdapter | |
{ | |
private readonly IServiceProvider serviceProvider; | |
public CustomValidatableObjectAdapter(IServiceProvider serviceProvider, IEnumerable<ModelValidatorProvider> validatorProviders) : base(validatorProviders) | |
{ | |
this.serviceProvider = serviceProvider; | |
} | |
public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container) | |
{ | |
object model = metadata.Model; | |
if (model == null) | |
{ | |
return Enumerable.Empty<ModelValidationResult>(); | |
} | |
IValidatableObject validatable = model as IValidatableObject; | |
if (validatable == null) | |
{ | |
throw new InvalidOperationException(); | |
} | |
ValidationContext validationContext = new ValidationContext(validatable, serviceProvider, null); | |
return ConvertResults(validatable.Validate(validationContext)); | |
} | |
private IEnumerable<ModelValidationResult> ConvertResults(IEnumerable<ValidationResult> results) | |
{ | |
foreach (ValidationResult result in results) | |
{ | |
if (result != ValidationResult.Success) | |
{ | |
if (result.MemberNames == null || !result.MemberNames.Any()) | |
{ | |
yield return new ModelValidationResult { Message = result.ErrorMessage }; | |
} | |
else | |
{ | |
foreach (string memberName in result.MemberNames) | |
{ | |
yield return new ModelValidationResult { Message = result.ErrorMessage, MemberName = memberName }; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment