【发布时间】:2018-11-06 16:19:02
【问题描述】:
对不同 API 端点中的同一实体使用多个 DTO 是否是一种好习惯。例如: 我有一个接受以下 Dto 的 api 端点:
public class AddressDto
{
public string City { get; set; }
public string Country { get; set; }
public string Contact { get; set; }
public string Street1 { get; set; }
public string Street2 { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
现在有第二个 Api 接受相同的 dto,但在那个 api 调用中我只使用 Streer1, Street2, Contact 所有其他的都被忽略了。
我是否应该为第二个api endpoint 再写一个DTO,比如:
public class AddressDtoForSecondAPI
{
public string Contact { get; set; }
public string Street1 { get; set; }
public string Street2 { get; set; }
}
【问题讨论】:
-
SOLID 中的inheritance 和/或interface segregation principle 怎么样?
-
两个地址是否都在同一个数据库表中?
-
同意@S.Akbari 也考虑数据完整性。虽然我不确定这是否可能。不管是什么原因,但看起来有风险(如果可能的话)。
-
@S.Akbari,你是对的。但我最终会为同一个实体提供多个类。这是一个好习惯吗?我应该为单个实体创建 3-4 个类吗?
-
@danish,是的,它最终会出现在同一个数据库表中。
标签: c# .net asp.net-web-api2 api-design