简体   繁体   English

流利Nhibernate

[英]Fluent Nhibernate

I am trying to use Fluent NHibernate with ASP.NET MVC 3 and I cannot seem to find a tutorial that explains how to get it all configured with ASP.NET MVC.我正在尝试将 Fluent NHibernate 与 ASP.NET MVC 3 一起使用,但我似乎找不到解释如何使用 ASP.NET MVC 进行全部配置的教程。 I mainly am wondering where to put the ISession building function and how to call it when I need it.我主要想知道ISession建筑function放在哪里,需要的时候怎么调用。 I see so many different implementations but none of them specify where they put this code.我看到了很多不同的实现,但没有一个指定他们把这段代码放在哪里。 So if anyone can explain how to get it all configured to work with MVC 3 or where a very detailed tutorial is, that would be greatly appreciated.因此,如果有人可以解释如何将其全部配置为与 MVC 3 一起使用,或者在哪里有非常详细的教程,那将不胜感激。

You can have a look at S#arp Architecture.你可以看看 S#arp Architecture。 It's a pretty solid architectural framework to work with ASP.NET MVC & NHibernate.这是一个非常可靠的架构框架,可与 ASP.NET MVC 和 NHibernate 一起使用。 They have a decent documentation and there's some sample projects to look at.他们有一个不错的文档,并且有一些示例项目可供查看。

http://www.sharparchitecture.net/ http://www.sharparchitecture.net/

If you are not using dependency injection you can try something like this如果你不使用依赖注入,你可以尝试这样的事情

public class MvcApplication : System.Web.HttpApplication
{

    public static ISession CurrentSession
    {
        get { return (ISession)HttpContext.Current.Items["current.session"]; }
        set { HttpContext.Current.Items["current.session"] = value; }
    }

    private static ISessionFactory _session_factory;
    private static object _session_factory_lock = new object();

    protected static ISessionFactory CreateSessionFactory()
    {

        if (_session_factory != null) return _session_factory;

        if (ConfigurationManager.ConnectionStrings["DbConnection"] != null)
        {
            var conn = ConfigurationManager.ConnectionStrings["DbConnection"];
            SqlServerSessionFactoryBuilder fb = new SqlServerSessionFactoryBuilder(conn.ConnectionString);
            _session_factory = fb.GetSessionFactory();
            return _session_factory;
        }

        throw new Exception("Cannot build session factory, connection string is not defined.");

    }


    public MvcApplication()
    {
        _session_factory = CreateSessionFactory();

        BeginRequest += delegate
        {

            try
            {
                CurrentSession = _session_factory.OpenSession();
            }
            catch (FluentConfigurationException ex)
            {
                logger.FatalException(string.Format("Error configuring the database {0}", ex.Message), ex);

            }

        };


        EndRequest += delegate
        {
            if (CurrentSession != null)
            {
                if (CurrentSession.Transaction != null && CurrentSession.Transaction.IsActive)
                {
                    logger.Error("Rolling back uncommited transaction");
                    CurrentSession.Transaction.Rollback();
                }
                else
                {
                    CurrentSession.Flush();           
                }
                CurrentSession.Close();
            }
        };

        Error += delegate
        {
            var error = this.Server.GetLastError();
            logger.ErrorException(string.Format("Unhandled error : {0}", error.Message), error);  
        };
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);


    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM