简体   繁体   English

具有不同数量参数的代表

[英]Delegates with varying number of parameters

I have written a DLL which calls off to a third-party API which I do not have access to.我编写了一个 DLL,它调用了我无权访问的第三方 API。

Because of how the user could quit and restart the application every method has lots of logic around the checking of Access and Refresh tokens in order to authenticate with the API.由于用户可以退出和重新启动应用程序的方式,每个方法都有很多围绕检查访问和刷新令牌的逻辑,以便通过 API 进行身份验证。 (As well error handling processing) This has resulted in lots of duplicated code for each different API call. (以及错误处理处理)这导致每个不同的 API 调用都有大量重复的代码。

I want to refactor this so I can just pass the type of response object I want to a generic method called ExecuteApiCall.我想重构它,这样我就可以将我想要的响应对象类型传递给一个名为 ExecuteApiCall 的通用方法。 Which can then call an appropriate method for just the specific REST call.然后可以为特定的 REST 调用调用适当的方法。

I have created this method with the following signature:我用以下签名创建了这个方法:

private T ExecuteApiCall<T>(string name, Action<T> requestCall) where T : IApiResponse, new()

and this works as I would expect it.这正如我所期望的那样工作。 The problem I have now is that a few of the requests require extra parameters and I can't pass a varying number of parameters to an Action delegate.我现在遇到的问题是一些请求需要额外的参数,我无法将不同数量的参数传递给 Action 委托。

How do I go about handling this?我该如何处理?

I was thinking of changing to我想改成

Action<T, ApiRequestParameters>

where ApiRequestParameters is a class of all possible parameters and the public facing method can set what it needs to before calling the private ExecuteApiCall.其中 ApiRequestParameters 是所有可能参数的类,面向公众的方法可以在调用私有 ExecuteApiCall 之前设置它需要的参数。 But this doesn't really feel like best practice.但这并不是真正的最佳实践。

I really hope this makes sense to someone and thanks in advance.我真的希望这对某人有意义,并提前致谢。 Happy to provide further code examples if required.如果需要,很乐意提供更多的代码示例。

The following needs to be handled需要处理以下问题

ExecuteCallA()
{
    //The API call is done here using RestSharp
}

ExecuteCallB(string aParameter)
{
    //The API call is done here using RestSharp
}

ExecuteCallC(string aParameters, int anotherParameter)
{
    //The API call is done here using RestSharp
}

Or would it be easier to just set the Action to look something like this.或者将 Action 设置为看起来像这样会更容易。

Action<T, object, object, object>

So that it can handle extra parameters and just ignore any it does not need.这样它就可以处理额外的参数,而忽略任何它不需要的参数。

EDIT:编辑:

Thanks to Sean for the suggestion, this is likely what I will go with.感谢肖恩的建议,这可能是我会去的。 My other option, again not sure on the best practices here...我的另一个选择,再次不确定这里的最佳实践......

Would be to have the public signature contain the method parameters and set these to a private field.将让公共签名包含方法参数并将它们设置为私有字段。

private string myParameter;
public ApiResponseA GetWhatever(string a)
{
    myParameter = a;
    ExecuteApiCall<ApiResponseA>();
    myParameter = null;
}

Then change the private method responsible for the actual API call to use the private field instead of a parameter.然后将负责实际 API 调用的私有方法更改为使用私有字段而不是参数。 Thoughts?想法?

I'd add overloads for a certain number of parameters.我会为一定数量的参数添加重载。 For example:例如:

private T ExecuteApiCall<T>(string name, Action<T> requestCall) where T : IApiResponse, new();

private T ExecuteApiCall<P1, T>(string name, P1 p1, Action<P1, T> requestCall) where T : IApiResponse, new()

private T ExecuteApiCall<P1, P2, T>(string name, P1 p1, P2 p2, Action<P1, P2, T> requestCall) where T : IApiResponse, new()

// etc

It's a bit repetitive to implement, but once you've done it you can forget about it, and it'll make the API you expose to users a lot cleaner and predictable.实现起来有点重复,但是一旦完成,您就可以忘记它,它会使您向用户公开的 API 更加清晰和可预测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM