-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: ASP.NET Provider pattern thread safety help
PostPosted: Wed Jan 25, 2006 9:20 am 
Newbie

Joined: Wed Nov 23, 2005 5:44 am
Posts: 7
I am trying to come up with a general pattern I can use to provide ORP Framework services to my business logic layer as well as trying to implement thread-safe custom NHibernate powered Providers for ASP.NET 2.0's new provider model (Membership Provider, Profile Provider etc)

As I am not a high end super coder I decided to do the following and was wondering what you guys think of this and if you could give me some advice.
Code:
namespace FusionRecruit.OrpLayer
{
    public interface IOrpFrameworkProvider
    {
        ISessionFactory GetSessionFactory();
    }



    public class NHibernateOrpProvider : IOrpFrameworkProvider
    {
        private static ISessionFactory sessionFactory = null;

        #region IOrpFrameworkProvider Members

        public ISessionFactory GetSessionFactory()
        {
            // Running an ASP.NET Application?
            if (System.Web.HttpContext.Current != null)
            {
                if (System.Web.HttpContext.Current.Application["HibernateSessionFactory"] == null)
                {
                    System.Web.HttpContext.Current.Application["HibernateSessionFactory"] = BuildHibernateSessionFactory();
                }

                return (ISessionFactory)System.Web.HttpContext.Current.Application["HibernateSessionFactory"];
            }
            else
            {
                if (sessionFactory == null)
                {
                    sessionFactory = BuildHibernateSessionFactory();
                }

                return sessionFactory;

            }

        }

        #endregion

        private static ISessionFactory BuildHibernateSessionFactory()
        {
            // Tell the configuration where our assembly is
            Configuration cfg = new Configuration();
            cfg.AddAssembly("FusionRecruit.Pocos");
            ISessionFactory factory = cfg.BuildSessionFactory();
            return factory;
        }
    }



    public abstract class OrpProviderFactory
    {
        public static IOrpFrameworkProvider GetOrpProvider()
        {
            return new NHibernateOrpProvider();
        }
    }

}



I am trying to encapsulate NHibernate through an interface IOrpFrameworlProvider which will provide object relational persistence services to my business logic layer and the ASP.NET custom Providers I am writing.

With the custom providers MS insist that they must be thread safe, and I was wondering what an impact this would have on my design, in a typical method of my custom MembershipProvider class I have something like this:

Code:
            IOrpFrameworkProvider orpProvider =  OrpProviderFactory.GetOrpProvider();
            ISessionFactory orpSessionFactory = orpProvider.GetSessionFactory();
            ISession orpSession = orpSessionFactory.OpenSession();

                    IQuery membershipQuery = orpSession.CreateQuery("from FusionRecruit.AspNetProviders.Pocos.AspNetMembership as aspNetMembership where aspNetMembership.User.UserId ='" + uid + "'");
                    AspNetMembership membershipCheck = (AspNetMembership)membershipQuery.UniqueResult();

                    if (membershipCheck != null && ((int)membershipCheck.User.UserId) != 0)
                    {
                        status = MembershipCreateStatus.DuplicateUserName;
                        return null;
                    }

            orpSession.Close();


I ussually use a session across a few queries and operations, not sure if this is advisable but I thought it might be best to keep a session open for a series of business logic where I might request a few lists from the db, single objects or just values, rather than openeing a session and closing sessions for every operation.

Anyway, if anyone can point out where my code should be thread safe I would much appreciate it, I am very new to threading and thread-safety so its a big learning curve for me!

regards

Fluxtah


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 9:29 am 
Newbie

Joined: Wed Nov 23, 2005 5:44 am
Posts: 7
Oh, by the way, this is what my business logic layer looks like in use:

Code:
    protected ContactInfo contactInfo = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        IBusinessLogicSession session = BusinessLogicSessionFactory.GetSession();
        contactInfo = session.ContactInfo.Find(1);
        Repeater1.DataSource = contactInfo.AddressRecords;
        this.DataBind();

        session.Close();

    }


I think I might be going factory crazy here however I think it reads really well and provides the page with the databinding it needs and allows me to control the underlying NHibernate sessions, the main goal for me was to expose only NHibernates interfaces to my business logic layer and expose only the business object layers interfaces to my presentation layer, what do you think?

best regards

Fluxtah


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.