Announcement Announcement Module
Collapse
No announcement yet.
Facade pattern with DAO Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Facade pattern with DAO

    Hi, all

    I have problem with facade implementation.

    Firstly I had one table but now I have 15 tables in my DB and I generate pojo with netbeans.

    Firstly I had BasicDaoMethods interface with Student( save, get, getById ... ) methods. And for example HibernateDaoMethods class which implement this interface.
    I have a something like Object Manager class like this


    Code:
    public class ObjectManagerFactory {
    
        public static final Integer XML_DAO = 1;
        public static final Integer HIBERNATE_DAO = 3;
        private static BasicDaoMethods instance;
        private static HashMap<Integer, BasicDaoMethods> map;
    
    
        static {
            setInstance((BasicDaoMethods) null);
            map = new HashMap<Integer, BasicDaoMethods>();
            map.put(XML_DAO, XmlBasicDaoMethods.getInstance());
            map.put(HIBERNATE_DAO, HibernateBasicDaoMethods.getInstance());
        }
    
        private ObjectManagerFactory() {
    
        }
    
        public static BasicDaoMethods getInstance(Integer type) {
            log.info("getInstance( Integer ["+type+"] ) invocation");
            Entry<Integer, BasicDaoMethods> e = null;
            Iterator<Entry<Integer, BasicDaoMethods>> i = map.entrySet().iterator();
            while (i.hasNext()) {
                e = i.next();
                if (type == e.getKey()) {
                    instance = e.getValue();
                }
            }
            return instance;
        }
        
        public static BasicDaoMethods getInstance(String type, SessionFactory sessionFactoryBean) {
            getInstance(getType(type));
            if(getType(type) == HIBERNATE_DAO) {
                ((HibernateBasicDaoMethods)instance).setSessionFactory(sessionFactoryBean);
            }
            return instance;
        }
    
        public static BasicDaoMethods getInstance(String type) {
            log.info("getInstance( String ["+type+"]) invocation");
            return getInstance(getType(type));
        }
    
        public static BasicDaoMethods getInstance() {
            log.info("getInstance() invocation");
            if (instance == null) {
                setInstance(getInstance(DB_DAO));
                log.info("getInstance() instance was null");
            }
            return instance;
        }
    
    
        public static void setInstance(BasicDaoMethods aBdm) {
            log.info("setInstance( BasicDaoMethods ["+aBdm+"]) invocation");
            instance = aBdm;
        }
    
        public static Integer getType(String t) {
    if (t.equalsIgnoreCase("XML_DAO") || t.equalsIgnoreCase(XML_DAO.toString())) {
                return XML_DAO;
            } else if (t.equalsIgnoreCase("HIBERNATE_DAO") || t.equalsIgnoreCase(HIBERNATE_DAO.toString())) {
                return HIBERNATE_DAO;
            }
            return DB_DAO;
        }
    
    }
    I have the bean FACADE like
    Code:
       <bean id="facade" class="org.zethost.dao.ObjectManagerFactory"
            factory-method="getInstance" >
                <constructor-arg>
                    <value>HIBERNATE_DAO</value>
                </constructor-arg>
                <!-- sessionFactoryBean inject only if HIBERNATE_DAO used -->
                <constructor-arg>
                    <ref bean="sessionFactoryBean"/>
                </constructor-arg>
                <!-- //  -->
        </bean>
    which I inject in evry controller in my application. This simple solution
    was good enaugh until I had one or two table in data base

    but If I have 15 tables and 15 pojos I need many methods like save for User, getById for User, save for Product .... and so on

    the question is How can I design "good"
    facade which I can inject to evry controller in this ( 15 tables or more ) case
    One guy told me that I should throw all this save, get methods to concern difrent pojos into BasicDaoMethods interface and HibernateBasicDaoMethods class ( implementation). But is this good solution to create a few dozen methods to concern many pojo objects in one class like HibernateBasicDaoMethods and hold this in ObjectManagerFactory and inject all this into controller ?

    Maybe there are other approach. Could somebody help me.

    Sorry for my english.

  • #2
    mmmm what is the problem with my post, this is bad question or bad language or maybe noone know the answer or maybe all of above or something other
    please could yo tell me this at least ?

    Comment


    • #3
      im not sure if this is exactly what You need, but this thread will help You understand how to code You'r DAO's properly.
      page 1 and 2;
      http://forum.springsource.org/showthread.php?t=71665
      Last edited by yarco; May 12th, 2009, 05:47 PM.

      Comment

      Working...
      X