Architecture DDD-贫血与富领域模型

Architecture DDD-贫血与富领域模型,architecture,domain-driven-design,Architecture,Domain Driven Design,我读过Eric Evan的DDD书,他(以及Fowler和其他人)似乎认为贫血领域模型是一种反模式 所以我只是想了解一下这个问题 另外,我正在寻找一些好的(基本的)富域模型示例,以及它提供的相对于贫乏域模型的好处。假设您编写了一些代码来解决复杂的业务问题。如果您有贫血领域模型,则 对象上几乎没有任何行为,使得它们只不过是一袋袋的getter和setter。[…]相反,存在一组服务对象,它们捕获所有域逻辑,执行所有计算并使用结果更新模型对象 以下是一个非常简单的示例: 贫血 class Box {

我读过Eric Evan的DDD书,他(以及Fowler和其他人)似乎认为贫血领域模型是一种反模式

所以我只是想了解一下这个问题


另外,我正在寻找一些好的(基本的)富域模型示例,以及它提供的相对于贫乏域模型的好处。

假设您编写了一些代码来解决复杂的业务问题。如果您有贫血领域模型,则

对象上几乎没有任何行为,使得它们只不过是一袋袋的getter和setter。[…]相反,存在一组服务对象,它们捕获所有域逻辑,执行所有计算并使用结果更新模型对象

以下是一个非常简单的示例:

贫血

class Box
{
    public int Height { get; set; }
    public int Width { get; set; }
}
class Box
{
    public int Height { get; private set; }
    public int Width { get; private set; }

    public Box(int height, int width)
    {
        if (height <= 0) {
            throw new ArgumentOutOfRangeException(nameof(height));
        }
        if (width <= 0) {
            throw new ArgumentOutOfRangeException(nameof(width));
        }
        Height = height;
        Width = width;
    }

    public int Area()
    {
    return Height * Width;
    }
}
非贫血性

class Box
{
    public int Height { get; set; }
    public int Width { get; set; }
}
class Box
{
    public int Height { get; private set; }
    public int Width { get; private set; }

    public Box(int height, int width)
    {
        if (height <= 0) {
            throw new ArgumentOutOfRangeException(nameof(height));
        }
        if (width <= 0) {
            throw new ArgumentOutOfRangeException(nameof(width));
        }
        Height = height;
        Width = width;
    }

    public int Area()
    {
    return Height * Width;
    }
}
类框
{
公共整数高度{get;私有集;}
公共整数宽度{get;私有集;}
公用框(整数高度、整数宽度)
{

if(height假设您编写了一些代码来解决复杂的业务问题

对象上几乎没有任何行为,这使得它们只不过是一包包的getter和setter。[..]相反,有一组服务对象捕获所有域逻辑,执行所有计算并用结果更新模型对象

以下是一个非常简单的示例:

贫血

class Box
{
    public int Height { get; set; }
    public int Width { get; set; }
}
class Box
{
    public int Height { get; private set; }
    public int Width { get; private set; }

    public Box(int height, int width)
    {
        if (height <= 0) {
            throw new ArgumentOutOfRangeException(nameof(height));
        }
        if (width <= 0) {
            throw new ArgumentOutOfRangeException(nameof(width));
        }
        Height = height;
        Width = width;
    }

    public int Area()
    {
    return Height * Width;
    }
}
非贫血性

class Box
{
    public int Height { get; set; }
    public int Width { get; set; }
}
class Box
{
    public int Height { get; private set; }
    public int Width { get; private set; }

    public Box(int height, int width)
    {
        if (height <= 0) {
            throw new ArgumentOutOfRangeException(nameof(height));
        }
        if (width <= 0) {
            throw new ArgumentOutOfRangeException(nameof(width));
        }
        Height = height;
        Width = width;
    }

    public int Area()
    {
    return Height * Width;
    }
}
类框
{
公共整数高度{get;私有集;}
公共整数宽度{get;私有集;}
公用框(整数高度、整数宽度)
{

如果(高度@SubhashBhushan)这个问题与这里的原因相同,那么这个问题在那里是不合适的。请不要推荐你不熟悉的网站。请看@SubhashBhushan这个问题与这里的原因相同。请不要推荐你不熟悉的网站。请看