I have a generic class with the methods being defined as under
public abstract class BaseService<X> where X : new()
{
public PartnerListingResponse Execute(Func<X, PartnerListingResponse> clientCall)
{
PartnerListingResponse response = null;
using (dynamic client = new X())
{
try
{
// invoke client method passed as method parameter
response = clientCall(client);
}
catch (TimeoutException ex)
{
Console.WriteLine("The service operation timed out. " + ex.Message);
client.Abort();
}
catch (CommunicationException ex)
{
Console.WriteLine("There was a communication problem. " + ex.Message + ex.StackTrace);
client.Abort();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
client.Abort();
}
}
return response;
}
public RenewalListingResponse Execute(Func<X, RenewalListingResponse> clientCall)
{
RenewalListingResponse response = null;
using (dynamic client = new X())
{
try
{
// invoke client method passed as method parameter
response = clientCall(client);
}
catch (TimeoutException ex)
{
Console.WriteLine("The service operation timed out. " + ex.Message);
client.Abort();
}
catch (CommunicationException ex)
{
Console.WriteLine("There was a communication problem. " + ex.Message + ex.StackTrace);
client.Abort();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
client.Abort();
}
}
return response;
}
Actually there is no difference between the two Execute method except for the ResponseType …
I am calling the methods as under
public Tuple<int, string, List<PartnerListing>> GetEarningListingRecords(string uRL)
{
//Create the request
PartnerListingRequest listingRequest = new PartnerListingRequest();
listingRequest.URL = uRL;
//Send the request and get the service response
var result = Execute(client => client.GetEarningListingRecords(listingRequest));
//Send the response back to client
return Tuple.Create(result.ResultCode, result.ResultMessage, result.PartnerListings);
}
public Tuple<int, string, List<RenewalListing>> GetRenewalListingRecords(int PartnerId)
{
//Create the request
RenewalListingRequest listingRequest = new RenewalListingRequest();
listingRequest.PartnerId = PartnerId;
//Send the request and get the service response
var result = Execute(client => client.GetRenewalListingRecords(listingRequest));
//Send the response back to client
return Tuple.Create(result.ResultCode, result.ResultMessage, result.RenewalListings);
}
But I want to make the Execute methods as generic so that for every response type I should not have to write a new method..is it possible and if so please help me in doing so..
Thanks
Declare and define Execute method like below
And call this method like this