我遇到了一个不好的做法 - 在两个服务之间创建了一个依赖关系,我不确定重构和改进它的最佳策略是什么。用Java和Spring重构服务之间的依赖关系
这是我的例子,我有一个PurchaseServiceImpl
实现我的PurchaseService
接口,并在此PurchaseServiceImpl
我注入我CustomerService
和ProductService
:
@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);
}
}
什么是你最好的策略来攻击这种做法?目前这很容易,但感觉像债务需要相当注意。
你有什么做法有问题吗? –
我不想在服务之间创建依赖关系(就像我上面所做的那样)。 – lapadets
为什么你认为这是一种不好的做法? – Jesper