球衣从另一个资源调用一个资源以重用代码
我正在尝试重构我的代码以重新使用现有代码,但想要一些关于如何使用 REST 资源(在本例中为泽西岛)以最佳方式完成此操作的建议。
我有一个处理域的资源。此资源中的此类方法包括获取域、获取多个域以及发布批量获取(太多而无法使用 GET)。
不过,我还有一个对 URL 执行相同操作的资源。除了我在这里所做的是从 URL 中提取域并有效地执行与使用 DomainResource 类相同的逻辑,因为一旦我从 URL 中获取了域,我就需要执行完全相同的查找。但是,我没有重复使用现有的 DomainResource 方法,而是在 URLResouce 类中复制并调用相同的方法。
因此,在这种情况下最好进行重复并保持资源分离,还是可以(坏/好实践)从另一个资源调用一个资源,在这种情况下,URLResouce 将委托给 DomainResource?这听起来怎么样?
I am trying to refactor my code to make re-use existing code but wanted some suggestions as to how this should be done in the best possible way using REST resources (Jersey in this case).
I have a resource that deals with Domains. Such methods in this resource include getting a domain, getting multiple domains, and posting for bulk gets (too many to use a GET).
However, I also have a resouce that does the same for URLs. Except that what I do here is to extract the domains from the URL and effectively perform the same logic that I do with the DomainResource class as once I have the domain from the URL, I need to execute the exact same lookups. However instead of re-using the existing DomainResource methods, I am duplicating and calling the same methods in my URLResouce class.
So is it better to have duplication in this case and keep the resources separate, or is it possible (bad/good preactice) to call one resource from another, in the case, the URLResouce would delegate to the DomainResource? How does this sound?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
公共代码应该位于实用程序类中。您的每个资源(我认为您使用 @GET 注释的方法)通常不会相互调用。我会将 fetchDomains 方法放入实用程序类中。然后,用于获取域的控制器/服务/资源方法将直接调用该实用程序方法,而用于从 URL 获取域的控制器/服务/资源方法将首先提取域,然后调用该实用程序方法。
The common code should be in a utility class. Each of your resources (by which I think you man the methods annotated with @GET) would not normally call each other. I would put a
fetchDomains
method in a utility class. Then the controller/service/resource method for getting domains would call that utility method directly, and the controller/service/resource method for getting domains from URLs would first extract the domain then call the utility method.不,无论如何,因为重复会减少你的 oops 概念。所以请尝试将公共代码保留在单独的类中。
No, In any case Because duplication reduce your oops concept.So please try to keep common code in separate class.