在当前项目中,我们决定使用 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/