每个请求的 ServiceStack NHibernate 会话 [英] ServiceStack NHibernate Session per request

查看:28
本文介绍了每个请求的 ServiceStack NHibernate 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始构建一个应用程序,我打算使用 ServiceStack.只想知道处理 NHibernate ISession 或其他每个请求"上下文特定会话对象的最佳实践/好的方法是什么.

I am starting to build an app, and I'm planning to use ServiceStack. Just want to know what are the best practices/good approaches for handling NHibernate ISession or, other "per request" context specific session objects.

我想在 Ioc 中注册一个 ISessionFactory 就像:

I thought registering a ISessionFactory in the Ioc like:

container.Register<ISessionFactory>(sessionFactory);

并且在需​​要时获取一个新的 Session 对象...或者直接提供会话对象:

And when needed get a new Session object... Or maybe provide the session object directly:

container.Register<ISession>(c => sessionFactory.OpenSession()).ReusedWithin(ReuseScope.None);

或者通过 Global.asax BeginRequest 事件处理 ISession 和默认事务:

Or either handle the ISession and a default transaction via the Global.asax BeginRequest event:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    var session = factory.OpenSession();
    ITransaction itrans = session.BeginTransaction();
    Context.Items.Add("session", session);
    Context.Items.Add("trans", itrans);
}

所以,我有点迷茫,鉴于上述技术或类似技术,例如 EF 或其他 Rest-Services 框架,最佳实践是什么?

So, I am kind of lost, what are the best practices, given the above technologies, or similar ones, like EF or another Rest-Services framework?

提前致谢

推荐答案

请参阅此博客文章,了解如何最佳地结合使用 ServiceStack 和 NHibernate 的完整示例:

See this blog post for a complete example of how to optimally use ServiceStack and NHibernate together:

http://www.philliphaydon.com/2012/06/using-nhibernate-with-servicestack/这是上面帖子中使用的 AppHost 示例:

http://www.philliphaydon.com/2012/06/using-nhibernate-with-servicestack/ Here's the AppHost example used in the above post:

public class Global : HttpApplication
{
    public class SampleServiceAppHost : AppHostBase
    {
        private readonly IContainerAdapter _containerAdapter;
        public SampleServiceAppHost(ISessionFactory sessionFactory)
            : base("Service Stack with Fluent NHibernate Sample", typeof(ProductFindService).Assembly)
        {
            base.Container.Register<ISessionFactory>(sessionFactory);
        }

        public override void Configure(Funq.Container container)
        {
            container.Adapter = _containerAdapter;
        }
    }

    void Application_Start(object sender, EventArgs e)
    {
        var factory = new SessionFactoryManager().CreateSessionFactory();

        (new SampleServiceAppHost(factory)).Init();
    }
}

这篇关于每个请求的 ServiceStack NHibernate 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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