I have multiple interfaces that my objects can implement. I am wondering if there is a way to “cascade” one extension method into another while using the same method name. I may be looking at this all wrong, but here is an example:
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;}
}
So, let’s say I want to convert the DTOs into entities using an extension method. An example would be MeetingDto.ToEntity();
I am trying to think if I can write part of the extension method for an IBaseDto, another for the IDocumentDto and then for each concrete implementations for just their own properties. When I call MeetingDto.ToEntity() it would first hit the meeting extension method and call the IDocumentDto version, fill in what it needed, and then the IDocumentDto would call the IBaseDto. I hope this makes sense.
UPDATE:
I came up with this and it works pretty well:
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)
}
Like this?