简体   繁体   English

我应该从应用程序服务或实体获得其他汇总值吗?

[英]Should I get other Aggregate value from application service or entity?

I have Inventory, Catalog, and Sales bounded contexts. 我有库存,目录和销售限制上下文。

I have this invariant: 我有这个不变式:

When creating catalog, we cannot sell more than what we have in Inventory 创建目录时,我们所售商品不能超过库存商品

class CatalogApplicationService {
    void createCatalog(ProductId productId, int numToSell) {
        ProductValue pv = productService.byId(productId)
        // Catalog constructor will validate numToSell againts pv.numAvailable
        Catalog c = new Catalog(pv, numToSell)
        catalogRepository.save(c)
    }
}

class Catalog {
    Catalog(ProductValue pv, int numberToSell) {
        if (pv.numAvailable < numberToSell) throw new IllegalArgumentException();
        setSku(pv.sku);
        setAmount(numberToSell);
    }
}

or should I pass along the service into Catalog 还是应该将服务传递到目录中

class CatalogApplicationService {
    void createCatalog(ProductId productId, int numToSell) {
        Catalog c = new Catalog(productService, numToSell)
        catalogRepository.save(c)
    }
}

class Catalog {
    Catalog(ProductService productService, int numberToSell) {
        ProductValue pv = productService.byId(productId)
        if (pv.numAvailable < numberToSell) throw new IllegalArgumentException();
        setSku(pv.sku);
        setAmount(numberToSell);
    }
}

You should not inject services inside your aggregates ( Catalog ) but pass the data to them, in order to keep them as clean as possible, without unnecessary dependencies. 您不应将服务注入聚合( Catalog )内部,而应将数据传递给它们,以保持其尽可能干净,而没有不必要的依赖关系。

Also, you should use the ubiquitous language (UL) throughout your code. 同样,您应该在代码中使用通用语言(UL)。 For example, throwing a generic IllegalArgumentException doesn't seem to be from from your UL. 例如,抛出通用的IllegalArgumentException似乎不是来自您的UL。

Verifying a business logic is generally done at the Service layer. 验证业务逻辑通常是在服务层进行的。 Models should be kept as only containing data, rather than logic. 模型应保持为仅包含数据,而不是逻辑。 So, your first solution is advised. 因此,建议您使用第一个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM