DAO 和依赖注入,有建议吗?

发布于 2024-12-14 01:36:15 字数 385 浏览 1 评论 0 原文

这是我第一次使用 DAO 模式。从我到目前为止所读到的内容来看,实现这个模式将帮助我将我的调用代码(控制器)与任何持久性实现分开 - 正是我想要的;也就是说,我不想被限制使用任何特定的数据库或第三方库。

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

据我所知,扩展 BasicDAO 需要一个接受 Morphia 和 Mongo 对象的构造函数;这些是非常具体的(第 3 方)类型,我真的不想在 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

べ映画 2024-12-21 01:36:15

“可插入”DAO 层通常/总是基于接口 DAO。例如,让我们考虑一个非常通用的简单的:(

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 实现,你可以在其中找到不同的字段(反映在构造函数参数中) , setter 和 getter 等)。让我们假设一个基于 JDBC 的 DAO:

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

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

一旦实现了通用 DAO(对于具体的数据存储),获得一个具体的 DAO 将是轻而易举的事:

public interface PersonDAO extends GenericDAO<Person, Long> {

}

并且

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

}

(顺便说一句,您在 Morphia 的BasicDAO 是 MongoDB 通用 DAO 的实现。

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

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.

厌倦 2024-12-21 01:36:15

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

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

将军与妓 2024-12-21 01:36:15

嗨,我不是java专家。但试图给出解决方案。

您可以拥有一个超类,其中发生所有与连接相关的事情,以及任何其他可以扩展和使用它的基类。

稍后,您的数据库中针对特定第 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. :)

一页 2024-12-21 01:36:15

Spring 和 Guice 是几个标准 DI 框架。这两个框架都有利于 TDD。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文