这是我第一次使用 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?
发布评论
评论(4)
“可插入”DAO 层通常/总是基于接口 DAO。例如,让我们考虑一个非常通用的简单的:(
这就是 Morphia 的通用 DAO)
然后你可以开发不同的几个通用 DAO 实现,你可以在其中找到不同的字段(反映在构造函数参数中) , setter 和 getter 等)。让我们假设一个基于 JDBC 的 DAO:
一旦实现了通用 DAO(对于具体的数据存储),获得一个具体的 DAO 将是轻而易举的事:
并且
(顺便说一句,您在 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:
(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:
Once the generic DAO is implemented (for a concrete datastore), getting a concrete DAO would be a no brainer:
and
(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.
Spring 使用配置为您进行 DI,并且它被广泛使用。
Spring does DI for you using configurations and it's widely used.
嗨,我不是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. :)
Spring 和 Guice 是几个标准 DI 框架。这两个框架都有利于 TDD。
A couple standard DI frameworks are Spring and Guice. Both these frameworks facilitate TDD.