So here is the problem. I have a shared class library that stores all repositories, domain and mapping files, so the library can be reused in other web applications. Now in this class library there is a piece of code that allows itself to create a factory session that will be used in its repositories. The code looks something like this.
private static ISessionFactory sessionFactory; private static Configuration configuration; public static Configuration Configuration() { if (configuration == null) { configuration = new Configuration().Configure(); } return configuration; } private static ISessionFactory SessionFactory { get { if (sessionFactory == null) { sessionFactory = Configuration().BuildSessionFactory(); } return sessionFactory; } } public static ISession GetCurrentSession() { if (!CurrentSessionContext.HasBind(SessionFactory)) { CurrentSessionContext.Bind(SessionFactory.OpenSession()); } return SessionFactory.GetCurrentSession(); }
So the repository calls the GetCurrentSession () method to get the ISession. Now this works fine, but I am worried that it may not be thread safe. Can someone help me with an approach that will help me make it thread safe.
A few notes:
I thought about setting up and creating a SessionFactory in global.asax web applications at the launch event, but the problem is that the shared class library in question is used in 20 different applications, so this would mean switching to all applications and updating the global.asax file. before I do this, I wanted to ask a question to see if there are other ways I can do this. So the library of ordinary classes can independently configure its SessionFactory and nevertheless be thread safe.
Thanks for reading this huge question. Will attach any help.
source share