绑定到多个接口时,防止Ninject多次调用Initialize [英] Prevent Ninject from calling Initialize multiple times when binding to several interfaces

查看:90
本文介绍了绑定到多个接口时,防止Ninject多次调用Initialize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个具体的单例服务,该服务实现了Ninject.IInitializable和2个接口.问题在于,当只需要一次服务时,服务Initialize-methdod被调用2次.我们正在使用.NET 3.5和Ninject 2.0.0.0.

We have a concrete singleton service which implements Ninject.IInitializable and 2 interfaces. Problem is that services Initialize-methdod is called 2 times, when only one is desired. We are using .NET 3.5 and Ninject 2.0.0.0.

Ninject中是否有某种模式可以防止这种情况的发生.两个接口均未实现Ninject.IInitializable.服务类别为:

Is there a pattern in Ninject prevent this from happening. Neither of the interfaces implement Ninject.IInitializable. the service class is:

public class ConcreteService : IService1, IService2, Ninject.IInitializable
{
    public void Initialize()
    {
        // This is called twice!
    }
}

模块如下:

public class ServiceModule : NinjectModule
{
    public override void Load()
    {
        this.Singleton<Iservice1, Iservice2, ConcreteService>();
    }
}

其中Singleton是这样定义的扩展方法:

where Singleton is an extension method defined like this:

    public static void Singleton<K, T>(this NinjectModule module) where T : K
    {
        module.Bind<K>().To<T>().InSingletonScope();
    }

    public static void Singleton<K, L, T>(this NinjectModule module) 
        where T : K, L
    {
        Singleton<K, T>(module);
        module.Bind<L>().ToMethod(n => n.Kernel.Get<T>());
    }

当然,我们可以将bool initialized-member添加到ConcreteService中,并且仅在为false时才进行初始化,但这似乎有些hack.而且,这需要在每个实现两个或多个接口的服务中重复相同的逻辑.

Of course we could add bool initialized-member to ConcreteService and initialize only when it is false, but it seems quite a bit of a hack. And it would require repeating the same logic in every service that implements two or more interfaces.

感谢所有答案!我从所有人那里都学到了一些东西! (我很难决定哪个标记正确).

Thanks for all the answers! I learned something from all of them! (I am having a hard time to decide which one mark correct).

我们最终创建了IActivable接口并扩展了ninject内核(它也很好地删除了代码级对ninject的依赖,尽管仍然保留了所有属性).

We ended up creating IActivable interface and extending ninject kernel (it also removed nicely code level dependencies to ninject, allthough attributes still remain).

推荐答案

Ninject 3

Ninject 3.0现在在绑定调用中支持多种泛型类型,您可以尝试在单个链式语句中轻松完成所要执行的操作.

Ninject 3

Ninject 3.0 now supports multiple generic types in the call to bind, what you are trying to do can be easily accomplished in a single chained statement.

kernel.Bind<IService1, IService2>()
      .To<ConcreteService>()
      .InSingletonScope();

注入2

您正在设置两个不同的绑定K => T和L => T.请求L的实例将返回T的瞬时实例.请求K的实例将返回T的单例实例.

Ninject 2

You are setting up two different bindings K=>T and L=>T. Requesting instances of L will return transient instances of T. Requesting K will return a singleton instance of T.

在Ninject 2.0中,对象作用域是绑定到作用域回调的每个服务接口.

In Ninject 2.0, an objects scope is per service interface bound to a scope callback.

有空的时候

Bind<IFoo>...InSingletonScope();
Bind<IBar>...InSingletonScope();

您要创建两个不同的作用域.

you are creating two different scopes.

你在说 绑定到IFoo将解析为返回的同一对象 .get被调用的时间." 和 绑定到IBar将解析为返回的同一对象 何时调用.Get."

You are saying "Binding to IFoo will resolve to the same object that was returned when .Get was called." and "Binding to IBar will resolve to the same object that was returned when .Get was called."

您可以将绑定链接在一起,但是您需要删除 IInitializable ,因为在激活实例时,它将导致重复的初始化:

you can chain the bindings together, but you will need to remove IInitializable as it will cause duplicate initialization when the instance is activated:

kernel.Bind<IBoo>()
      .To<Foo>()
      .InSingletonScope();
      .OnActivation(instance=>instance.Initialize());

kernel.Bind<IBaz>()
      .ToMethod( ctx => (IBaz) ctx.Kernel.Get<IBoo>() );

kernel.Bind<Foo>().ToSelf().InSingletonScope()
    .OnActivation(instance=>instance.Initialize());
kernel.Bind<IBaz>().ToMethod( ctx => ctx.Kernel.Get<Foo>() );
kernel.Bind<IBoo>().ToMethod( ctx => ctx.Kernel.Get<Foo>() );

,以便获得多个接口来解析为同一单例实例.当我看到这种情况时,我总是要问,如果您有一个承担两个职责的单身人士,您的对象是否做得太多?

in order to get multiple interfaces to resolve to the same singleton instance. When I see situations like this, I always have to ask, is your object doing too much if you have a singleton with two responsibilities?

这篇关于绑定到多个接口时,防止Ninject多次调用Initialize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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