我有以下(简化)情况:我有两个接口
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
我正在使用以下代码注册组件:
StandardKernel kernel = new StandardKernel();
string currentDirectory = Path.GetDirectoryName(GetType().Assembly.Location)
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (!Path.GetDirectoryName(assembly.Location).Equals(currentDirectory))
continue;
foreach (var type in assembly.GetTypes())
{
if (!type.IsComponent())
continue;
foreach (var @interface in type.GetInterfaces())
kernel.Bind(@interface).To(type).InSingletonScope();
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个实现两个接口的类:
class StandardConsole : IStartable, IConsumer<ConsoleCommand>
Run Code Online (Sandbox Code Playgroud)
如果我解决了IStartable我得到一个实例,如果我解决IConsumer<ConsoleCommand>我得到另一个.
如何为两个接口获取相同的实例?
我们有一个具体的单件服务,它实现了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 …
我有例如2个interfases IInterface1和IInterface2,
public interface IInterface1 {...}
public interface IInterface2 {...}
Run Code Online (Sandbox Code Playgroud)
以及这些接口的一个实现ImplClass.
public class ImplClass : IInterface1, IInterface2 {...}
Run Code Online (Sandbox Code Playgroud)
我必须确保应用程序只有一个ImplClass实例,它将用作IInterface1和IInterface2.我正在使用ninject进行依赖注入.所以我的问题是:下面的代码是否符合我的要求?
...
Bind<IInterface1>().To<ImplClass>().Using<SingletonBehavior>();
Bind<IInterface2>().To<ImplClass>().Using<SingletonBehavior>();
...
Run Code Online (Sandbox Code Playgroud)
或者此代码将为eash接口创建2个ImplClass实例?
我想做一些类似的事情:
kernel.Bind<IBootTaskA>().To<BootTaskA>().InSingletonScope();
kernel.Bind<IBootTaskB>().To<BootTaskB>().InSingletonScope();
kernel.Bind<IBootTask>().To<IBootTaskA>();
kernel.Bind<IBootTask>().To<IBootTaskB>();
Run Code Online (Sandbox Code Playgroud)
所以我可以这样做:
public class Boot
{
public Boot(IBootTask[] bootTasks)
{
foreach(var task in bootTasks){task.Execute();}
}
}
Run Code Online (Sandbox Code Playgroud)
但我似乎无法将接口绑定到接口,有人知道解决方法吗?