Skip to main content
added 792 characters in body
Source Link
DavidG
  • 119.8k
  • 13
  • 232
  • 239

I think you want to be using Action<T> here. For example:

public static void Do<T>(
    Action<T> action,
    T param,
    int retryCount = 3)
{
    var exceptions = new List<Exception>();

    for (int retry = 0; retry < retryCount; retry++)
    {
        try
        {
            action(param);
            return;
        }
        catch (Exception ex)
        {
            exceptions.Add(ex);
        }
    }

    throw new AggregateException(exceptions);
}

You would call this function like this:

Do(s => {
    Console.WriteLine(s);
}, "test", 3);

Based on your comments, it seems that you want to pass in multiple databases and try each one in succession until you find one that works. One simple option would be to remove to retryCount and instead pass in your array.

public static void Do<T>(
    Action<T> action,
    IEnumerable<T> items)
{
    var exceptions = new List<Exception>();

    foreach(var item in items)
    {
        try
        {
            action(item);
            return;
        }
        catch (Exception ex)
        {
            exceptions.Add(ex);
        }          
    }

    throw new AggregateException(exceptions);
}

And now you call it something like this:

Do(s => {
    Console.WriteLine(s);
}, new[] { "db1", "db2", "db3" });

I think you want to be using Action<T> here. For example:

public static void Do<T>(
    Action<T> action,
    T param,
    int retryCount = 3)
{
    var exceptions = new List<Exception>();

    for (int retry = 0; retry < retryCount; retry++)
    {
        try
        {
            action(param);
            return;
        }
        catch (Exception ex)
        {
            exceptions.Add(ex);
        }
    }

    throw new AggregateException(exceptions);
}

You would call this function like this:

Do(s => {
    Console.WriteLine(s);
}, "test", 3);

I think you want to be using Action<T> here. For example:

public static void Do<T>(
    Action<T> action,
    T param,
    int retryCount = 3)
{
    var exceptions = new List<Exception>();

    for (int retry = 0; retry < retryCount; retry++)
    {
        try
        {
            action(param);
            return;
        }
        catch (Exception ex)
        {
            exceptions.Add(ex);
        }
    }

    throw new AggregateException(exceptions);
}

You would call this function like this:

Do(s => {
    Console.WriteLine(s);
}, "test", 3);

Based on your comments, it seems that you want to pass in multiple databases and try each one in succession until you find one that works. One simple option would be to remove to retryCount and instead pass in your array.

public static void Do<T>(
    Action<T> action,
    IEnumerable<T> items)
{
    var exceptions = new List<Exception>();

    foreach(var item in items)
    {
        try
        {
            action(item);
            return;
        }
        catch (Exception ex)
        {
            exceptions.Add(ex);
        }          
    }

    throw new AggregateException(exceptions);
}

And now you call it something like this:

Do(s => {
    Console.WriteLine(s);
}, new[] { "db1", "db2", "db3" });
Source Link
DavidG
  • 119.8k
  • 13
  • 232
  • 239

I think you want to be using Action<T> here. For example:

public static void Do<T>(
    Action<T> action,
    T param,
    int retryCount = 3)
{
    var exceptions = new List<Exception>();

    for (int retry = 0; retry < retryCount; retry++)
    {
        try
        {
            action(param);
            return;
        }
        catch (Exception ex)
        {
            exceptions.Add(ex);
        }
    }

    throw new AggregateException(exceptions);
}

You would call this function like this:

Do(s => {
    Console.WriteLine(s);
}, "test", 3);