Fluentvalidation inject business object to validator
I am using fluentvalidatio开发者_如何学Pythonn and use structuremap to inject validators for actions in asp.net mvc3. I have problems to inject my business objects to validator objects. I get an error like this:
{"StructureMap Exception Code: 202\nNo Default Instance defined for PluginFamily Suggestion.Biz.BO.ISubjectBO, Suggestion.Biz, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"}
How can i inject SubjectBO to SubjectValidator?
SubjectValidator:
public class SubjectValidator : AbstractValidator<SUBJECT>
{
private ISubjectBO _subjectBO;
public SubjectValidator(ISubjectBO subjectBO)
{
_subjectBO = subjectBO;
Custom(x=>{
if(!_subjectBO.CanUpdate(x))
return new ValidationFailure(null, "error msg");
return null;
});
}
}
SuggestionValidationRegistry:
public class SuggestionValidationRegistry : Registry
{
public SuggestionValidationRegistry()
{
For<IValidator<SUBJECT>>().Singleton().Use<SubjectValidator>();
ValidatorOptions.ResourceProviderType = typeof(ValidationResources);
}
}
StructureMapValidatorFactory:
public class StructureMapValidatorFactory : ValidatorFactoryBase
{
public override IValidator CreateInstance(Type validatorType)
{
return ObjectFactory.TryGetInstance(validatorType) as IValidator;
}
}
App_start:
ObjectFactory.Configure(cfg => cfg.AddRegistry(new SuggestionValidationRegistry()));
FluentValidationModelValidatorProvider.Configure(provider => provider.ValidatorFactory = new StructureMapValidatorFactory());
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
Yes i configure at app_start SuggestionBizRegistry :
public class BizRegistry : Registry
{
public BizRegistry()
{
For<IAwardTypeBO>().Use<AwardTypeBO>();
For<IQuoteBO>().Use<QuoteBO>();
For<ISubjectBO>().Use<SubjectBO>();
For<IContestBO>().Use<ContestBO>();
For<IApplicationBO>().Use<ApplicationBO>();
For<IScreenBO>().Use<ScreenBO>();
}
}
App_start:
protected void Application_Start()
{
ConfigureStructureMap();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
EFUnitOfWorkFactory.SetObjectContext(() => new SuggestionModel());
UnitOfWork.UnitOfWorkFactory = new EFUnitOfWorkFactory();
}
private static void ConfigureStructureMap()
{
IContainer container = new Container(x =>
{
x.For<IControllerActivator>().Use<StructureMapControllerActivator>();
x.AddRegistry(new BizRegistry());
});
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
ObjectFactory.Configure(cfg => cfg.AddRegistry(new SuggestionValidationRegistry()));
FluentValidationModelValidatorProvider.Configure(provider => provider.ValidatorFactory = new StructureMapValidatorFactory(container));
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
}
You have not configured constructor parameter ISubjectBO subjectBO
when configuring SubjectValidator
injection
You could write a custom validator factory as explained in this thread.
精彩评论