简体   繁体   English

Ninject WhenInjectedIn等效于Simple Injector中的等效项

[英]Ninject WhenInjectedInto equivalent in Simple Injector

Mapping to constant value. 映射到恒定值。

This happens for example when you need to resolve an automapper IMapper instance, Example in Ninject would be 例如,当您需要解析自动映射器IMapper实例时,就会发生这种情况,Ninject中的Example将是

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

Bind different implementation based on the injecting type 根据注入类型绑定不同的实现

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 ) {
    }
}

In this case with Ninject we are able to inject a different concrete implementation based on the type we are injecting to. 在这种情况下,使用Ninject,我们可以根据要注入的类型来注入不同的具体实现。 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));

If you make a simple helper method, you can even mimic the Ninject API a bit more: 如果您使用简单的辅助方法,甚至可以模仿Ninject API:

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

Do note that since Simple Injector v4 it is impossible to make the binding contextual based on the service type of the consumer; 请注意,由于Simple Injector v4,因此不可能根据使用者的服务类型在上下文中进行绑定; 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. 请注意,尽管这个问题是普遍存在的,并且适用于所有DI容器,不仅适用于Simple Injector。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM