开发者

Converting Some Code From Ninject To StructureMap

I'm trying to use Steven's solution for Service Layer validation which is outlined in his reply to some question StackOverflow( Validation: How to inject A Model State wrapper with Ninject? ). Unfortunately, I'm relatively new to dependency injection and I know nothing about Ninject. In his code, he's using Ninject to do the IoC stuff, but in my project I am using StructureMap. I tried to convert the code into StructureMap syntax, but I failed... Anyway, here's the Ninject code:

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>();

And here's my attempt:

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<>));

});

I am getting this error:

No Default Instance defined for PluginFamily Sharwe.Services.Validation.Validator

So how do I fix this?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜