我在当前项目中越来越多地采用DDD/洋葱架构。 其中一个我仍然不清楚的问题是应该有多少封装性。 以具体示例更容易解释。
示例
namespace MyProject.Model
{
public class ComplexEntity
{
private int _id;
public int Id { get {return _id;} }
public ValueObjectA ValueA {get; set;}
public bool IsBool {get; set;}
public ComplexEntity(ValueObjectA a, bool isBool)
{
// Do some validation first
ValueA = a;
ValueB = b;
IsBool = isBool;
}
}
public class ValueObjectA
{
public bool IsBoolA {get; private set;}
public bool IsBoolB {get; private set;}
public ValueObjectA(bool a, bool b)
{
IsBoolA = a;
IsBoolB = b;
}
}
public Interface IComplextEntityFactory
{
// Option 1
ComplexEntity Create(
ValueObjectA a,
bool IsBool);
// Option 2
ComplexEntity Create(
bool valueABool a,
bool valueBBool b,
bool isBool);
}
}
问题
对于实体的工厂,你是:
- 期望调用方为你构建值对象并使用它来初始化 ComplexEntity 吗?
- 传递基本 CLR 类型到工厂,然后构造每个组成 Entity 的 ValueObject ?
我倾向于选项2,但似乎找不到支持这一观点的文献。
编辑1
说实话,我还是不清楚。聚合根呢?
当我的实体引用其他实体时,例如下面这样。
- Should I have an
IComplexEntityFactory
,ILessComplexEntityFactory
? Or just anIComplexEntityAggregateFactory
that creates the LessComplexEntity and instantiates ComplexEntity ? - In the case of the AggregateFactory solution, what do I do if the LessComplexEntity attribtues that have been passed to the factory correspond to an existing LessComplexEntity? Do I retrieve and reuse it from a Repository? Or do I return an error to the caller?
What would the method signature be for the AggregateFactory? Would it be
(ValueObject a, ValueObject b)
, or(ValueObject value, LessCompelxEntity entity)
public class ComplexEntity { private readonly int _id; public int Id { get { return _id;} }
public ValueObject Value {get; set;} public LessComplexEntity Entity {get; set;} public ComplexEntity(int id, ValueObject value, LessComplexEntity entity) { }
}
public class LessComplexEntity { private readonly int _id; public int Id { get {return _id;} } public ValueObject Value {get; set;} public LessComplexEntity(int id, ValuObject value) { } }