[英]CustomerDAO alternative when Customer's fields are retrieved both from a third-party web-service and application's dedicated database
I have a Customer
class.我有一个
Customer
类。 Its private field is a CustomerDAO
interface which database-access methods are called by myCustomer.getAge()
for example.它的私有字段是一个
CustomerDAO
接口,例如由myCustomer.getAge()
调用数据库访问方法。 CustomerDAO retrieves data from the application's dedicated database. CustomerDAO 从应用程序的专用数据库中检索数据。
The problem is that a few fields of Customer
now have to be retrieved from a third-party web-service.问题是现在必须从第三方 Web 服务中检索
Customer
的一些字段。 Logically calling some web-service doesn't belong to CustomerDAO
(which is intended for communicating with a single database, right?).从逻辑上讲,调用某些 Web 服务不属于
CustomerDAO
(旨在与单个数据库进行通信,对吗?)。
What is the industry recommended approach in such cases?在这种情况下,行业推荐的方法是什么?
Shall I just add another field CustomerWebServiceDAO
(as interface) to Customer
?我是否应该向
Customer
添加另一个字段CustomerWebServiceDAO
(作为接口)? And my Customer
would use internally two different instances ( CustomerDAO
and CustomerWebServiceDAO
).我的
Customer
将在内部使用两个不同的实例( CustomerDAO
和CustomerWebServiceDAO
)。 So DAO gets split into 2 separate classes... What if next I need access to another Web-service plus an additional database?所以 DAO 被分成 2 个独立的类……如果接下来我需要访问另一个 Web 服务和一个额外的数据库怎么办? What better solution/pattern can be used?
可以使用什么更好的解决方案/模式?
Adding web-service methods to initial CustomerDAO interface would mix database access with web-service access, which I think is not good.将 web 服务方法添加到初始 CustomerDAO 接口会将数据库访问与 web 服务访问混合在一起,我认为这并不好。
If I understand your question right you need to have a database service which has all GRUD operation of Customer and assume it will be called CustomerDBDao and another service Called CustomerAPIDao which is responsible of all web service operations and all services is called in a general service called CustomerDAO which is filling data from both services and generate One Unified POJO this concept is called adapter design patter如果我理解您的问题是正确的,您需要有一个数据库服务,其中包含Customer 的所有 GRUD 操作并假设它将被称为CustomerDBDao和另一个名为CustomerAPIDao 的服务,它负责所有 Web 服务操作,所有服务都在一个名为的通用服务中调用CustomerDAO正在填充来自两个服务的数据并生成一个统一的 POJO,这个概念被称为适配器设计模式
https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.