如何在ninject中使用Unity的子容器注入?

时间:2013-08-23 20:13:21

标签: inversion-of-control ninject unity-container

我坚持使用相当于这种统一注册的ninject:

var container = new UnityContainer();
container.RegisterType<MyFactory>(new ContainerControlledLifetimeManager());

// resolve instances once per child container by MyFactory
container.RegisterType<IMyInstance>(new HierarchicalLifetimeManager(), 
                                    new InjectionFactory(c => c.Resolve<MyFactory>().GetMyInstance()));

已经尝试过这个没有任何影响:

var kernel = new StandardKernel();
kernel.Bind<MyFactory>().ToSelf().InSingletonScope();

// context preserving get should do the job for child containers, right?
// thought in call scope ist the equivalent to unity's hierarchical lifetime manager?
kernel.Bind<IMyInstance>().ToMethod(ctx => ctx.ContextPreservingGet<MyFactory>().GetMyInstance())
                          .InCallScope();

0 个答案:

没有答案