【发布时间】:2018-04-17 12:31:41
【问题描述】:
我需要找到更好的解决方案:
public MappingProfile()
{
CreateMap<DesireDto, Desire>()
.ForMember(x => x.CreationDate, x => x.Ignore())
.ForMember(x => x.ModifiedDate, x => x.Ignore())
.ForMember(x => x.ModifiedUser, x => x.Ignore())
.ForMember(x => x.CreationUser, x => x.Ignore())
.ForMember(x => x.Language, x => x.Ignore());
CreateMap<ProductDto, Product>()
.ForMember(x => x.CreationDate, x => x.Ignore())
.ForMember(x => x.ModifiedDate, x => x.Ignore())
.ForMember(x => x.ModifiedUser, x => x.Ignore())
.ForMember(x => x.CreationUser, x => x.Ignore())
.ForMember(x => x.Language, x => x.Ignore());
}
我的条件相同,但这看起来真的很丑
这个帖子什么都没有:https://github.com/AutoMapper/AutoMapper/issues/2236
【问题讨论】:
-
SRP 是最好的。鉴于您已经分离了 DTO,您可以控制小的更改。如果你把这些东西粘在一起,你就会开始违反 SRP。它们是不同的东西,如果一个改变,它不应该影响另一个。就这样保持吧。有时代码重复是有好处的。
-
它们不是相同的条件,它们是具有相同名称的不同条件(您命名它们)。它们的全名是 DesireCreationDate/ProductCreationDate/etc。除非您的业务中的某个地方一起处理 DesireCreationDate/ProductCreationDate,否则抽象像
IWithCreationDate这样的接口是毫无价值的。
标签: c# .net-core automapper