将 DAO 类与应用程序实际实例化的 DAO 类分开的目的是什么

发布于 2024-11-01 17:31:14 字数 934 浏览 0 评论 0原文

将 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 技术交流群。

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

发布评论

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

评论(1

入画浅相思 2024-11-08 17:31:14

您的 DAO 始终是接口,它们从来都不是类。这个 DAO 基本上是一种设计模式。 DAO 及其实现的这种分离为分离对象持久机制和数据访问逻辑提供了一种很好的技术。

今天在您提到的 bean xml 文件中,

  <bean name="ServiveDao" class="com.example.ServiceImplHibnernate">
     <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

但是明天您可能希望您的应用程序使用您完成的不同实现而不更改客户端代码。例如,您使用 ibatis 重写了实现,并添加了附加功能来满足您的需求。因此,您编写一个类

   class ServiceImplIBAtis implements ServiceDao {..}

并更改 xml 文件来加载您的实现,

  <bean name="ServiveDao" class="com.mycompany.ServiceImplIBAtis">
     <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

无论何时引用 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,

  <bean name="ServiveDao" class="com.example.ServiceImplHibnernate">
     <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

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

   class ServiceImplIBAtis implements ServiceDao {..}

and change the xml file to load your implementation

  <bean name="ServiveDao" class="com.mycompany.ServiceImplIBAtis">
     <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

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.

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