最近看了一些DDD相关的文章和书籍,反思应用到我现在负责的一个系统中,不知道下面这种方式对不对?麻烦各位大佬指导一下。
/*
以系统的部分功能为例:
业务员在门店资源列表中认领门店,门店认领后会进入业务员个人的门店库存中。
然后业务员和商家谈好合作后,从门店库存中选择未签约的门店,进行签约操作。
签约审批完成后,会生成一条合约。这条合约里有业务员和商家谈的合作细节。
如果谈不下来,将这个门店从自己的库存中释放掉。
合作过程中,如果商家不愿意合作了,需要将门店挂着的合约设置为失效。
合作过程中也可以随时修改谈的合约。
这种情况下该如何建模?
*/
@Getter
public class Shop{
private Integer bdUserId;
private Integer contractId;
...
public void relateBd(Bd bd){
this.bdUserId = bd.getUserId();
}
public void relateContact(Contract contract){
this.contractId = contract.getId();
}
public void clearBd(){
this.bdUserId = 0;
}
public void clearContact(){
this.contractId = 0;
}
}
public interface ShopRepository{
int save(Shop shop);
Shop searchOne(int shopId);
boolean modify(Shop shop);
boolean removeOne(Shop shop);
List<Shop> listBdShop(Bd bd);
List<Shop> list(ShopListReq listReq);
}
@Getter
public class Contract{
private Integer opStatus;
...
}
public interface ContractRepository{
int save(Contract contract);
Contract searchOne(int contractId);
boolean modify(Contract contract);
boolean removeOne(Contract contract);
List<Contract> list(ContractListReq listReq);
}
@Component
public class ShopResource{
private final ShopRepository shopRepository;
@Autowired
public ShopResource(ShopRepository shopRepository){
this.shopRepository = shopRepository;
}
// 查询门店资源列表
public List<ShopDTO> list(ShopListReq shopListReq){
List<Shop> shopList = shopRepository.list(shopListReq);
return shopList -> List<ShopDTO>;
}
}
@Component
public class BdShop{
private final ShopRepository shopRepository;
private final ContractRepository contractRepository;
@Autowired
public ShopResource(ShopRepository shopRepository, ContractRepository contractRepository){
this.shopRepository = shopRepository;
this.contractRepository = contractRepository;
}
// BD认领具体某个门店
public void claimSelf(BD bd, int shopId){
Shop shop = shopRepository.searchOne(shopId);
shop.relateBd(bd);
shopRepository.modify(shop);
}
// 获取归属当前BD的商户
public List<ShopDTO> list(Bd bd){
List<Shop> shopList = shopRepository.listBdShop(bd);
return shopList -> shopDTOList;
}
// BD完成签约
public void signComplete(int shopId, Contract contractDTO){
Shop shop = shopRepository.searchOne(shopId);
shop.relateContact(contract);
shopRepository.modify(shop)
}
// BD释放掉门店
public void release(int shopId){
Shop shop = shopRepository.searchOne(shopId);
shop.clearBd();
shopRepository.modify(shop)
}
// BD取消与门店的合约
public void giveUpContract(int shopId){
Shop shop = shopRepository.searchOne(shopId);
shop.clearContact();
shopRepository.modify(shop)
}
// BD取消与门店的合约,并释放门店
public void giveUpContractAndRelease(int shopId){
Shop shop = shopRepository.searchOne(searchReq);
shop.clearContact();
shop.clearBd();
shopRepository.modify(shop)
}
// BD修改合约
public void modifyContract(int shopId, ContractModifyReq contractModifyReq){
Shop shop = shopRepository.searchOne(shopId);
Contract contract = contractRepository.searchOne(shop.getContractId());
contract.modify(contractModifyReq);
contractRepository.modify(contract);
}
}