简体   繁体   English

DAO和依赖注入,建议?

[英]DAO and dependency injection, advice?

This is the first time im using the DAO pattern. 这是我第一次使用DAO模式。 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. 我正在使用MongoDB和morphia(作为示例)创建一些测试代码(以TDD方式),并使用morphia提供的BasicDAO类。

As far as I can tell, extending BasicDAO<T, V> requires a constructor that accepts Morphia and Mongo objects; 据我所知,扩展BasicDAO<T, V>需要一个接受Morphia和Mongo对象的构造函数; these are very specific (3rd party) types that I don't really want floating around outside of the DAO class itself. 这些是非常具体的(第三方)类型,我真的不想在DAO类本身之外浮动。

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? 我的意思是,我应该考虑如何配置我的应用程序以使用具有特定配置参数的特定DAO,在实际源外部?

A "pluggable" DAO layer is usually/always based on an interface DAO. “可插拔”DAO层通常/总是基于接口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 ) (这是你在Morphia的通用DAO中所拥有的

Then you can develop different several generic DAO implementations, where you can find different fields (reflected in constructor parameters, setters and getters, etc). 然后,您可以开发不同的几个通用DAO实现,您可以在其中找到不同的字段(反映在构造函数参数,setter和getter等)。 Let's assume a JDBC-based one: 我们假设一个基于JDBC的:

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: 一旦实现了通用DAO(对于具体的数据存储区),获得具体的DAO将是没有道理的:

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). (顺便说一下,你在Morphia的BasicDAO中所拥有的是MongoDB的通用DAO的实现)。

The second thing in the pluggable architecture is the selection of the concrete DAO implementation. 可插拔架构中的第二件事是选择具体的DAO实现。 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. 我建议你阅读Apress的第2章:Pro Spring 2.5 (“将春天放入”Hello World“)以逐步了解工厂和依赖注入。

Spring使用配置为您做DI,并且它被广泛使用。

Hi i am not an expert in java. 嗨,我不是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. 试着在这里学习。 :) :)

A couple standard DI frameworks are Spring and Guice. 一对标准的DI框架是Spring和Guice。 Both these frameworks facilitate TDD. 这两个框架都有助于TDD。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM