2017-01-23 3 views
2

Ich habe viele Funktionen mit unterschiedlichem Inhalt, aber die Parameter und versuchen fangen innen ist fast ähnlich. Gibt es trotzdem eine Umhüllung der Funktion, so dass redundante Codes reduziert werden können.Generic Function wrapper

ResponseStatus GetPotatoList(GetPotatosRequest requestParam, out GetPotatosResponse response, out ResponseErrorType errorType) 
{ 
    ResponseStatus status = ResponseStatus.Fail; 
    response = new GetPotatosResponse(); 

    //To Do 

    try 
    { 

     //To Do 

     status = ResponseStatus.Success; 
    } 
    catch(CustomException ex) 
    { 
     errorType = ResponseErrorType.CustomError; 
    } 
    catch(TimeoutException ex) 
    { 
     errorType = ResponseErrorType.Timeout; 
    } 
    catch(Exception ex) 
    { 
     errorType = ResponseErrorType.GeneralFailure; 
    } 

    return status; 
} 

Antwort

6

Sie können eine Action an Ihre Methode übergeben.

ResponseStatus GetPotatoList(Action action1, Action action2, GetPotatosRequest requestParam, out GetPotatosResponse response, out ResponseErrorType errorType) 
{ 
    ResponseStatus status = ResponseStatus.Fail; 
    response = new GetPotatosResponse(); 

    action1(); 

    try 
    { 
     action2(); 
     status = ResponseStatus.Success; 
    } 
    catch(CustomException ex) 
    { 
     errorType = ResponseErrorType.CustomError; 
    } 
    catch(TimeoutException ex) 
    { 
     errorType = ResponseErrorType.Timeout; 
    } 
    catch(Exception ex) 
    { 
     errorType = ResponseErrorType.GeneralFailure; 
    } 

    return status; 
} 

dann verwenden:

var response = GetPotatoList(
    () => doSomething(), 
    () => doSomethingElse(), 
    requestParam, 
    out response, 
    out errorType); 
+0

Ist der Zugriff auf den Wert in action1() und action2() möglich? –

+0

@Amigo warum brauchst du es? Wenn Sie Parameter übergeben müssen, können Sie eine typisierte Aktion verwenden: 'Aktion ' zum Beispiel, und Sie können es wie folgt setzen: '(myInt) => doSomething (myInt)' und verwenden Sie es wie folgt: 'action1 (213); '; Ich lade Sie ein, wenn Sie sie noch nicht kennen, um nach 'C# lambdas' zu suchen, um zu verstehen, wie es funktioniert. – Kilazur

1

Stattdessen Aktion verwenden, sollten Sie vielleicht eine Funktion, die die Anforderung als Parameter nehmen und senden Sie Ihre Antwort-Objekt, dann können Sie profitieren von Generika nehmen zu machen der Anruf und dann bestimmte Fälle behandeln. Auch das Zurückgeben eines Tupels oder eines generischen Typs für das Ergebnis könnte eine gute Idee sein, anstatt Out-Parameter zu verwenden.

public static Tuple<TResponse, ResponseStatus, ResponseErrorType> GetResponse<TRequest, TResponse>(Func<TRequest, TResponse> action, TRequest request) 
{ 
    var status = ResponseStatus.Fail; 
    var errorType = ResponseErrorType.None; 
    var response = default(TResponse); 

    try 
    { 
     response = action(request); 
     status = ResponseStatus.Success; 
    } 
    catch (CustomException ex) 
    { 
     errorType = ResponseErrorType.CustomError; 
    } 
    catch (TimeoutException ex) 
    { 
     errorType = ResponseErrorType.Timeout; 
    } 
    catch (Exception ex) 
    { 
     errorType = ResponseErrorType.GeneralFailure; 
    } 

    return new Tuple<TResponse, ResponseStatus, ResponseErrorType>(response, status, errorType); 
} 
Verwandte Themen