Hi everyone!

Just wanted to share this, for whoever might find it useful.

I have been working for about a year on a ASP MVC based application.
We have added quite a nice little infrastructure part to the project
which handles fetching data from our old Firebird system databases.
This is a lot of non-trivial mapping and requires no less than three
session factories for our different databases, all of which may have
to be used during the same request.

The SessionContext system in NHibernate turned out to be quite helpful
for storing the multiple sessions that we where handling during
requests, and I decided to exchange our own home grown session
handling system with the “official” way of doing it.

But I hit a snag. The WebSessionContext worked like a charm be course
it was able to handle multiple different factories without problems,
but unit tests failed be course our the ThreadStaticSessionContext
unlike WebSessionContext was not build on MapBasedSessionContext.

Solving this turned out to be fairly trivial. (Well, at least out
NUnit tests work now, I will leave it for others to judge how it would
hold up in a truly multithreaded application.)

First I wrote this new version of ThreadStaticSessionContext (Slightly
edited for new namespace):

using System;
using System.Collections;
using NHibernate.Context;
using NHibernate.Engine;

namespace Xapp.Infrastructure.NHibernateIntegration
{
    public class ThreadStaticMapSessionContext :
MapBasedSessionContext
    {
        [ThreadStatic]
        private static IDictionary _sessionFactoryMap;

                public ThreadStaticMapSessionContext(ISessionFactoryImplementor
factory) : base(factory) {}

                protected override IDictionary GetMap()
                {
            return _sessionFactoryMap;
                }

                protected override void SetMap(IDictionary value)
                {
                        _sessionFactoryMap = value;
                }
    }
}

And then just hooked that up in the config used for test.
I love how you can bend NHibernate to solve your issues when having to
integrate with legacy systems.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to