相关疑难解决方法(0)

是否可以将不同的接口绑定到实现所有接口的同一个实例?

我有以下(简化)情况:我有两个接口

interface IAmAnInterface
{
    void DoSomething();
}
Run Code Online (Sandbox Code Playgroud)

interface IAmAnInterfaceToo
{
    void DoSomethingElse();
}
Run Code Online (Sandbox Code Playgroud)

和一个实现两者的类:

class IAmAnImplementation: IAmAnInterface, IAmAnInterfaceToo
{
    public IAmAnImplementation()
    {
    }

    public void DoSomething()
    {
    }

    public void DoSomethingElse()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我使用Ninject将同一个类绑定到两个接口.因为我想要使用相同IAmAnImplementationbeeing 实例,IAmAnInterface而且IAmAnInterfaceToo很明显我需要某种单例.我和ninject.extensions.namedscope一起,InScope()但没有成功.我的最后一次尝试是:

Bind<IAmAnImplementation>().ToSelf().InSingletonScope();
Bind<IAmAnInterface>().To<IAmAnImplementation>().InSingletonScope();
Bind<IAmAnInterfaceToo>().To<IAmAnImplementation>().InSingletonScope();
Run Code Online (Sandbox Code Playgroud)

但不幸的是,当我通过kernel.Get<IDependOnBothInterfaces>();它请求我的测试类的实例时实际上使用了不同的实例IAmAnImplementation.

class IDependOnBothInterfaces
{
    private IAmAnInterface Dependency1 { get; set; }
    private IAmAnInterfaceToo Dependency2 { get; set; }

    public IDependOnBothInterfaces(IAmAnInterface i1, …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection ninject inversion-of-control ninject-3

38
推荐指数
1
解决办法
5938
查看次数

绑定到多个接口时,防止Ninject多次调用Initialize

我们有一个具体的单件服务,它实现了Ninject.IInitializable2个接口.问题是服务Initialize-methdod被调用2次,只需要一次.我们使用的是.NET 3.5和Ninject 2.0.0.0.

Ninject中是否存在一种模式可以防止这种情况发生.两个接口都没有实现Ninject.IInitializable.服务类是:

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

模块看起来像这样:

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

Singleton是一个定义如下的扩展方法:

    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>());
    }
Run Code Online (Sandbox Code Playgroud)

当然我们可以将bool …

ninject

20
推荐指数
1
解决办法
5535
查看次数

在Ninject中将单例绑定到多个服务

我有一个问题,似乎与http://markmail.org/message/6rlrzkgyx3pspmnf中描述的问题非常相似,如果您使用不同的服务类型访问它,实际上创建的单个实例不仅仅是单个实例.

我正在使用最新版本的Ninject 2 for Compact Framework,我遇到的确切问题是,如果我将相同的提供程序方法绑定到:

Func<Service> serviceCreator = () => new Service(false);
kernel.Bind<IService>().ToMethod(serviceCreator).InSingletonScope();
kernel.Bind<Service>().ToMethod(serviceCreator).InSingletonScope();
Run Code Online (Sandbox Code Playgroud)

如果我将它们解析为IService和Service,它似乎创建了2个Service实例.

这在解析Service时会导致循环依赖性异常.

这是设计,还是一个bug?

.net ninject inversion-of-control ninject-2

9
推荐指数
2
解决办法
3018
查看次数