ServiceStack NHibernate Session Per Request

I am starting to create an application, and I plan to use ServiceStack. I just want to know what are the best methods / good approaches for handling NHibernate ISession or other contextual session request objects.

I was thinking of registering ISessionFactory in Ioc like:

container.Register<ISessionFactory>(sessionFactory); 

And if necessary, get a new Session object ... Or you can directly provide a session object:

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

Or either handle ISession and the default transaction through 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); } 

So, I lost, what are the best practices given the above technologies or the like, for example, EF or other Rest-Services infrastructure?

Thanks in advance

+8
servicestack nhibernate
source share
3 answers

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/ Here is an example of AppHost used in the message above:

 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(); } } 
+3
source share

Creating a session for each request using the HttpHandler is the most common way to do this that I found. Ayende explained that creating a session is really easy. http://ayende.com/blog/4123/what-is-the-cost-of-opening-a-session

Ayende actually has a number of posts where he is gradually building a solution for accessing data. Each article explains why he did what he did, and what problems need to be solved with the steps taken so far. Start here: http://ayende.com/blog/4803/refactoring-toward-frictionless-odorless-code-the-baseline

Finally, http://nhforge.org/blogs/nhibernate/archive/2011/03/03/effective-nhibernate-session-management-for-web-apps.aspx

All of the above are session options for each request. Common to all is the lack of the need to manually worry about creating a session / transaction. They automatically commit or roll back transactions.

+4
source share

I know this is an old question, but I decided that I would go and show everyone who is still interested in the alternative answer how we did it.

We use ServiceRunner in the new ServiceStack API in this way:

 public class BaseServiceRunner<TRequest> : ServiceRunner<TRequest> { public BaseServiceRunner(AppHost appHost, ActionContext actionContext) : base(appHost, actionContext) { } public override void OnBeforeExecute(IRequestContext requestContext, TRequest request) { var req = request as MyRequestType; if(req == null) base.OnBeforeExecute(requestContext, request); var factory = TryResolve<NHibernate.ISessionFactory>(); var session = factory.OpenSession(); var trans = session.BeginTransaction(IsolationLevel.ReadCommitted); requestContext.SetItem("session", session); requestContext.SetItem("transaction", trans); } public override object OnAfterExecute(IRequestContext requestContext, object response) { var trans = requestContext.GetItem("transaction") as ITransaction; if (trans != null && trans.IsActive) trans.Commit(); var session = requestContext.GetItem("session") as ISession; if (session != null) { session.Flush(); session.Close(); } return base.OnAfterExecute(requestContext, response); } public override object HandleException(IRequestContext requestContext, TRequest request, Exception ex) { var req = request as MyRequestType; if(req != null) { var trans = requestContext.GetItem("transaction") as ITransaction; if (trans != null && trans.IsActive) trans.Rollback(); var session = requestContext.GetItem("session") as ISession; if (session != null) { session.Flush(); session.Close(); } } return base.HandleException(requestContext, request, ex); } } 
+3
source share

All Articles