【发布时间】:2012-01-24 21:07:56
【问题描述】:
我的对象可以实现多个接口。我想知道是否有一种方法可以在使用相同的方法名称时将一个扩展方法“级联”到另一个扩展方法中。我可能看错了,但这里有一个例子:
public interface IBaseDto
{
int Id {get;set;}
string CreatedByFullName {get;set;}
}
public interface IDocumentDto
{
List<ContactDto> Subscriptions {get;set;}
}
public class ContactDto: IBaseDto
{
public int Id {get;set;}
public string CreatedByFullName {get;set;}
public string FirstName {get; set}
public string LastName {get;set;}
}
public class MeetingDto: IDocumentDto
{
public int Id {get;set;}
public string CreatedByFullName {get;set;}
public List<ContactDto> Subscriptions {get;set;}
}
所以,假设我想使用扩展方法将 DTO 转换为实体。一个例子是MeetingDto.ToEntity();
我正在考虑是否可以为IBaseDto 编写扩展方法的一部分,为IDocumentDto 编写另一个扩展方法,然后为每个具体实现编写它们自己的属性。当我调用MeetingDto.ToEntity()时,它会先点击会议扩展方法并调用IDocumentDto版本,填写它需要的内容,然后IDocumentDto会调用IBaseDto。我希望这是有道理的。
更新:
我想出了这个,效果很好:
public static TBaseDto ToEntity<TBridgeDto>(this TBaseDto dto) where TBaseDto: IBaseDto
{
...
return dto;
}
public static TDocumentDto ToEntity<TDocumentDto>(this TDocumentDto dto, IDocumentDto currentDto) where TDocumentDto : IDocumentDto
{
...
return dto.ToEntity();
}
public static MeetingDto ToEntity(this RfiDto dto)
{
...
return dto.ToEntity(dto)
}
【问题讨论】:
-
为什么要使用扩展方法?您是否认为您的 DTO 更干净,因为
ToEntity()方法是在其他地方定义的?您对 的描述中的其他所有内容似乎都表明,每个子类覆盖的 IBaseDTO 上的ToEntity()方法是理想的。顺便说一句,MeetingDto 也应该从 IBaseDTO 继承吗? -
automapper 可能是你的朋友。
-
@DanielA.White,我正在使用 Automapper 来生成我的 DTO,但是从 DTO 到实体的转换是有问题的。此外,还有一些我们正在做的映射属性之外的事情。
-
@perfectist,我们已经有很多基础设施,创建扩展方法似乎比更新我们所有的 DTO 更容易。
标签: c# extension-methods