问题描述
我具有需要为客户和客户地址(通过JDBC)更新数据库的组件.从CustomerDAO调用CustomerAddressDAO是否合适?还是创建一个单独的"CustomerDataManager"组件来分别调用它们?
I have component which needs to update the database for the customer and customer address (via JDBC). Is it appropriate to call the CustomerAddressDAO from the CustomerDAO? Or create a separate "CustomerDataManager" component which calls them separately?
推荐答案
您可以做到这一点,但这并不意味着您应该这样做.在这些情况下,我喜欢使用服务(在这种情况下为CustomerService
),该服务具有使用两个DAO的方法调用.您可以围绕服务方法定义事务,因此,如果一个调用失败,它们都会回滚.
You can do it, but that doesn't mean you should. In these cases, I like to use a Service (CustomerService
in this case) that has a method call that uses both DAOs. You can define the transaction around the service method, so if one call fails, they both roll back.
调用其他DAO的DAO的问题是,您很快就会以循环引用结束.依赖注入变得更加困难.
The problem with DAOs that call other DAOs is you will quite quickly end up with circular references. Dependency injection becomes much more difficult.
这篇关于DAO可以呼叫DAO吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!