重构设计模式:将适配器模式应用于第 3 方 API

发布于 2024-10-21 04:34:02 字数 1030 浏览 3 评论 0原文

为了访问我们的帐户数据库的数据,我们需要通过第 3 方 API,这只是作为我们解决方案的引用添加的另一个程序集。

在我们的服务中,有几种类型的 API 调用与此类似:

 ICustomers customerCollection =GetCollection(OrgId,OrgUnit,CustomerInfo, "Customers");
       {
           customerCollection.Filter.Add("MasterCustomerId", QueryOperatorEnum.Equals, masterCustomerID);
           customerCollection.Filter.Add("SubCustomerId", QueryOperatorEnum.Equals,subCustomerID);
       }
       customerCollection.Fill();

调用 customerCollection.Fill() 后,API 会访问数据库并使用适当的过滤器运行查询。返回的对象属于API。在此服务中,有许多对 API 的调用,都具有独特的过滤要求。

我想做的是使用适配器模式将这些调用放在我自己的类后面并返回我拥有的对象。然后,我可以针对我的服务中的适配器进行编码,并让他们担心调用第三方 API。

问题是我不知道处理过滤器所有变体的好方法,因此我不能简单地在适配器中重新创建过滤语法。

我可以创建扩展/静态方法,将 API 的过滤系统转换为流畅的语法,因此它可能看起来像这样:

customerCollection.Filter(“MasterCustomerId).Equals(masterCustomerId).Filter(SubCustomerId).Equals(subCustomerId).Fill();

但与此相关的某些内容似乎是错误的。我觉得我对服务承担了太多责任,无法完成所有这些电话。

我正在努力发展我的面向对象技能,如果有人能帮助我在这个问题上指明正确的方向,我将不胜感激。

In order to access data for our Accounts database we need to go through a 3rd party API, which is simply another assembly added as a reference to our solution.

Within a service of ours there are several types of calls to the API that look similar to this:

 ICustomers customerCollection =GetCollection(OrgId,OrgUnit,CustomerInfo, "Customers");
       {
           customerCollection.Filter.Add("MasterCustomerId", QueryOperatorEnum.Equals, masterCustomerID);
           customerCollection.Filter.Add("SubCustomerId", QueryOperatorEnum.Equals,subCustomerID);
       }
       customerCollection.Fill();

After calling customerCollection.Fill(), the API goes out to the database and runs a query with the appropriate filters. The objects returned belong to the API. Within this service there are many calls to the API all with unique filtering requirements.

What I’d like to do is use the Adapter pattern to put these calls behind my own classes and return the objects that I own. Then I can code against my adapters from my service, and let them worry about calling out to the third party API.

The problem is that I don’t know a good way to handle all of the variations of the filters so that I’m not simply recreating the filtering syntax within my adapters.

I could make extension/static methods that turn the API’s filtering system into a fluent syntax, so it could look like this:

customerCollection.Filter(“MasterCustomerId).Equals(masterCustomerId).Filter(SubCustomerId).Equals(subCustomerId).Fill();

But something about that just seems wrong. I feel that I’m giving too much responsibility to the service to do all of those calls.

I’m working hard on developing my object oriented skills and I would appreciate if anyone could help point me in the right direction on this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

白首有我共你 2024-10-28 04:34:02

我不会尝试用你的对象重新创建他们的 API。它将创建一个非常紧密耦合的关联,并且不太可能给您带来太多好处。

您的适配器可能应该提供一个更加面向领域的接口,例如

public IList<MyCustomerObjects> GetCustomers()
{
    //Call out to the third party API with the appropriate filters

    //foreach object in 3rd party collection, wrap in adapter class and add to list

    //return list of your objects
}

(这几乎是领域驱动设计的存储库模式)。

换句话说,您让适配器担心第 3 方 API 中需要什么过滤标准,并且您的调用代码处理合理的业务/域级概念。

I wouldn't try and re-create their API with your objects. It'll create a very tightly-coupled association and is unlikely to bring you much in the way of benefits.

Your adapter should probably provide a more domain-oriented interface e.g.

public IList<MyCustomerObjects> GetCustomers()
{
    //Call out to the third party API with the appropriate filters

    //foreach object in 3rd party collection, wrap in adapter class and add to list

    //return list of your objects
}

(This is pretty much the repository pattern from Domain-Driven design btw).

In other words, you let the adapter worry about what filtering criteria is needed in the 3rd party API and your calling code deals with sensible business/domain-level concepts.

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