mercredi 6 avril 2016

c# or vb Generic function to retry code block n number of times

I am trying to create a generic function to which I can specify the method to be called & number of times it should try getting result before it fails.

Something like:

//3 stands for maximum number of times GetCustomerbyId should be called if it fails on first attempt.
var result = RetryCall(GetCustomerbyId(id),3);

Secondly the return type should be adjusted automatically based on function it is calling.

For example I should be able to get result from both of following function, one returns string & other Customer entity.

public static string GetCustomerFullNamebyId(int id){
    return dataContext.Customers.Where(c => c.Id.Equals(id)).SingleOrDefault().FullName;
}

public static Customer GetCustomerbyId(int id){
   return dataContext.Customers.Find(id);
}

Is this possible?

Warm Regards





Aucun commentaire:

Enregistrer un commentaire