简体   繁体   English

Ninject静态属性可以绑定吗?

[英]Is possible Ninject static property binding?

Does ninject works for static property bindings? ninject是否适用于静态属性绑定?

I set IEventHandlerFactory with Ninject, 我用Ninject设置了IEventHandlerFactory

public class ServiceModule: NinjectModule
{
    public override void Load()
    {
      Kernel.Bind<IEventHandlerFactory>().To<EventHandlerFactoryService>();
    }
}

And my static class 还有我的静态课

public static class DomainEvents
{
    public static IEventHandlerFactory EventHandlerFactory { get; set; }

    public static void Raise<T>(T domainEvent) 
    {
        EventHandlerFactory
            .GetDomainEventHandlersFor(event)
            .ForEach(h => h.Handle(event));
    }
}

But this does not bind to static property. 但这并不绑定到静态属性。

DomainEvents.EventHandlerFactory is Null DomainEvents.EventHandlerFactory为Null

Is there any way to bind property? 有什么方法可以绑定财产吗?

Since ninject doesn't have a concept of dividing "creating all bindings" and "using the kernel", there's of course no extension point for "tell me when the kernel is done with all bindings so i can do something". 由于ninject没有将“创建所有绑定”和“使用内核”分开的概念,因此“在内核完成所有绑定后告诉我,以便我可以做某事”当然没有扩展点。 With a static class it also doesn't make sense to request it from the kernel. 对于静态类,从内核请求它也是没有意义的。

So the obvious answer is: no. 因此,显而易见的答案是:不。

Of course, the obvious solution is just to extend your code where you're done building up the kernel (probably close to where you do var kernel = new StandardKernel() ) with a call like 当然,显而易见的解决方案是使用如下调用将代码扩展到完成内核构建的位置(可能接近您执行var kernel = new StandardKernel()

DomainEvents.EventHandlerFactory = kernel.Get<IEventHandlerFactory>();

Alternative 1 - tying initialization to activation of another type 备选方案1-将初始化绑定到另一种类型的激活

If that initialization should be tied to the activation of another type, let's say IFoo , you can also do: 如果该初始化应该与另一种类型的激活联系在一起,那么假设IFoo ,您还可以执行以下操作:

kernel.Bind<IFoo>()
      .To<Foo>()
      .InSingletonScope()
      .OnActivation(x => 
          DomainEvents.EventHandlerFactory = kernel.Get<IEventHandlerFactory>());

Alternative 2 - tying initialization to loading of a NinjectModule 备选方案2-将初始化绑定到NinjectModule加载

You can sublcass NinjectModule and in it's Load you can initialize the static property. 您可以订阅NinjectModule并在其Load中初始化static属性。 This works in case you can make sure the module is only loaded after the kernel is sufficiently initialized to create an IEventHandlerFactory . 如果您可以确保仅在内核充分初始化以创建IEventHandlerFactory之后才加载模块,则此方法IEventHandlerFactory

Disclaimer 放弃

Both alternatives probably suck because their not clear and not straight forward. 两种选择都可能很烂,因为它们不清楚和不直接。 They hide a dependency deep in some place. 他们在某个地方深藏了依赖性。 I'd only use one of these if the first approach is not feasible, for example because your writing a plugin and there's no extension point post-kernel initialization. 如果第一种方法不可行,我只会使用其中的一种,例如,因为您编写了插件并且没有扩展点内核后的初始化。

Do you really need DomainEvents class to be static? 您真的需要DomainEvents类是静态的吗?

Maybe you can do as follows. 也许您可以执行以下操作。 Raise will still be static though. 提升仍然是静态的。

You will have to call kernel.Get though. 您将必须调用kernel.Get。

public class DomainEvents
{
    public static IEventHandlerFactory EventHandlerFactory { get; set; }

    public DomainEvents(IEventHandlerFactory factory)
    {
         EventHandlerFactory = factory;
    }

    public static void Raise<T>(T domainEvent) 
    {
        EventHandlerFactory
            .GetDomainEventHandlersFor(event)
            .ForEach(h => h.Handle(event));
    }
}

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

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