将 DAO 类与应用程序实际实例化的 DAO 类分开的目的是什么
将 DAO 类与应用程序代码中实际实例化的 DAO 类分开有什么好处,即为什么不在这样的场景中直接实例化 DAO 类:
Class CreateIocContainer{
p s v main(String[] args){
new IocContainer("springMetadataFile.xml");
}
}
Class ClassThatInstantiatesServicesViaSpringBean{
Services services;
// bean setter for services class
setServices(Services services){
this.services = services
}
}
Class ServicesImpl implements Services
ServicesDao servicesDao;
String getSomethingFromDB(String argumentForQuery){
return servicesDao.getSomethingFromDB(argumentForQuery);
}
}
Class ServicesDaoImpl implements ServicesDao{
String getSomethingFromDb(String argumentForQuery){
//code to return something from db
return queryResultString;
}
}
另外,我调用的类会是 Class ClassThatInstantiatesServicesViaSpringBean 是一个工厂类,通常命名为Class XFactory?
What is the benefit of distancing DAO classes from the ones actually being instantiated in the application code, i.e. why not just instantiate the dao class straight up in a scenario like this:
Class CreateIocContainer{
p s v main(String[] args){
new IocContainer("springMetadataFile.xml");
}
}
Class ClassThatInstantiatesServicesViaSpringBean{
Services services;
// bean setter for services class
setServices(Services services){
this.services = services
}
}
Class ServicesImpl implements Services
ServicesDao servicesDao;
String getSomethingFromDB(String argumentForQuery){
return servicesDao.getSomethingFromDB(argumentForQuery);
}
}
Class ServicesDaoImpl implements ServicesDao{
String getSomethingFromDb(String argumentForQuery){
//code to return something from db
return queryResultString;
}
}
Also, would the class I called Class ClassThatInstantiatesServicesViaSpringBean be a factory class and usually be named Class XFactory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 DAO 始终是接口,它们从来都不是类。这个 DAO 基本上是一种设计模式。 DAO 及其实现的这种分离为分离对象持久机制和数据访问逻辑提供了一种很好的技术。
今天在您提到的 bean xml 文件中,
但是明天您可能希望您的应用程序使用您完成的不同实现而不更改客户端代码。例如,您使用 ibatis 重写了实现,并添加了附加功能来满足您的需求。因此,您编写一个类
并更改 xml 文件来加载您的实现,
无论何时引用 bean ServiceDao,spring 都会注入 ServiceImplIBAtis 实例而不是 ServiceImplHibnernate。现在您的应用程序不需要知道后台发生了什么变化。它只需要知道有一个名为 Service 的 dao,并且有一些可用于数据访问的方法。
Your DAOs are always interfaces they are never a class. This DAO is basically a design pattern. This separation of DAO and its implementation gives a nice technique for separating object persistence mechanism and data access logic.
Today in the bean xml file you mention,
But tomorrow you may want your application to use different implementation done by you without changing the client code. For example, you have rewritten the implementation using ibatis with additional features to match up your requirement. So you write a class
and change the xml file to load your implementation
Where ever the bean ServiceDao is referenced the spring will inject the ServiceImplIBAtis instance instead of ServiceImplHibnernate. Now your application does not need to know what is changed in the background. All it needs to know is there is a dao called Service and there are methods that can be used for data access.