I have some replicated code and so looking to make a generic method. I have a common named extension method that I'd like to use in the method. Normally, if it wasn't an extension method, I'd create an interface, restrict the generic parameter class by that interface and then you can use that common method. But this doesn't work with extension methods.
Here is my generic method:
public ActionConfirmation<string> CreateUpdateEntity<TExternalEntity, TQuickbooksEntity>(TExternalEntity entity, CompanyPreferencesFinancialsSystemCommon preferences)
where TExternalEntity : class, OTIS.Domain.IEntity, IFinancials, IExternalMapper<TExternalEntity, TQuickbooksEntity>, new()
where TQuickbooksEntity : class, Intuit.Ipp.Data.IEntity, new()
{
return CreateUpdateQuickBooksEntity<TQuickbooksEntity>(
entity.ToQuickBooksEntity(preferences),
x => x.Id == entity.FinancialsId,
entity.FinancialsId);
}
Attempted Interface
public interface IExternalMapper<TExternalEntity, TQuickbooksEntity>
where TExternalEntity : class, OTIS.Domain.IEntity, new()
where TQuickbooksEntity : class, Intuit.Ipp.Data.IEntity, new()
{
static TQuickbooksEntity ToQuickBooksEntity<TExternalEntity>(this TExternalEntity externalEntity, CompanyPreferencesFinancialsSystemCommon preferences);
}
This produces error:
Extension method must be defined in a non-generic static class
And this
public static class VendorExtensions : IExternalMapper<OTIS.Domain.InventoryMgmt.Vendor, Intuit.Ipp.Data.Vendor>
{
public static Intuit.Ipp.Data.Vendor ToQuickbooksEntity(this OTIS.Domain.InventoryMgmt.Vendor importedVendor)
Results in Static classes cannot implement interfaces
.
I understand why this doesn't work. But don't know how to re-architect the code to support the requirement of using an extension method in a generic method.