通用函数包装器

4
我有很多内容不同的函数,但参数和try catch几乎相似。 有没有办法封装这些函数以减少重复代码?
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;
}
3个回答

10

您可以将Action传递给您的方法。

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;
}

然后使用它:

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

能否访问action1()和action2()内部的值? - Z.V
@Amigo 你需要它来干什么?如果你需要传递参数,你可以使用一个带类型的Action:例如Action<int>,你可以这样设置它:(myInt) => doSomething(myInt),并像这样使用它:action1(213);;如果你还不知道,我邀请你搜索“C# lambdas”以了解它的工作原理。 - Kilazur

1
我需要在调用一个原始方法之前和之后提供功能,而这个方法的签名没有太大变化。
我使用了 Func<..>...
    public static Func<string, string> Hello = name => "hello " + name;

    public static string Hello2(string name) => wrap(Hello)(name);

    // This does NOT retain the name of the arg for hints in the IDE 
    public static Func<string, string> Hello3 = name => wrap(Hello)(name);

    private static Func<string, T> wrap<T>(Func<string, T> orig)
    {
        return name => orig(name.ToUpper());
    } 

0

不要使用Action,而是应该使用一个函数,将请求作为参数并返回响应对象,然后您可以利用泛型来进行调用,然后处理特定情况。此外,返回元组或某个通用类型作为结果可能是一个好主意,而不是使用输出参数。

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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接