[英]How to return from Inner function in c#?
I have a function that is a wrapper for an external REST web API. 我有一个函数,它是外部REST Web API的包装器。 In my function, I am checking for the required parameters and if they're there, then I make the request and return the data.
在我的函数中,我正在检查所需的参数,如果它们在那里,那么我发出请求并返回数据。 If a required parameter is missing, I log the name of the missing parameter and return status as failure.
如果缺少必需参数,则记录缺失参数的名称并将状态返回为失败。
I have extracted the logging the missing parameter part to a function as it will be called multiple times across the function. 我已将缺少参数部分的日志记录提取到函数中,因为它将在函数中多次调用。 Here is my code as it stands:
这是我的代码:
public string WrapFunc(Dictionary<string, string> parameter)
{
//Check for required params
if (parameter.ContainsKey("Param1") && !string.IsNullOrEmpty(parameter["Param1"]))
{
objWebAPiRequest.Param1 = parameter["Param1"];
}
else
{
LogRequiredParameterError("Param1");
response = "FAILURE";
return response;
}
}
private void LogRequiredParameterError(string parameterName)
{
//Logging the missing parameter name to db
}
I'm looking for a way to encapsulate the returning of the response in the LogRequiredParameterError
method or better still an elegant way of returning the failure message without repeating myself. 我正在寻找一种方法来封装在
LogRequiredParameterError
方法中返回响应,或者更好的方法是返回失败消息而不重复自己。 Any suggestions? 有什么建议么?
Add the missing parameters to a list as you go along, then process them once (and set the response to "FAILURE" once) at the end of the method. 在您继续时将缺少的参数添加到列表中,然后在方法结束时处理它们一次(并将响应设置为“FAILURE”一次)。
public string WrapFunc(Dictionary<string, string> parameter)
{
var response = "SUCCESS";
var missingParams = new List<string>();
//Check for required params
if (parameter.ContainsKey("Param1") && !string.IsNullOrEmpty(parameter["Param1"]))
{
objWebAPiRequest.Param1 = parameter["Param1"];
}
else
{
// Add the name of the missing param to a list
missingParams.Add("Param1");
}
if (missingParams.Any())
{
// Log all the missing parameters and set response to "FAILURE"
foreach (var p in missingParams)
LogRequiredParameterError(p);
response = "FAILURE";
}
return response;
}
private void LogRequiredParameterError(string parameterName)
{
//Logging the missing parameter name to db
}
Well, it's just 2 lines to replace, but you could do something like this: 好吧,它只需要2行代替,但你可以这样做:
public string WrapFunc(Dictionary<string, string> parameter)
{
//Check for required params
bool hasAllParams = true;
hasAllParams = CheckParam(parameter, "Param1") && hasAllParams;
//hasAllParams = CheckParam(parameter, "Param2") && hasAllParams; etc.
if (hasAllParams)
{
//proceed normally
response = "SUCCESS"; //might be worth making it a const
}
else
{
response = "FAILURE";
}
return response;
}
private bool CheckParam(Dictionary<string, string> parameters, string paramName)
{
if (!parameters.ContainsKey(paramName) || string.IsNullOrEmpty(parameters[paramName]))
{
LogRequiredParameterError(paramName);
return false;
}
return true;
}
Alternatively, you could write specific checking functions for other parameters if there are different conditions for them other than just not being empty. 或者,如果存在不同的条件,您可以为其他参数编写特定的检查函数,而不仅仅是不为空。
And to answer your original question - no, there is no way to call a method and have it force a return
in its caller. 并回答你原来的问题 - 不,没有办法调用一个方法并强制它在调用者中
return
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.