DAO and dependency injection, advice?

≡放荡痞女 提交于 2019-12-01 03:07:16

问题


This is the first time im using the DAO pattern. From what I've read so far, implementing this pattern will help me seperate my calling code (controller) from any persistence implementation - exactly what I want; that is, I don't want to be restrcited to the use of any particular database or 3rd party libraries.

I'm creating some test code (in TDD fashion) using MongoDB and morphia (as an example), with morphia's provided BasicDAO class.

As far as I can tell, extending BasicDAO<T, V> requires a constructor that accepts Morphia and Mongo objects; these are very specific (3rd party) types that I don't really want floating around outside of the DAO class itself.

How can I have more of a pluggable architecture? By this I mean, what should I look into re being able to configure my application to use a specific DAO with specific configuration arguments, external to the actual source?


回答1:


A "pluggable" DAO layer is usually/always based on an interface DAO. For example, lets consider a quite generic simple one:

public interface GenericDAO <T, K extends Serializable> {  
    List<T> getAll(Class<T> typeClass);   
    T findByKey(Class<T> typeClass, K id);  
    void update(T object);  
    void remove(T object);  
    void insert(T object);  
}

(This is what you have in Morphia's generic DAO)

Then you can develop different several generic DAO implementations, where you can find different fields (reflected in constructor parameters, setters and getters, etc). Let's assume a JDBC-based one:

public class GenericDAOJDBCImpl<T, K extends Serializable> implements GenericDAO<T, K extends Serializable> {
    private String db_url;

    private Connection;
    private PreparedStatement insert;
    // etc.
}

Once the generic DAO is implemented (for a concrete datastore), getting a concrete DAO would be a no brainer:

public interface PersonDAO extends GenericDAO<Person, Long> {

}

and

public class PersonDAOJDBCImpl extends GenericDAOJDBCImpl<Person, Long> implements PersonDAO {

}

(BTW, what you have in Morphia's BasicDAO is an implementation of the generic DAO for MongoDB).

The second thing in the pluggable architecture is the selection of the concrete DAO implementation. I would advise you to read chapter 2 from Apress: Pro Spring 2.5 ("Putting Spring into "Hello World") to progressively learn about factories and dependency injection.




回答2:


Spring does DI for you using configurations and it's widely used.




回答3:


Hi i am not an expert in java. but trying to give a solution.

you can have a superclass where all the connection related stuff happens and any other base class where you can extend and use it.

Later any switch in your DB for specific 3rd party drivers you can rewrite the superclass.

Again I am no expert. Just trying around here to learn. :)




回答4:


A couple standard DI frameworks are Spring and Guice. Both these frameworks facilitate TDD.



来源:https://stackoverflow.com/questions/8049627/dao-and-dependency-injection-advice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!