DAO和依赖注入,建议? [英] DAO and dependency injection, advice?

查看:159
本文介绍了DAO和依赖注入,建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用DAO模式。到目前为止,我已经阅读,实现这种模式将有助于我从任何持久化实现中分离我的调用代码(控制器) - 正是我想要的;也就是说,我不想被限制使用任何特定的数据库或第三方库。

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.

我正在创建一些测试代码(以TDD方式)使用MongoDB和morphia(作为示例),morphia提供 BasicDAO 类。

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

据我所知,扩展 BasicDAO< T,V> 需要一个接受Morphia和Mongo对象的构造函数;这些是非常具体的(第三方)类型,我不希望浮在DAO类外面。

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.

我如何拥有更多的可插拔架构?我的意思是说,我应该怎么看待,可以配置我的应用程序使用特定的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层通常/总是基于接口DAO。例如,让我们考虑一个非常简单的简单方法:

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);  
}

(这是你在 Morphia的通用DAO

然后,您可以开发不同的几个通用DAO实现,您可以在其中找到不同的字段(反映在构造函数参数,设置器和getter等中)。让我们假设一个基于JDBC的一个:

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.
}

一旦实现了通用DAO(for一个具体的数据库),获得一个具体的DAO将是一个没有脑子的:

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> {

}

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

}

(BTW,你在 Morphia的BasicDAO 是MongoDB的通用DAO的实现)。

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

可插拔架构中的第二件事是选择具体的DAO实现。我建议您阅读 Apress:Pro Spring 2.5 (将春天放入Hello World)中的第2章)逐步了解工厂和依赖注入。

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.

这篇关于DAO和依赖注入,建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆