需要帮助了解Ninject是如何得到一个NHibernate的SessionFactory的实例成的UnitOfWork? [英] Need help understanding how Ninject is getting a Nhibernate SessionFactory instance into a UnitOfWork?

查看:96
本文介绍了需要帮助了解Ninject是如何得到一个NHibernate的SessionFactory的实例成的UnitOfWork?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,使用一些援助教程我设法一个NHibernate的会议连线到了我的资料库和我的仓库来使用Ninject我的控制器。但是,我不是抓什么Ninject是做AUTOMAGIC设置的一个peice的,并希望有人能解释一下。

So using some assistance from tutorials I have managed to wire up a Nhibernate session to my repositories and my repositories to my controllers using Ninject. However, there is one peice of the setup that I am not grasping the "automagic" of what Ninject is doing and was hoping someone could explain.

下面是我的 Ninject ModuleRepository 从NinjectModule继承了做所有的绑定。

Below is my Ninject ModuleRepository that inherits from NinjectModule that does all the binding.

public class ModuleRepository : NinjectModule
{
    public override void Load()
    {
        var helper = new NHibernateHelper(ConfigurationManager.ConnectionStrings[Environment.MachineName].ConnectionString);

        Bind<ISessionFactory>().ToConstant(helper.SessionFactory)
            .InSingletonScope();

        Bind<IUnitOfWork>().To<UnitOfWork>()
            .InRequestScope();
        Bind<ISession>().ToProvider<SessionProvider>()
            .InRequestScope();
        Bind<IRepository<Product>>().To<ProductRepository>();
        Bind<IRepository<Category>>().To<CategoryRepository>();
    }
}

下面是在的UnitOfWork类

public class UnitOfWork : IUnitOfWork
{
    private readonly ISessionFactory _sessionFactory;
    private readonly ITransaction _transaction;
    public ISession Session { get; private set; }

    public UnitOfWork(ISessionFactory sessionFactory)
    {
        _sessionFactory = sessionFactory;

        //Open Session
        Session = _sessionFactory.OpenSession();
        Session.FlushMode = FlushMode.Auto;
        _transaction = Session.BeginTransaction(IsolationLevel.ReadCommitted);
    }

    public void Commit()
    {
        if (!_transaction.IsActive)
            throw new InvalidOperationException("There is no active Transaction");
        _transaction.Commit();
    }

    public void Rollback()
    {
        if (_transaction.IsActive)
            _transaction.Rollback();
    }

    //Close open session
    public void Dispose()
    {
        Session.Close();
    }
}

所以,我明白,我们正在创建一个用于创建NHibernate的的SessionFactory对象的单个实例常量实例。下面是从一个包装的每个工作单元在事务中的UnitOfWork对象返回会话的SessionProvider类。

So I understand that we are creating a single instance constant instance of the object that creates a Nhibernate SessionFactory. Below is the SessionProvider class which returns the session from the UnitOfWork object that wraps each unit of work in a transaction.

SessionProvider

public class SessionProvider : Provider<ISession>
{
    protected override ISession CreateInstance(IContext context)
    {
        var unitOfWork = (UnitOfWork)context.Kernel.Get<IUnitOfWork>();
        return unitOfWork.Session;
    }
}

该库采取的ISession在其构造。但我没有看到的是如何UnitOfWork.Session是获取传递给我的资料库中的会话?

The Repositories take a ISession in their constructor. But what I am not seeing is how the UnitOfWork.Session is the "session" that gets passed to my repositories?

在理解这个任何帮助将是巨大的。谢谢你。

Any help in understanding this would be great. Thanks.

推荐答案

该使用绑定:

Bind<ISession>().ToProvider<SessionProvider>().InRequestScope();

指出,它应该保持请求范围。这意味着,Ninject将缓存ISession的所有请求全部的Htt prequest中 - 因此所有的类被注入(或明确得到一个实例)将使用的ISession的同一个实例。在您的配置同样适用于IUnitOfWork。

states that it should maintain Request Scope. That means that Ninject will cache all requests for ISession during the entire HttpRequest - so all classes being injected (or explicitly getting an instance) will be using the same instance of the ISession. In your configuration the same goes for the IUnitOfWork.

请参阅这个帖子按内特Kohari在Ninject不同的范围对象的描述。

See this post by Nate Kohari for descriptions of the different scope objects in Ninject.

这篇关于需要帮助了解Ninject是如何得到一个NHibernate的SessionFactory的实例成的UnitOfWork?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆