user1025901
user1025901

Reputation: 1909

Making generic Methods in C#

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

Upvotes: 1

Views: 345

Answers (2)

Maheep
Maheep

Reputation: 5605

Declare and define Execute method like below

public T Execute<T>(Func<X, T> clientCall)
{
    T 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;
}

And call this method like this

var result = Execute<PartnerListingResponse >(client => client.GetRenewalListingRecords(listingRequest));

Upvotes: 1

curiosity
curiosity

Reputation: 201

I suggest to create a base class like ListingResponse from which you derive the two subclasses RenewalListingResponse and PartnerListingResponse.

Then you have only one method that will work for all types of ListingResponse:

public ListingResponse Execute(Func<X, ListingResponse> clientCall)
    {
        ListingResponse response = null;

        using (dynamic client = new X())
        {
            try
            {
                // invoke client method passed as method parameter
                response = clientCall(client);

            }
            ...
         }
        return response;
     }

Upvotes: 0

Related Questions