c# - 编写解析 DTO 的代码的更简洁方法

标签 c# entity-framework design-patterns

在当前项目中,我们决定使用 dto 在服务器和客户端之间传输数据。

Dto是平的,奉承不是问题,可以轻松搞定。但是 dto unflattering 可能会变得很难实现,因为用户可能会删除、创建和更新扁平化实体图的某些部分。

这是 Web 服务方法之一的示例代码:

    [Update, EmpresaHasPermissions("PERMIT_INS_Employee")]
    public void UpdateBackground(EmployeeBackgroundDTO dto)
    {
        using (var context = GetObjectContext())
        {
            var user = EmpresaAuthentication.Current.User;

            Employee employee = context.Employees
                .Include(it => it.Nationality)
                .Include(it => it.EthnicOrigin)
                .Include(it => it.MaritalStatus)
                .Include(it => it.Religion)
                .Include(it => it.CRB)
                .Include(it => it.Passport)
                .Single(it => it.OwnerOrganizationId == user.OrganizationId &&
                              !it.Deleted && it.Id == dto.Id);

            var updater = new EmployeeBackgroundUpdater(context);

            updater.UpdateEntity(employee, dto);

            context.SaveChanges();

            dto.MaritalStatusId = employee.MaritalStatusId;
            dto.EthnicOriginId = employee.EthnicOriginId;
            dto.ReligionId = employee.ReligionId;
        }
    } 

正如你所看到的,这里混合了很多东西: 情境创造 数据选择 调用数据更新器 将新创建的 dto 的 id 发送回客户端

当您看到 EmployeeBackgroundUpdater 的实现方式时,事情开始变得有值(value):

public override void UpdateEntity(Employee employee, EmployeeBackgroundDTO dto)
{
    employee.InjectFrom(dto);

    if (!IsPassportNull(dto))
    {
        if (employee.Passport == null)
        {
            employee.Passport = new Passport();
        }

        employee.Passport.IssueDate = dto.PassportIssueDate.Value;
        employee.Passport.ExpiryDate = dto.PassportExpiryDate.Value;
        employee.Passport.PassportNo = dto.PassportPassportNo;
        employee.Passport.IssuingCountryId = dto.PassportIssuingCountryId.Value;
        employee.Passport.OwnerUserId = UserId;
    }
    else
    {
        if (employee.Passport != null)
        {
            DeleteObject(employee.Passport);
            employee.Passport = null;
        }
    }

    if (!IsCRBNull(dto))
    {
        if (employee.CRB == null)
        {
            employee.CRB = new CRB();
        }

        employee.CRB.IssueDate = dto.CRBIssueDate.Value;
        employee.CRB.ExpiryDate = dto.CRBExpiryDate.Value;
        employee.CRB.Registration = dto.CRBRegistration;
        employee.CRB.Notes = dto.CRBNotes;
    }
    else
    {
        if (employee.CRB != null)
        {
            DeleteObject(employee.CRB);
            employee.CRB = null;
        }
    }

    var epmpresaContext = (EmpresaEntities)ObjectContext;

    AddMaritalStatus(employee, dto, epmpresaContext);

    AddReligion(employee, dto, epmpresaContext);

    AddEthnicOrigin(employee, dto, epmpresaContext);

    employee.NationalityId = dto.NationalityId;
}

private void AddMaritalStatus(Employee employee, EmployeeBackgroundDTO dto, EmpresaEntities epmpresaContext)
{
    if (!dto.MaritalStatusId.HasValue && !String.IsNullOrWhiteSpace(dto.MaritalStatusDescription))
    {
        var item = epmpresaContext.MaritalStatuses.FirstOrDefault(
            it => it.Description.ToUpper() == dto.MaritalStatusDescription.ToUpper());

        if (item == null)
        {
            employee.MaritalStatus = new MaritalStatus
            {
                Description = dto.MaritalStatusDescription
            };
        }
        else
        {
            employee.MaritalStatus = item;
        }
    }
    else
    {
        employee.MaritalStatusId = dto.MaritalStatusId;
    }
}

代码结构相同,唯一的区别是实体集的类型和实体类型。这很可怕,因为如果我们选择更新验证逻辑或类似的东西,我将不得不在项目的许多不同位置重写相同的代码。

感谢您阅读至此。我提出了一系列问题:

1) 如何将可以有子对象的平面 dto 解析为有效的实体图?

2)dto(或者可能的表示模型)应该包含对象的层次结构吗?

3)如何去掉重复代码?

最佳答案

要将实体解析为 DTO,反之亦然,您可以检查 AutoMapper 。 DTO 可以是对象的层次结构(不必展平)。恐怕您永远无法避免一些重复代码,因为每种实体类型在某种程度上都是特殊的,您必须手动处理它 - 这就是使用 DTO 所包含的复杂性。

关于c# - 编写解析 DTO 的代码的更简洁方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6211493/

相关文章:

c# - MVC 路由匹配除资源之外的所有路由

c# - 使用 C# 在多级文件夹结构中查找 Azure 存储容器中的 Blob

design-patterns - 在某些情况下,单例是最佳选择吗?

design-patterns - 是否有用于实现可选功能的良好设计模式?

c# - 将 DateTime 的 Date 部分序列化为 4​​ 个字节的最快方法?

c# - .NET Core 中 Assembly.GetEntryAssembly() 的等价物是什么?

linq - EF如何按日期过滤数据

c# - 带有 IOC : "The current type is an interface and cannot be constructed" 的身份框架

entity-framework - Entity Framework -联合会导致 "Unable to create a constant value of type.."

c++ - 单例实现和对::GetInstance 的 undefined reference