我正在尝试使用Steven的服务层验证解决方案,这在他对一些问题StackOverflow(Validation: How to inject A Model State wrapper with Ninject? )的回复中有所概述。不幸的是,我对依赖注入相对较新,我对Ninject一无所知。在他的代码中,他使用Ninject来完成IoC的工作,但在我的项目中我使用的是StructureMap。我试图将代码转换为StructureMap语法,但我失败了...无论如何,这是Ninject代码:
Func<Type, IValidator> validatorFactory = type =>
{
var valType = typeof(Validator<>)
.MakeGenericType(entity.GetType());
return (IValidator)kernel.Get(valType);
};
kernel.Bind<IValidationProvider>().ToConstant(
context => new ValidationProvider(validatorFactory));
kernel.Bind<Validator<Product>>().To<ProductValidator>();
这是我的尝试:
Func<Type, IValidator> validatorFactory = type =>
{
var valType = typeof(Validator<>)
.MakeGenericType(type.GetType());
return (IValidator) ObjectFactory.GetInstance(valType);
};
container.Configure(x =>
{
x.For<IValidationProvider>()
.Use(() => new ValidationProvider(validatorFactory));
x.For(typeof(Validator<>)).Use(typeof (NullValidator<>));
});
我收到此错误:
未定义默认实例 PluginFamily Sharwe.Services.Validation.Validator
那么我该如何解决这个问题呢?