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) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          My Links

          News

          留言簿(18)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 临清市| 叙永县| 岳池县| 新乐市| 秀山| 淄博市| 濮阳市| 陇川县| 大同市| 浦城县| 海原县| 沧州市| 东城区| 张北县| 通河县| 施秉县| 遂平县| 射阳县| 阳高县| 合山市| 岳池县| 长乐市| 融水| 炎陵县| 绩溪县| 雅江县| 遂川县| 呈贡县| 密山市| 米脂县| 交城县| 越西县| 信丰县| 永善县| 略阳县| 西和县| 江源县| 全椒县| 辉县市| 桂阳县| 新兴县|