英文:
What's a good service design for database entity update/delete
问题
我需要关于设计一个没有循环依赖和不必要的数据库更新的服务布局的指导。
假设我有一个包含两个实体的数据库,Factory
(工厂)和 Product
(产品)。一个工厂拥有一个产品列表,每个产品都有对工厂的引用。
public class Factory {
private String id;
private List<String> productIds;
}
public class Product {
private String id;
private String factoryId;
}
每个实体都有一个仓库类,其中包含了在数据库中查找、创建、更新和删除这些实体的方法。
public interface EntityRepository {
public void find();
public void save();
public void delete();
}
查找、创建、更新和删除的业务逻辑被放在服务类中,每个实体对应一个服务类。当删除产品时,应该将其从工厂的产品列表中移除。当删除工厂时,应该同时删除列表中的所有产品。
public class FactoryService {
public boolean deleteFactory(Factory factory) {
// 其他操作
factory.getProductIds().forEach(productId -> {
productRepository.deleteProductWithId(productId);
});
}
public void removeProductFromFactory(Product product, Factory factory) {
factory.getProductIds().remove(product.getId());
factoryRepository.save(factory);
}
// 其他方法
}
public class ProductService {
public boolean deleteProduct(Product product) {
// 其他操作
Factory factory = factoryService.findById(product.getFactoryId());
factoryService.removeProductFromFactory(product, factory);
productRepository.delete(product);
}
// 其他方法
}
为了避免在删除产品时重复编写// 其他操作
的代码,我想在这两个服务中重用代码。然而,正如您所看到的,这会创建一个循环依赖,因为FactoryService
调用ProductService
,而ProductService
在删除工厂时又调用FactoryService
。另一个问题是在删除工厂时,会对数据库进行不必要的更新。这是因为在删除每个产品时,产品删除方法会更新工厂。即使向FactoryService
类中添加一个新的函数deleteAllProducts
,至少会执行一次更新操作。由于工厂将被删除,这是一个不必要的操作。
如果我要重新设计所有这些,有什么更好的设计方法?我缺少哪些辅助类?我是否错误地使用了服务类?
【注意】上述代码示例中的“其他操作”是占位符,您需要根据您的实际需求填充这些操作。
英文:
I need guidance for designing a service-layout without circular dependencies and unnecessary database updates.
Assume I have a database with two entities, Factory
and Product
. A factory has a list of product and each product has a reference to a factory.
public class Factory {
private String id;
private List<String> productIds;
}
public class Product {
private String id;
private String factoryId;
}
Each of these entities has a repository class that has methods for finding, creating, updating and deleting these entities in the database.
public interface EntityRepository {
public void find();
public void save();
public void delete();
}
The business logic for finding, creating, updating and deleting is put in service-classes, one for each entity. When a product is deleted, it should be removed from the list in the factory. When a factory is deleted, all products in the list should also be deleted.
public class FactoryService {
public boolean deleteFactory(Factory factory) {
// Bunch of other things happening
factory.getProductIds().forEach(productId -> {
productRepository.deleteProductWithId(productId);
});
}
public void removeProductFromFactory(Product product, Factory factory) {
factory.getProductIds().remove(product.getId());
factoryRepository.save(factory);
}
...
}
public class ProductService {
public boolean deleteProduct(Product product) {
// Bunch of other things happening
Factory factory = factoryService.findById(product.getFactoryId());
factoryService.removeProductFromFactory(product, factory);
productRepository.delete(product);
}
...
}
In order to skip duplicating the code for // Bunch of other things happening
when deleting a product, I wanted to reuse code in the two services. However, as you can see it creates a circular dependency as factoryService calls productService which in turn calls factoryService when a factory is deleted. Another problem is that when deleting a factory, unnecessary updates are made to the database. This is because when each product is deleted, the product deletion method updates the factory. Even if adding a new function deleteAllProducts
to the FactoryService
-class, at least one update will be performed. Since the factory will be deled this is an unnecessary operation.
If I were to redo all of this, what is a better design? What helper-class am I missing? Am I using service-classes the wrong way?
专注分享java语言的经验与见解,让所有开发者获益!
评论