What methods should go in my DDD factory class?

后端 未结 7 1383
终归单人心
终归单人心 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 18:20

    Be carefull, there are two reasons to instantiate a new object : Creating it and rehydrating it from the database.

    The first case is handled by the factory. You can provide several methods to create an object on the factory. Factory methods should return valid objects, so you can pass parameters to these methods to provides required information.

    The factory method can also chose the actual type to instantiate based on parameters.

    You should not mix this with rehydrating from the database. This kind of instantiation should take values from the datarow and instantiate the object with it. I usualy call this a data builder instead of a factory.

    The main difference is that the factory will instantiate an object with a new identity while the databuilder will instantiate an object with an already existing identity.

提交回复
热议问题