Nhibernate 简单注入器-将构造函数参数传递给容器

Nhibernate 简单注入器-将构造函数参数传递给容器,nhibernate,simple-injector,Nhibernate,Simple Injector,我正在将我的应用程序从Ninject转换为更简单的注入器。我试图通过getInstance()将构造函数参数(在本例中为NHibernate会话)传递到容器中。在ninject中,这是通过使用以下方法实现的: return _kernel.Get<IRepository<T>>(new ConstructorArgument("mysession", GetMySession())); return _kernel.Get(新的构造函数参数(“mysession”,Ge

我正在将我的应用程序从Ninject转换为更简单的注入器。我试图通过getInstance()将构造函数参数(在本例中为NHibernate会话)传递到容器中。在ninject中,这是通过使用以下方法实现的:

return _kernel.Get<IRepository<T>>(new ConstructorArgument("mysession", GetMySession()));
return _kernel.Get(新的构造函数参数(“mysession”,GetMySession());
如何使用简单的喷油器来实现这一点?我有一个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());
            }
            ....
公共类NHSessionFactory:IMySessionFactory
{
私有只读ISessionFactory myNHSessionFactory;
公共NHSessionFactory(程序集[]myMappings){
this.myNHSessionFactory=InitNHibernate(myMappings);
MyLocator.MyKernel.Bind(typeof(IRepository)).To(typeof(NHRepository));
}
公共IMySession CreateSession()
{
返回新的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()));
}
公共类MyLocator
{
私有静态标准内核;
私人静态ISessionFactory sessionFactory;
静态MyLocator()
{
this.kernel=新的标准内核();
this.kernel.Load(新的DependencyInjector());
}
公共静态标准内核MyKernel
{
得到
{
返回这个.kernel;
}
}
公共静态ISession GetMySession()
{
....
返回这个.kernel.Get();
}
.....
公共静态IRepository GetRepository(),其中T:MyEntity
{
返回this.kernel.Get(新的构造函数参数(“mysession”,GetMySession());
}

请告诉我如何使用Simple Injector实现这一点。我已经了解到Simple Injector不允许通过检索方法(即getInstance)传递运行时值的开箱即用支持。有哪些替代方案?是否有注册选项(RegisterConditional)如果是这样,怎么做?

没有现成的支持将运行时值传递到
GetInstance
方法以完成自动连接过程。这是因为这通常是次优设计的标志。有,但通常我建议不要这样做

在您的情况下,即使在您当前的设计中,在对象构造期间(即使使用Ninject),似乎完全没有必要将会话作为运行时值传递。如果您只是在容器中注册
ISession
,您可以让Simple Injector(或Ninject)自动连接它,而无需将其作为运行时值传递。这甚至可以消除使用此
IMySessionFactory
抽象和实现的需要

您的代码可能开始如下所示:

Assembly[]myMappings=。。。
ISessionFactory myNHSessionFactory=InitNHibernate(myMappings);
容器.Register(myNHSessionFactory.OpenSession,lifesture.Scoped);
容器注册(typeof(IRepository),typeof(NHRepository));
使用此配置,您现在可以使用
container.GetInstance()
或使用
IRepository
作为构造函数参数解析存储库