德米特混乱定律 - c#

我希望有人能帮助我解释神算法则。如果我假设一个类是一个聚合根,并且其中有一个子类集合,那么通过聚合根访问这些子类的属性来更新这些子类的属性是否合法?

例如

public class Company
{
    // company has a number of employees
    public List<Employee> Employees {get; set;}
}

public class Employee
{
    // each employee has a lastname
    public int Id {get; set;}
    public string LastName {get; set;}
    // other properties of employee
}

可以说,我有一个客户首先进入Company类,这会违反诸如demeter之类的法律。

Employee e = aCompany.Employees.Where(e => e.Id == 1).Single();
e.LastName = "MarriedName";

还是应始终将此委托给公司

public class Company
{
    public UpdateEmployeeLastName(int employeeId, string newName)
    {
        Employee e = Employees.Where(e => e.Id == employeeId).Single();
        e.LastName = newName;
    }
}

在客户中

aCompany.UpdateEmployeeLastName(1, "Marriedname");

第二个似乎更好,但是客户端必须知道它想要更新的Employee的ID有什么问题吗?

如果您有许多嵌套聚合,这似乎会变得复杂起来。

谢谢

参考方案

您的第二个选择是Demeter法则的目标。

由于Demeter的定律基本上说“只谈论您所知道的事情”。在第一种情况下,无论“客户”是什么,实际上根本不了解员工。它知道Company ..但不知道Company内部的复杂性。

委托Company使您可以灵活地更改员工的更新方式,而不必从客户端更改此功能的每个特定实例。如果有一天您决定只能更改Active个雇员的姓名,那么您将不得不将选项一的每个实例更新为:

Employee e = aCompany.Employees.Where(e => e.Id == 1 && e.IsActive).Single();
//                                                        ^^^^ active flag
e.LastName = "MarriedName";

将其包装在Company中会使将来处理起来更加轻松(无论是否尝试遵循Demeter法则)。

第二个似乎更好,但是客户端必须知道它想要更新的Employee的ID有什么问题吗?

您的两个示例都知道Employee的ID。因此,我不确定您的意思。当通过Aggregate传递信息时,使用代码时要知道ID是很常见的。

LeetCode题解371.sum-of-two-integers

题目地址 https://leetcode.com/problems/sum-of-two-integers/description/ 题目描述 Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example 1: …

调用glDrawArrays之后的GL_OUT_OF_MEMORY。为什么? - java

我的情况似乎很奇怪。我将尝试提供足够的详细信息,以便比我聪明的人可以对此进行解释。基本上这里是设置:OS: Android 2.2 Froyo Device: SGS Vibrant Application: OpenGL-ES 1.1 问题就在这里:我可以成功渲染一个相当复杂的场景,并且它可以连续运行数小时而不会泄漏任何内存。 Dalvikvm每3-5分钟…

LeetCode题解342.power-of-four

题目地址 https://leetcode.com/problems/power-of-four/description/ 题目描述 Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example 1: Input: 16 Out…

LeetCode题解200.number-of-islands

题目地址 https://leetcode.com/problems/number-of-islands/description/ 题目描述 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by …

LeetCode题解1020.number-of-enclaves

题目地址 https://leetcode-cn.com/problems/number-of-enclaves/ 题目描述 给出一个二维数组 A,每个单元格为 0(代表海)或 1(代表陆地)。 移动是指在陆地上从一个地方走到另一个地方(朝四个方向之一)或离开网格的边界。 返回网格中无法在任意次数的移动中离开网格边界的陆地单元格的数量。   示例 1: 输入…