afunms

          My Software,My Dream—Forge a more perfect NMS product.

          perfect DAO design

          perfect DAO solution

          ------BaseDao------
          public abstract class BaseDao
          {  
             private static final int maxRow = 1000;
             protected Connection conn;
             protected String table;
             protected Class<? extends DtoInterface> dtoClass;
             protected JspPage jspPage;
             protected boolean insideConnection;
                     
             public BaseDao(Connection conn)
             { 
              init();
              if(conn==null)
              {
               this.conn = ConnectionManager.getConnection();
               insideConnection = true;        
              }
              else
              {
                  this.conn = conn;   
                  insideConnection = false;
              }
             }

             public BaseDao()
             { 
              init();
              this.conn = ConnectionManager.getConnection();
              insideConnection = true;   
             }

             public void close(Statement stmt,ResultSet rs)
             {
              try
                 {
                    if(rs!= null)
                       rs.close();
                    if(stmt!=null)
                       stmt.close();
                    /**
                     * if the connection is passed from outside
                     * do not close it.
                     */
                    if(insideConnection)
                    ConnectionManager.close(conn);
                 }
                 catch(SQLException se)
                 {   
                 }
             }

             protected abstract void init();
          }

          ------sub dao class example------
          public class ProducerDao extends BaseDao
          {
           public ProducerDao(Connection conn)
           {
            super(conn);
           } 
           
           protected void init()
              {
               super.dtoClass = ProducerDto.class;
               super.table = "nms_producer";
              } 

          ------client code-----
          For the first scenario

             ProducerDao dao = new ProducerDao(null);
          or ProducerDao dao = (ProducerDao)BeanFactory.newDao("producer");
             dao.method();

          For the second scenario
             Connection conn = ConnectionManager.createConnection();
             ProducerDao dao1 = new ProducerDao(conn);
             AnOtherDao dao2 = new AnOtherDao(conn);
             dao1.method1();
             dao2.method2();
             dao2.method3();
             ConnectionManager.close(conn);   

          or Connection conn = ConnectionManager.createConnection();
             ProducerDao dao = (ProducerDao)BeanFactory.newDao("producer",conn);
             AnOtherDao dao = (AnOtherDao)BeanFactory.newDao("another",conn);
             dao1.method1();
             dao2.method2();
             dao2.method3();
             ConnectionManager.close(conn);   

          posted on 2007-05-11 10:35 afunms 閱讀(142) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          My Links

          News

          留言簿(18)

          隨筆檔案

          相冊(cè)

          搜索

          最新評(píng)論

          閱讀排行榜

          主站蜘蛛池模板: 汝南县| 益阳市| 汝阳县| 南乐县| 通渭县| 安溪县| 犍为县| 四子王旗| 威远县| 获嘉县| 萝北县| 阜康市| 昔阳县| 当涂县| 涟源市| 利辛县| 周口市| 弋阳县| 和龙市| 崇礼县| 友谊县| 武山县| 大关县| 祁门县| 旬邑县| 清新县| 奈曼旗| 石泉县| 武义县| 南宁市| 宣化县| 德保县| 宣武区| 微山县| 平武县| 博客| 新巴尔虎左旗| 德保县| 聂荣县| 寻乌县| 双鸭山市|