Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在领域驱动设计中使用具有较大聚合的工厂方法?_C#_Domain Driven Design_Factory Pattern - Fatal编程技术网

C# 如何在领域驱动设计中使用具有较大聚合的工厂方法?

C# 如何在领域驱动设计中使用具有较大聚合的工厂方法?,c#,domain-driven-design,factory-pattern,C#,Domain Driven Design,Factory Pattern,我在我的一个有界上下文中使用领域驱动设计,我使用工厂方法创建我的领域聚合,并强制执行我的业务规则和不变量,但我的问题是,有时我有大量属性的大型聚合,而这些属性中的大多数只是数据属性,而不是数据属性行为方法不会影响聚合的状态,因此,使用工厂方法填充所有这些属性是一场噩梦,请就如何使用最佳实践向我提供建议 public async Task CreateEmployeeAsync(CreateOrUpdateEmployeeInput input) { Guard.Argum

我在我的一个有界上下文中使用领域驱动设计,我使用工厂方法创建我的领域聚合,并强制执行我的业务规则和不变量,但我的问题是,有时我有大量属性的大型聚合,而这些属性中的大多数只是数据属性,而不是数据属性行为方法不会影响聚合的状态,因此,使用工厂方法填充所有这些属性是一场噩梦,请就如何使用最佳实践向我提供建议

public async Task CreateEmployeeAsync(CreateOrUpdateEmployeeInput input)
    {
        Guard.ArgumentNotNull(input.Employee, nameof(input.Employee));
        var employeeDto = input.Employee;
        Address address = null;
        if (employeeDto.Address != null)
            address = new Address(employeeDto.Address.Street, employeeDto.Address.City, employeeDto.Address.State,
                employeeDto.Address.Country, employeeDto.Address.ZipCode);

        var employee = Employee.Create(employeeDto.FirstName, employeeDto.LastName, employeeDto.Email,
            employeeDto.DateOfJoining, employeeDto.DepartmentId, employeeDto.EmployeeType,
            employeeDto.ReportTo, employeeDto.Title, employeeDto.AllowSystemAccess, address);

        //These properties are just data properties, they will not affect the state of the aggregate
        //This is a nightmare to fill each one hand by hand or even using Factory
        employee.MobilePhone = input.Employee.MobilePhone;
        employee.WorkPhone = input.Employee.WorkPhone;
        employee.MaritalStatus = input.Employee.MaritalStatus;
        await _employeeRepository.InsertAsync(employee);
    }
移动电话和工作电话等“数据属性”是聚合状态的一部分。还有与之相关的业务规则。电话号码必须是有效的。MaritalStatus可以是3个预定义值之一,不需要WorkPhone

创建Employee对象时需要验证这些属性。 我可以建议将它们添加到构造函数中。 在这种情况下,域模型有许多属性,无法避免设置它们。我不推荐自动化的东西,因为它会以某种方式隐藏一些属性。下一个使用此代码的开发人员可能会说:“我看不出手机被设置在哪里”,这将增加处理该部分代码时的认知负荷。您需要了解域模型和自动映射或用于自动填充属性的工具,以便了解正在发生的事情


有很多属性并不一定是坏事。如果您想改进聚合设计,您可以将一些属性分组到一个新的值对象中,如果这对您的业务有意义的话。例如,您可以创建包含电子邮件和电话号码的ContactInfo值对象。

不确定问题是什么。您是否在问,是否有比自己编写代码更自动化的方法来填充属性?或者你是在问你是否可以省略一些属性?我的意思是我需要一种自动的方法来填充这些属性,但只填充那些会影响使用工厂方法进行聚合的属性扫描你给我们展示了一些示例代码?我添加了一个示例你考虑过使用类似的方法吗?