在域驱动设计中为模型属性设置默认值的最佳做法? [英] Best practice for setting default values for model properties in Domain Driven Design?

查看:145
本文介绍了在域驱动设计中为模型属性设置默认值的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在DDD中为新实体设置默认属性的最佳方法是什么?此外,为复杂属性(例如集合)设置默认状态的最佳方法是什么?



我的感觉是默认值应该在模型本身,因为它们是业务规则的形式(默认情况下,我们希望X为Y& Z),域代表业务。使用这种方法,模型本身可能会使用一个静态的GetNew()方法:

 
public class Person {
public string Name {get;组; }
public DateTime DateOfBirth {get;组; }
public bool IsAlive {get;组; }
public List Limbs {get;组; }

public static Person GetNew(){
return new Person(){
IsAlive = true,
Limbs = new List(){RightArm,LeftArm,RightLeg ,LeftLeg}
}
}
}

不幸的是,我们需要收集属性要设置为另一个列表的所有成员,并且由于此模型与其Repository / DbContext解耦,因此它没有任何方式加载它们。



Crappy解决方案将作为参数传递:

  public static Person GetNew(List< Limb> allLimbs){
return new Person (){
IsAlive = true,
Limbs = allLimbs
}
}

或者,还有一些更好的方法来设置简单的&复杂的模型属性?

解决方案

这是一个 DDD中的工厂模式。它可以是一个专门的类,例如 PersonFactory 或静态方法,如您的示例。我喜欢静态方法,因为我看不出需要创建一个全新的类。



就初始化集合而言, GetNew 方法与集合作为​​一个参数是我会去的东西。它说明了一个重要的制约因素 - 创建一个你需要该集合的新的人实体。集合实例将由承载需要的特定用例的应用程序服务提供。更一般地,默认值可以存储在数据库中,在这种情况下,应用程序服务将调用到存储库以获取所需的值。


What's the best way to set default properties for new entities in DDD? Also, what's the best way to set default states for complex properties (eg. collections)?

My feeling is that default values should be in the models themselves as they are a form of business rule ("by default, we want X's to be Y & Z"), and the domain represents the business. With this approach, maybe a static "GetNew()" method on the model itself would work:

public class Person {
    public string Name { get; set; }
    public DateTime DateOfBirth { get; set; }
    public bool IsAlive { get; set; }
    public List Limbs { get; set; }

    public static Person GetNew() {
        return new Person() { 
            IsAlive = true,
            Limbs = new List() { RightArm, LeftArm, RightLeg, LeftLeg }
        }
    }
}

Unfortunately in our case, we need the collection property to be set to all members of another list, and as this model is decoupled from its Repository/DbContext it doesn't have any way of loading them all.

Crappy solution would be to pass as parameter :

public static Person GetNew(List<Limb> allLimbs) {
    return new Person() { 
        IsAlive = true,
        Limbs = allLimbs
    }
}

Alternatively is there some better way of setting default values for simple & complex model properties?

解决方案

This is an instance of the factory pattern in DDD. It can either be a dedicated class, such as PersonFactory, or a static method, as in your example. I prefer the static method because I see no need to create a whole new class.

As far as initializing the collection, the GetNew method with the collection as a parameter is something I would go with. It states an important constraint - to create a new person entity you need that collection. The collection instance would be provided by an application service hosting the specific use case where it is needed. More generally, default values could be stored in the database, in which case the application service would call out to a repository to obtain the required values.

这篇关于在域驱动设计中为模型属性设置默认值的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆