2016-08-12 86 views
-1

我遇到了一个不好的做法 - 在两个服务之间创建了一个依赖关系,我不确定重构和改进它的最佳策略是什么。用Java和Spring重构服务之间的依赖关系

这是我的例子,我有一个PurchaseServiceImpl实现我的PurchaseService接口,并在此PurchaseServiceImpl我注入我CustomerServiceProductService

@Service 
public class PurchaseServiceImpl implements PurchaseService { 
    // TODO Refactor this dependency between services 
    @Inject 
    private CustomerService customerService; 
    // TODO Refactor this dependency between services 
    @Inject 
    private ProductService productService; 

这里我用customerService获取一个客户和productService获取产品分别来自后端:

private Customer getCustomer(long id) throws BackendException { 
    try { 
     return customerService.getCustomer(id); 
    } catch (CustomerNotFoundException e) { 
     throw new BackendException(e); 
    } 
} 

private ProductDetails getProductDetails(long id) throws BackendException { 
    try { 
     return productService.getProductDetails(id); 
    } catch (ProductNotFoundException e) { 
     throw new BackendException(e); 
    } 
} 

什么是你最好的策略来攻击这种做法?目前这很容易,但感觉像债务需要相当注意。

+0

你有什么做法有问题吗? –

+0

我不想在服务之间创建依赖关系(就像我上面所做的那样)。 – lapadets

+0

为什么你认为这是一种不好的做法? – Jesper

回答

0

我没有看到一个服务(购买服务)使用两个其他服务的问题。如果两个服务具有循环依赖性,即彼此使用,那将是有问题的。

+0

嗯,我正在考虑引入Facade,以便我的UI通过Facade与服务进行通信,而不是直接进行通信。目前为止没有循环依赖,我不认为我会遇到这个问题。我只收集来自更有经验的开发人员或具有类似经历的开发人员的意见,我想知道他们是如何处理这些问题的。 – lapadets

+0

有一个门面是一个很好的设计理念:-)但我见过很多应用程序,其中服务使用其他服务。如上所述,只要没有循环依赖关系,就可以正常工作。 – Guenther