Henrique,

There are many options when starting an NHibernate project. I assume you are
using ASP.NET WebForms and have settled on mapping with Fluent NHibernate.
You want to implement session-per-request, which can be done a number of
ways.

Here's how this looks:
On application startup:

   1. Configure NHibernate following the FNH docs. This takes care of all
   the mapping magic. The fluent syntax ends by building a session factory.
   2. Hold on to the session factory for as long as your application is
   alive. It is your one and only session factory. This can be as simple as a
   static field in the global.asax, though I prefer to put it in my IoC
   container.

On request start:

   1. Using the session factory, open a session. This is cheap. It doesn't
   open a database connection. Don't worry about opening a session when it's
   not used.
   2. Make the session available for the lifetime of the request. You have
   two good options:
      1. Put it in your IoC container
      2. Use NHibernate contextual sessions, so you would call
      sessionFactory.GetCurrentSession() to find the session for this request.
      http://nhforge.org/doc/nh/en/index.html#architecture-current-session

On request end:

   1. Close or Dispose the session.


Thanks,
Jason

On Fri, Aug 13, 2010 at 12:19 AM, Henrique Cabral <hcab...@gmail.com> wrote:

> Hello everyone,
>
> What about this approach:
>
>
> http://nhforge.org/blogs/nhibernate/archive/2010/07/11/nhibernate-bootstrapper-unitofwork-and-sessionperrequest.aspx
>
> Versus the Global.asax? Or the Burrow framework?
>
> Cheers,
> Henrique
>
>
>
> On Thu, Aug 12, 2010 at 7:26 PM, Henrique Cabral <hcab...@gmail.com>
> wrote:
> > James,
> >
> > I think you're right, I will have to check how this is implemented.
> > Our group is formed mostly by Java developers, and ASP.NET is pretty
> > new to everyone. Thanks for the tip, will keep doing research on FNH.
> >
> > Cheers,
> > Henrique Cabral
> >
> > On Thu, Aug 12, 2010 at 4:31 PM, James Gregory <jagregory....@gmail.com>
> wrote:
> >> I think you're mistaken about where FNH is used, and when it does it's
> >> work. Fluent NHibernate should only be initialised upon application
> >> startup; in the case of a web application that'd be in your
> >> Global.asax or something similar. The SessionFactory is then stored
> >> (in a global, singleton, static, whatever) and reused throughout the
> >> application lifecycle. Sessions are only used to open database
> >> connections, there's no "mapping" taking place.
> >>
> >> On 8/12/10, Henrique Cabral <hcab...@gmail.com> wrote:
> >>> Hey everyone,
> >>>
> >>> Following this thread, I'm interested in knowing if anyone has any
> >>> further ideas about performance. My app is made in asp.net, and I'm
> >>> trying to follow the motto that "database connections are too
> >>> expensive", and each request that needs data opens a Session, then
> >>> maps objects, and so on.
> >>>
> >>> I'm prematurely reaching the conclusion that this is not viable.
> >>> Creating XML mappings for each build defeats the elegant purpose of
> >>> FNH. I started to think in Java (app server) and service tiers, since
> >>> database connections are not really that expensive to me yet.
> >>>
> >>> So, any thoughts?
> >>>
> >>> Cheers,
> >>> Henrique
> >>>
> >>> --
> >>> You received this message because you are subscribed to the Google
> Groups
> >>> "Fluent NHibernate" group.
> >>> To post to this group, send email to
> fluent-nhibern...@googlegroups.com.
> >>> To unsubscribe from this group, send email to
> >>> fluent-nhibernate+unsubscr...@googlegroups.com<fluent-nhibernate%2bunsubscr...@googlegroups.com>
> .
> >>> For more options, visit this group at
> >>> http://groups.google.com/group/fluent-nhibernate?hl=en.
> >>>
> >>>
> >>
> >> --
> >> You received this message because you are subscribed to the Google
> Groups "Fluent NHibernate" group.
> >> To post to this group, send email to fluent-nhibernate@googlegroups.com
> .
> >> To unsubscribe from this group, send email to
> fluent-nhibernate+unsubscr...@googlegroups.com<fluent-nhibernate%2bunsubscr...@googlegroups.com>
> .
> >> For more options, visit this group at
> http://groups.google.com/group/fluent-nhibernate?hl=en.
> >>
> >>
> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "Fluent NHibernate" group.
> To post to this group, send email to fluent-nhibern...@googlegroups.com.
> To unsubscribe from this group, send email to
> fluent-nhibernate+unsubscr...@googlegroups.com<fluent-nhibernate%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/fluent-nhibernate?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibern...@googlegroups.com.
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.

Reply via email to