-->
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.  [ 6 posts ] 
Author Message
 Post subject: My DAL
PostPosted: Tue Feb 21, 2006 5:54 am 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
I was wondering if anyone could give me some tips on my data access layer \ DAOs or spot any ommisions. Im particually interested to know if i can make my DAOS static?

Firsty i have this singelton for setting up the session factory:

Code:
namespace vcf.code.framework
{

   public sealed class NHibernateSessionFactory
   {
      private static volatile ISessionFactory instance;
      private static object syncRoot = new Object();

      private NHibernateSessionFactory() {}

      public static ISessionFactory getFactory()
      {
      
         if (instance == null)
         {
            lock (syncRoot)
            {
               if (instance == null)
               {
                  Configuration cfg = new Configuration();
                  cfg.AddAssembly("vcf");
                  instance = cfg.BuildSessionFactory();
               }
                  
            }
         }

         return instance;
      
      }
   }

}



And then i have one baseDAO (fragment):

Code:
namespace vcf.code.framework
{

   public class BaseDAO
   {
      NHibernate.ISession session = null;
      NHibernate.ITransaction transaction = null;
   

      public BaseDAO()
      {
   
      }

      protected Object get(Object ob,int id)
      {   
         try
         {
            ISessionFactory factory = NHibernateSessionFactory.getFactory();
            
            session = factory.OpenSession();

            return session.Get(ob.GetType(),id);

         }
         finally
         {
            if(session != null)
               session.Close();
         }
      }

      

      protected int add(Object o)
      {   
         try
         {
            ISessionFactory factory = NHibernateSessionFactory.getFactory();
            
            session = factory.OpenSession();

            ITransaction transaction = session.BeginTransaction();

            int key =  Convert.ToInt32(session.Save(o));

            transaction.Commit();

            session.Flush();

            return key;
         }
         catch
         {
            if(transaction != null)
               transaction.Rollback(); // Error: we MUST roll back modifications
            throw; // Here, we throw the same exception so that it is handled (printed)
         }
         finally
         {
            if(session != null)
               session.Close();
         }

      }



      protected IList find(String namedQuery,String[] Params)
      {
         try
         {
            ISessionFactory factory = NHibernateSessionFactory.getFactory();
            session = factory.OpenSession();

            IQuery query = session.GetNamedQuery(namedQuery);
               
            for(int i=0;i<Params.Length;i++)
            {
               query.SetParameter(i,Params[i]);
               
            }

            return query.List();

         }
         finally
         {
            if(session != null)
               session.Close();
         }
      }



I then extend this for each of my hibernate objects:

Code:
namespace vcf.code.dao
{

   public class EmployerDAO : BaseDAO
   {
      private const string FIND_BY_NAME = "Employer.FindByName";
      private const string UNVALIDATED ="Employer.Unvalidated";

      public EmployerDAO()
      {
      }

      public IList findEmployer(String name)
      {
         String [] array = {"%"+name+"%"};
         return base.find(EmployerDAO.FIND_BY_NAME,array);

      }

      public int addEmployer(Employer e)
      {
         return base.add(e);
      }

      public Employer getEmployer(int i)
      {
         Employer e = new Employer();

         return (Employer)base.get(e,i);
      }


Top
 Profile  
 
 Post subject: Re: My DAL
PostPosted: Tue Feb 21, 2006 6:14 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
bosh wrote:
I was wondering if anyone could give me some tips on my data access layer \ DAOs or spot any ommisions. Im particually interested to know if i can make my DAOS static?


Seems like You do not intend to use transactions to tie bunch of additions/changes together? If You do not need them, then having static DAOs seems quite reasonable...

Gert


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 8:51 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
I think you should not use Transaction into the Add method because if you want to call Add to save master and details (for example), you can't commit just the master or just the detail... you have to commit both at the end of the task.


Top
 Profile  
 
 Post subject: Re: My DAL
PostPosted: Tue Feb 21, 2006 9:51 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
bosh wrote:
Code:
namespace vcf.code.framework
{
   public class BaseDAO
   {
      NHibernate.ISession session = null;
      NHibernate.ITransaction transaction = null;

      protected int add(Object o)
      {   
         try
         {
            [..]
            ITransaction transaction = session.BeginTransaction(); [1]
            [..]
         }
         catch
         {
            if(transaction != null) [2]
               transaction.Rollback(); // Error: we MUST roll back modifications
         [..]


I just noticed: You are having identically named local variable and instance fields. It is extremly bad practise: at [1] You declare local variable, but at [2] You rollback transaction declared at the instance scope.

Gert


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 6:42 am 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
Cheers gert....thanks for spotting that, that is very poor, i guess i got to change them to local if im making this static.


gert wrote:
Seems like You do not intend to use transactions to tie bunch of additions/changes together?


What do you mean by this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 7:01 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
bosh wrote:
gert wrote:
Seems like You do not intend to use transactions to tie bunch of additions/changes together?


What do you mean by this?


As You start and commit transaction inside the Add method? So, each addtion is in separate transaction...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.