DDD: What kinds of behavior should I put on a domain entity?

前端 未结 4 1085
栀梦
栀梦 2021-02-05 07:34

My team tries very hard to stick to Domain Driven Design as an architectural strategy. But, most of the time, our domain entities are pretty anemic. We\'d like to be putting mor

4条回答
  •  清酒与你
    2021-02-05 07:54

    The behaviour that is on your entities should reflect the business model. What can be done to or by that entity is the business world should be sopmething that can be done to or by the entity class. For example:

    In an online shopping system, you can add a product to your cart. So the Cart class should look like this:

    public class Cart
    {
        //...
    
        public void AddProduct(Product product)
        {
            ...code to add product to cart.
        }
    }
    

    It could be argued that the methids should reflect the use cases.

提交回复
热议问题