Generic Dao and service Layer
I am going to design a DAO layer for my application. My focus is that Services just calls DAO which is independent of underlaying implementation.
    public interface GenericSearchDao{
        List getAll();
        List getByQuery(String query);
}
public class UserJdbcSearch implements GenericSearchDao{
        public List getAll(){
                // Get al开发者_运维百科l users;
        }
        List getByQuery(String query){
                // Get users by query;
        }
}
public class UserFileSystemSearch implements GenericSearchDao{
        public List getAll(){
                // Get all users from file system;
        }
        List getByQuery(String query){
                // Get users by query[this leads to invalid operation];
        }
}
public userService {
        private GenericSearchDao dao = new UserFileSystemSearch();
        public List getUsers(){
                rturn dao.getAll();
        }
         public List getByQuery(String query){
               return  dao.getByQuery(query);
        }
}
Help Required:
What should I do to get rid from 'getByQuery(query)' specific implementaions because datastore can be RDBMS, filesystem, FTP etc.
How should I design my Dao layer generically?
If any one says "remove the getByQuery() from GenericSearchDao" then what should i do in the case where i need data specific to business operation for eg : user with roles , user with products etc..
What should I do to get rid from 'getByQuery(query)' specific implementaions because datastore can be RDBMS, filesystem, FTP etc.
You don't, you just need to provide "generic" query, for instance "name = a" may perform a query in a databae, or look for a file named "a" or pretty much anything else.
What information do you plan to pass in the "query" parameter ? SQL ? I'd replace the String parameter with something business-specific, maybe a small class wiht fields like "name", "surname", etc. The underlying implementation will transform it into SQL, or remote service calls, or other implementation-specific magic.
You may find this approach useful http://www.bejug.org/confluenceBeJUG/display/BeJUG/Generic+DAO+example
For specific impl just throw "UnsupportedOperationException". Good Generic dao implementation can be found here http://code.google.com/p/hibernate-generic-dao/ - I also could adjust the source code to work with hibernate 4 & spring 3
Hey, check this site DAO Implementation. There are some DAO implementations with different Design patterns. I think DAO with Abstact factory would be suitable for you.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论