【发布时间】:2020-12-12 17:39:56
【问题描述】:
我一直在为我的应用程序的命名约定和设计模式而苦苦挣扎,很难保持一致,所以我有一个简单的案例,假设我有一个方法名为 CreateOrder 的服务
public OrderDTO CreateOrder(int customerID, OrderShipDTO shipping, OrderDetailDTO products);
这是 DTO 类
public class OrderDTO
{
public int ID { get; set; }
public decimal PriceTotal { get; set; }
public string Status { get; set; }
public string PaymentMethod { get; set; }
public OrderShipDTO OrderShip { get; set; }
public ICollection<OrderDetailDTO> OrderDetails { get; set; }
}
public class OrderShipDTO
{
public string Name { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public string Province { get; set; }
public string City { get; set; }
public string District { get; set; }
public string SubDistrict { get; set; }
public string ZipCode { get; set; }
}
public class OrderDetailDTO
{
public int ID { get; set; }
public decimal Quantity { get; set; }
public decimal Price { get; set; }
public int ProductID { get; set; }
}
如您所见,我的方法CreateOrder将返回OrderDTO并接受OrderDetailDTO参数,但该方法实际上只需要属性OrderDetailDTO.ProductID和OrderDetailDTO.Quantity进行业务逻辑计算。
所以,我觉得传递整个 OrderDetailDTO 对象并不适合(并且令人困惑,因为我不确定哪些属性需要有值,哪些不需要),即使它只需要 2 个属性要填写,但我仍然需要传回 OrderDTO,其中将包括 ICollection<OrderDetailDTO>,因为我需要获取 OrderDetailDTO.Price 值并将其展示给我的客户。
所以我正在考虑创建另一个这样的 DTO
public class OrderDetailDTO_2 //temp name
{
public decimal Quantity { get; set; }
public int ProductID { get; set; }
}
但是我最终会得到很多 DTO,即使我对它很好,DTO 命名的最佳实践是什么?
【问题讨论】:
-
您是否愿意将 OrderDetailDTO_2 作为 OrderDetailDTO 的抽象类? & 订单详情更改为
public ICollection<OrderDetailDTO_2> OrderDetails { get; set; }?
标签: c# design-patterns dto data-transfer-objects