0

I found this method (function?) from another SO question and I'm unsure how I'm to use it. I'd like to use it to catch connectivity issues to my DB (over a wireless network) and retry or throw other exceptions. My application only has a couple of database accessing functions so I was about to just copy and paste everything but I don't want to have to update every function 10 times if I need to change something.

Method I want to use:

        private void Retry<T>(Action<T> retryAction) where T : DataContext, new()
    {
        var retryCount = 0;
        using (var ctx = new T())
        {
            for (; ; )
            {
                try
                {
                    retryAction(ctx);
                    break;
                }
                catch (SqlException ex)
                {
                    if (!Enum.IsDefined(typeof(RetryableSqlErrors), ex.Number))
                        throw;

                    retryCount++;
                    if (retryCount > MAX_RETRY) throw;

                    Thread.Sleep(ex.Number == (int)RetryableSqlErrors.Timeout ?
                                                            longWait : shortWait);
                }
            }
        }
    }

Example of functions I want to pass to it:

public async Task<List<EmployeeDisplay>> GetInspectorsAsync()
    {
        using (M1Context m1 = new M1Context())
        {

            var q = (from a in m1.Employees
                     where (a.lmeTerminationDate == null)
                     && (a.lmeInspectorEmployee == -1)
                     orderby a.lmeEmployeeName
                     select new EmployeeDisplay
                     {
                         EmployeeID = a.lmeEmployeeID,
                         EmployeeName = a.lmeEmployeeName.ToUpper().Trim()
                     });

            return await q.ToListAsync();
        }

    }


       public async Task<List<Reason>> GetScrapReasonsAsync()
    {
        using (M1Context m1 = new M1Context())
        {

            Debug.WriteLine("getting scrap reasons");
            var q = (from a in m1.Reasons
                     where a.xarReasonType.Trim().Equals("S")
                     select a);

            return await q.ToListAsync();

        }
    }
Community
  • 1
  • 1
ErpaDerp
  • 780
  • 1
  • 7
  • 16
  • why would you write the method 10 times when all you need to do is create a custom class where you can access it and or make a static class and then call the method from the 10 said different places.. I hope I am understanding your question correctly.. – MethodMan Nov 03 '15 at 23:26
  • This is a pretty generic retry method for handling `SqlException` what else would you want to do with it? – SimpleVar Nov 03 '15 at 23:39
  • You're understanding correctly MethodMan, and the answer is not a good one. Just needed to get something done and in my lack of experience I only knew one quick and dirty way to get it done. Luckily I have you guys to help me out! As for what else I'd want to do with it, I'm not sure at the moment but I hate they idea of having duplicate methods floating around when I can just create a custom class for it. – ErpaDerp Nov 04 '15 at 00:11
  • Oh also, I want to eventually add some logging functionality so I probably want to do some of that in this catch block. – ErpaDerp Nov 04 '15 at 02:02

1 Answers1

0

To to use the method you need to do the following

  1. Your Context

enter image description here

  1. Your Retry Method

enter image description here

  1. Your Do Stuff method

enter image description here

  1. Enum

enter image description here

Julius Depulla
  • 1,493
  • 1
  • 12
  • 27
  • I'm partially able to follow this, but I'm a little lost on how to call the method. I'm assuming that MyRetryActionMethod() in your example would be like the GetInspectorsAsync() method from mine. Do you think you could expand a bit on the //Do Stuff portion of your example? I'm not familiar with this procedure of creating the method as you did above: MyRetryActionMethod = (mycontext ) => { //Do Stuff }; – ErpaDerp Nov 04 '15 at 02:09
  • I have added example for you – Julius Depulla Nov 04 '15 at 06:25