I have the below program in MEF
Method 1:
public ObjectResult<PartnerListingStatement> GetCommissionListingRecords(string uRL, PortalConstant.DataSourceType DataSourceType)
{
ObjectResult<PartnerListingStatement> lstCommissionPartner = null;
var dataPlugin = DataPlugins.FirstOrDefault(i => i.Metadata["SQLMetaData"].ToString() == DataSourceType.EnumToString());
if (dataPlugin != null)
{
lstCommissionPartner = dataPlugin.Value.GetCommissionListingRecords(uRL);
}
return lstCommissionPartner;
}
Method B
public ObjectResult<CommissionEarned> GetCommissionPaidToPartners(string uRL, PortalConstant.DataSourceType DataSourceType)
{
ObjectResult<CommissionEarned> lstCommissionEarned = null;
var dataPlugin = DataPlugins.FirstOrDefault(i => i.Metadata["SQLMetaData"].ToString() == DataSourceType.EnumToString());
if (dataPlugin != null)
{
lstCommissionEarned = dataPlugin.Value.GetCommissionPaidToPartners(uRL);
}
return lstCommissionEarned;
}
Using generics or the like can these two be combined. Also the data types are different.
N.B.~ This question is different than Generics program to access WCF service from client
Thanks
The first question to ask after asking “Can I combine these methods?” is “What do these methods have in common?” I your case, the answer to that would be something like this:
where
***SomeType***and***SomeMethod***are the two meaningful differences between the methods. The deal with the type, make the method generic and replace all the***SomeType***with the generic parameter. To deal with the method, add a delegate parameter to the method. Based on its usage, the delegate will be of theFunc<PluginType, string, ObjectResult<***SomeType***>>type where PluginType is whatever typedataPlugin.Valueis. Now you have:which is changes GetCommissionListingRecords to (the generic type should be inferred)
and similarly for the other method.