Ninject whenInjectedIn等效于Simple Injector中的等效项 [英] Ninject WhenInjectedInto equivalent in Simple Injector

查看:86
本文介绍了Ninject whenInjectedIn等效于Simple Injector中的等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

映射为恒定值.

例如,当您需要解析自动映射器IMapper实例时,就会发生这种情况,Ninject中的Example将是

This happens for example when you need to resolve an automapper IMapper instance, Example in Ninject would be

var config = new MapperConfiguration( cfg => {
    cfg.AddProfile( new MyMapperConfiguration() );
} );
Bind<MapperConfiguration>().ToConstant( config ).InSingletonScope();
Bind<IMapper>().ToConstant( config.CreateMapper() );

根据注入类型绑定不同的实现

当一组通用类依赖于一个通用接口但具体实现应该有所不同时,会发生这种情况.例子

This happens when a set of common classes depends on a common interface but the concrete implementation should be different. Example

public interface ICardService  {}

public class TypeACardService : ICardService, ITypeACardService {

    public TypeACardService( ICardValidator validator ) {
    }
}

public class TypeBCardService : ICardService, ITypeBCardService {

    public TypeBCardService( ICardValidator validator ) {
    }
}

在这种情况下,使用Ninject,我们可以根据要注入的类型来注入不同的具体实现.例子

In this case with Ninject we are able to inject a different concrete implementation based on the type we are injecting to. Example

Bind<ICardValidator>().To<TypeAValidator>().WhenInjectedInto( typeof( ITypeACardService ) )
Bind<ICardValidator>().To<TypeBValidator>().WhenInjectedInto( typeof( ITypeBCardService ) )

推荐答案

等效于此的简单注入器是:

The Simple Injector equivalent to this is:

container.RegisterConditional<ICardValidator, TypeAValidator>(
    c => c.Consumer.ImplementationType == typeof(TypeACardService));
container.RegisterConditional<ICardValidator, TypeBValidator>(
    c => c.Consumer.ImplementationType == typeof(TypeBCardService));

如果您使用简单的辅助方法,甚至可以模仿Ninject API:

If you make a simple helper method, you can even mimic the Ninject API a bit more:

// Helper method:
private static bool WhenInjectedInto<TImplementation>(PredicateContext c) =>
    c => c.Consumer.ImplementationType == typeof(TImplementation);

// Registrations
c.RegisterConditional<ICardValidator, TypeAValidator>(WhenInjectedInto<TypeACardService>);
c.RegisterConditional<ICardValidator, TypeBValidator>(WhenInjectedInto<TypeBCardService>);

请注意,由于Simple Injector v4,基于消费者的服务类型 不可能进行绑定上下文;您将为此使用实现类型,并且如果您确实基于服务类型进行注册,则必须查询"实现类型以查看其是否实现了给定的接口.如此处所述,直接对服务类型执行此操作可能会导致难以跟踪错误.请注意,尽管这个问题是普遍存在的,并且适用于所有DI容器,不仅适用于Simple Injector.

Do note that since Simple Injector v4 it is impossible to make the binding contextual based on the service type of the consumer; you will have use the implementation type for this and if you really make the registration based on the service type, you will have to 'query' the implementation type to see if it implements the given interface. Doing this directly on service types can leads to hard to track bugs, as explained here. Note though that this problem is universal and holds for all DI Containers, not only for Simple Injector.

这篇关于Ninject whenInjectedIn等效于Simple Injector中的等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆