There is a bit of if-else-if going in within the code. Is there a construct which could come handy. Also the section names are very similar too
public string GetRestApiUrlFromHost()
{
var restApiUrl = string.Empty;
var HostUrl = Request.Host.Value;
var DevURL = _configuration.GetSection("DEV_API_URL").Value;
var QAURL = _configuration.GetSection("QA_API_URL").Value;
var ProdURL = _configuration.GetSection("PROD_API_URL").Value;
if (HostUrl.Contains(DevURL, StringComparison.CurrentCultureIgnoreCase))
{
restApiUrl = _configuration.GetSection("DEV_REST_API_URL").Value;
}
else if (HostUrl.Contains(QAURL, StringComparison.CurrentCultureIgnoreCase))
{
restApiUrl = _configuration.GetSection("QA_REST_API_URL").Value;
}
else if (HostUrl.Contains(ProdURL, StringComparison.CurrentCultureIgnoreCase))
{
restApiUrl = _configuration.GetSection("PROD_REST_API_URL").Value;
}
else
{
restApiUrl = _configuration.GetSection("Local_REST_API_URL").Value;
}
return restApiUrl;
}
Looks redundant to me. But cannot place it. Which would be the most ideal way to refactor this code?
-
\$\begingroup\$ Welcome to Code Review! Are you the author or maintainer of this code? \$\endgroup\$Sᴀᴍ Onᴇᴌᴀ– Sᴀᴍ Onᴇᴌᴀ ♦2022年02月02日 16:04:57 +00:00Commented Feb 2, 2022 at 16:04
-
1\$\begingroup\$ We need to know what the code is intended to achieve. To help reviewers give you better answers, please add sufficient context to your question, including a title that summarises the purpose of the code. We want to know why much more than how. The more you tell us about what your code is for, the easier it will be for reviewers to help you. The title needs an edit to simply state the task, rather than your concerns about the code. \$\endgroup\$Toby Speight– Toby Speight2022年02月02日 16:45:03 +00:00Commented Feb 2, 2022 at 16:45
-
1\$\begingroup\$ @SᴀᴍOnᴇᴌᴀ: Yes I am a maintainer of the code. I didn't like the way its done. Was thinking of a Dictionary<string, string> approach. Got a little bit stuck up with work while adding the code. \$\endgroup\$codeandcloud– codeandcloud2022年02月02日 17:32:00 +00:00Commented Feb 2, 2022 at 17:32
-
\$\begingroup\$ @TobySpeight: Three environments for the project and three file servers for each. Was getting the file server base url. Thanks. Sorry that my question was not clear enough. First time user here.(Not at Stackoverflow though) \$\endgroup\$codeandcloud– codeandcloud2022年02月02日 17:35:06 +00:00Commented Feb 2, 2022 at 17:35
1 Answer 1
You could create an array of tuples with url keys and section keys and loop them like this:
(string urlKey, string sectionKey)[] keys = new [] {
("DEV_API_URL", "DEV_REST_API_URL"),
("QA_API_URL", "QA_REST_API_URL"),
("PROD_API_URL", "PROD_REST_API_URL")
};
string sectionKey = "Local_REST_API_URL";
foreach (var key in keys) {
if (HostUrl.Contains(key.urlKey, StringComparison.CurrentCultureIgnoreCase)) {
sectionKey = key.sectionKey;
break;
}
}
return _configuration.GetSection(sectionKey).Value;
Or with LINQ (with same tuple array). .NET 6:
var key = keys.FirstOrDefault(
k => HostUrl.Contains(k.urlKey, StringComparison.CurrentCultureIgnoreCase),
(null, "Local_REST_API_URL")
);
return _configuration.GetSection(key.sectionKey).Value;
Uses the FirstOrDefault(IEnumerable, Func<TSource,Boolean>, TSource) extension method overload available since .NET 6
Framework versions prior to .NET 6:
var key = keys
.Where(
k => HostUrl.Contains(k.urlKey, StringComparison.CurrentCultureIgnoreCase))
.DefaultIfEmpty((null, "Local_REST_API_URL"))
.First();
);
return _configuration.GetSection(key.sectionKey).Value;
-
\$\begingroup\$ Thanks.
HostUrl.Contains(_configuration.GetSection(key.urlKey)...
should be my case \$\endgroup\$codeandcloud– codeandcloud2022年02月02日 17:34:13 +00:00Commented Feb 2, 2022 at 17:34