我正在将“域对象”->“汇编程序”->“数据传输对象”(DTO)模式写入我的共享库中,以允许表示层和服务层通过 DTO 进行通信。我避免了任何共享接口以允许在 DTO 中进行粗粒度聚合。我对“CreateDTO”方法有扎实的掌握,但想知道如何在 C# 的汇编程序中实现 UpdateDomainObject(DTO dto) 方法。我正在考虑使用以下结构来简化我的代码:
public class SomeAssembler
{
public static SomeDTO CreateDTO(SomeDomainObject obj)
{
dto.Property1 = obj.Property1;
...
}
//METHOD IN QUESTION:
public static void UpdateDomainObject(SomeDTO dto, SomeDomainObject obj)
{obj.Property1 = dto.Property1 ...}
}
我在方法中包含域对象参数的原因是允许如下代码:
//Presentation Layer Code (PL signifies presentation layer type)
ContactPL : IContact //(IContact is an Entity library contract shared across layers in separate DLL)
{
#region Properties
//Note Mixed Types, so params not an option
public int Id {get;set}
public String FirstName {get;set;}
public string LastName {get;set;}
public string PhoneNumber {get;set;}
public Address address {get;set;}
#endregion
#region Methods
//METHOD IN QUESTION:
public void GetContact(int id)
{
ContactDTO dto = ContactService.GetContactbyId(id);
ContactAssembler.UpdateContact(dot, this);
}
// Other Methods...
#endregion
}
请注意上面标记为“//METHOD IN QUESTION:”的方法。我的问题是,“这种结构是否可以接受和/或以这种方式编写代码是否有任何顾虑?”
我知道Java处理这些问题如下。我想确保“UpdateDomainObject”的 My Assembler 方法及其在表示层模型对象中的包含是可接受的或理想的。如果没有,有什么更好的给猫剥皮的方法吗?
Java 示例 - 从 DTO 更新的汇编程序方法(仅用于比较 - 请用 C# 术语回答):
public static void updateCustomer(CustomerDTO dto) {
Customer target = null;
for(Customer c: Domain.customers) {
if (dto.name.equals(c.getName())) {
target = c;
break;
}
}
if (target != null) {
target.setAddress(dto.address);
target.setPhone(dto.phone);
}
}