mercredi 30 septembre 2020

How to implement interface dependent on Action and Func? Action implementation returns nothing while Func returns

I need to implement an interface to implement strategy pattern in C#.

One step would be to CreateRetryInvocationContext and second would be to execute.

Second step can be of two types ) i.e. Func or Action. Retry operation could return a value or might returns void.

So, I already have two concrete implementation for this ( i.e. RetryOperationWithResult and RetryOperarationWithoutResult ). Both of implementations are implementing 'Execute' method. But the problem is WithResult needs to return and other one dont need. How can I persue with this? Which way I can arrange interfaces to have better orchestration and less burden on client side. Kind of balanced and well testable implementation on both ends.

Here is my proposed Class hierarchy :

 public class RetryOperationWithResult<T> : IRetryProcessor<T>
{
    private Func<List<object>, T> _action;

    public RetryOperationWithResult(Func<List<object>, T> action)
    {
        _action = action;
    }

    public T Execute(RetryOperationContext retryOpContext)
    {
        T result = default(T);
        try
        {
            //Retry logic 
            //
        }
        catch(Exception ex)
        {

        }

        return result;
    }
}

and the other for Action implementation. Of course following doesn't get compiled.

class RetryOperationWithOutResult : IRetryProcessor<void>
{
     public void Execute(RetryOperationContext retryOpContext)
     {
         throw new NotImplementedException();
     }
}

Aucun commentaire:

Enregistrer un commentaire