What methods should go in my DDD factory class?

后端 未结 7 1385
终归单人心
终归单人心 2021-02-02 17:38

I am struggling to understand what my factory class should do in my DDD project. Yes a factory should be used for creating objects, but what exactly should it be doing. Consid

7条回答
  •  太阳男子
    2021-02-02 17:54

    What should go in your factory's Create method is whatever is necessary to put a brand spanking new object into a VALID state.

    Now, for some objects that means you won't do anything except this:

    public Product Create()
    {
       return new Product();
    }
    

    However, you may have business rules, default settings, or other requirements that you want to enforce when an object is created. In that case, you would put that logic in that method.

    And that's part of the benefit of the Factory. You now have one and only one place where that special logic resides, and only one place where a new object gets created.

提交回复
热议问题