将Ninject ToConstant转换为简单的注入器实现

时间:2018-05-03 14:11:02

标签: c# dependency-injection ninject simple-injector

使用ToConstant从此Ninject代码中Simpleinjection中的等效代码是什么。

Func<Type, IValidator> validatorFactory = type =>
{
    var valType = typeof(Validator<>).MakeGenericType(type);
    return (IValidator)kernel.Get(valType);
};

kernel.Bind<IValidationProvider>().ToConstant(new ValidationProvider(validatorFactory));

1 个答案:

答案 0 :(得分:0)

RegisterInstance是与Ninject ToConstant等效的简单注入者:

Func<Type, IValidator> validatorFactory = type =>
{
    var valType = typeof(Validator<>).MakeGenericType(type);
    return (IValidator)container.GetInstance(valType);
};

container.RegisterInstance<IValidationProvider>(new ValidationProvider(validatorFactory));