[英]Equivalent Ninject code with Simple Injector
Decided to switch from Ninject to Simple Injector and one problem i'm having here is trying to convert this code into Simple Injectors Equivalent: 决定从Ninject切换到Simple Injector,我在这里遇到的一个问题是尝试将此代码转换为Simple Injectors等效:
var resolver = new SomeResolver(container);
container.Rebind(typeof(IHubConnectionContext<dynamic>))
.ToMethod(context =>
resolver.Resolve<IConnectionManager>().GetHubContext<PlanHub>().Clients
).WhenInjectedInto<PlanHubService>();
Rebind
in Ninject is equivalent to using Register
with AllowOverridingRegistrations set to true
in Simple Injector. Rebind
相当于在Simple Injector中使用Register
with AllowOverridingRegistrations设置为true
。 WhenInjectedInto
in Ninject is equivalent to the use of RegisterConditional in Simple Injector. WhenInjectedInto
相当于在Simple Injector中使用RegisterConditional 。 ToMethod
in Ninject typically equals to using Register<T>(Func<T>)
in Simple Injector, but in conjunction with RegisterConditional
you will have to fall back to creating a Registration
using Lifestyle.CreateRegistration<T>(Func<T>, Container)
. ToMethod
通常等于在Simple Injector中使用Register<T>(Func<T>)
,但与RegisterConditional
结合使用时,您必须回退到使用Lifestyle.CreateRegistration<T>(Func<T>, Container)
创建Registration
Lifestyle.CreateRegistration<T>(Func<T>, Container)
。 You can therefore rewrite your binding to the following Simple Injector code: 因此,您可以将绑定重写为以下Simple Injector代码:
container.RegisterConditional(
typeof(IHubConnectionContext<dynamic>),
Lifestyle.Transient.CreateRegistration(
() => container.GetInstance<IConnectionManager>().GetHubContext<PlanHub>().Clients,
container),
WhenInjectedInto<PlanHubService>);
Where WhenInjectedInto<T>
is a custom helper method defined as follows: WhenInjectedInto<T>
是定义如下的自定义帮助方法:
private static bool WhenInjectedInto<T>(PredicateContext context) =>
typeof(T).IsAssignableFrom(context.Consumer.ImplementationType);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.