且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

需要Dao和服务接口

更新时间:2023-02-26 22:10:14

也许最简单的具体例子就是测试。

Perhaps the simplest concrete illustration for the need is testing.

使用DAO接口,您可以测试应用程序的逻辑,而无需运行可从运行测试的计算机访问的数据库,只需在测试期间将DAO实现交换为虚拟实现。然后,该虚拟实现可以为测试运行之间不会改变的测试提供一致的数据,不能在DB中意外覆盖,在Git / SVN /等等中进行版本化。

With the DAO interface, you can test your application's logic without the need to have a DB running that's accessible from the machine running tests, simply by swapping your DAO implementation for a dummy one during tests. That dummy implementation can then provide consistent data for tests that doesn't change between test runs, cannot be overwritten in DB by accident, is versioned in you Git/SVN/whatever etc.

一般情况下,这是编程到界面,而不是实现设计原则。

In general, this is part of the Program to an interface, not an implementation design principle.

根据我的经验,即使你',这种界面与实现的分离也是一个好主意。永远不会有多个实现,因为它鼓励程序员更深入地思考类的契约

In my experience, this separation of interface vs. implementation is a good idea even if you're never going to have multiple implementations, because it encourages programmers to think more deeply about the contract of the class.

注意原则是不是普遍接受的,这里有一些反驳例如。

Note that the principle is not universally accepted, here are some counter-arguments for example.