【发布时间】:2015-09-22 18:51:10
【问题描述】:
我正在将我的应用程序从 Ninject 转换为 Simpler Injector。我正在尝试通过 getInstance() 将构造函数参数(在本例中为 NHibernate 会话)传递到容器中。在ninject中,这是通过使用以下方式完成的:
return _kernel.Get<IRepository<T>>(new ConstructorArgument("mysession", GetMySession()));
如何使用 Simple Injector 完成此任务?我有 HibernateFactory 类,其中与存储库完成绑定:
public class NHSessionFactory : IMySessionFactory
{
private readonly ISessionFactory myNHSessionFactory;
public NHSessionFactory(Assembly[] myMappings){
this.myNHSessionFactory = InitNHibernate(myMappings);
MyLocator.MyKernel.Bind(typeof(IRepository<>)).To(typeof(NHRepository<>));
}
public IMySession CreateSession()
{
return new NHSession(this.myNHSessionFactory.OpenSession());
}
....
MyLocator 类有以下内容:
public class MyLocator
{
private static StandardKernel kernel;
private static ISessionFactory sessionFactory;
static MyLocator()
{
this.kernel = new StandardKernel();
this.kernel.Load(new DependencyInjector());
}
public static StandardKernel MyKernel
{
get
{
return this.kernel;
}
}
public static ISession GetMySession()
{
....
return this.kernel.Get<ISession>();
}
.....
public static IRepository<T> GetRepository<T>() where T : MyEntity
{
return this.kernel.Get<IRepository<T>>(new ConstructorArgument("mysession", GetMySession()));
}
请告诉我如何使用 Simple Injector 实现这一目标。我已经读过 Simple Injector 不允许通过检索方法(即 getInstance)传递运行时值的开箱即用支持。有哪些替代方案?注册(RegisterConditional)是否有选项,如果有,怎么做?
【问题讨论】:
标签: nhibernate simple-injector