Reduce If Else condition in ASP.Net MVC

Richa
 
on Apr 27, 2022 04:43 AM
715 Views

Hi,

Could someone please help me to refactor the "if else code" below in a more simplified way:

public async Task<IActionResult> GetEtasjeAsync(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "etasjer")]
HttpRequest req, ILogger log)
{
    try
    {
        // Check if there is an access token to authorize the call to this endpoint
        var accessToken = _azureAdJwtBearerValidation.GetAccessToken(req);
 
        if (accessToken == null) return new UnauthorizedResult();
 
        // validate that the access token is valid by checking that the signature is valid
        var validateAccessToken = await _azureAdJwtBearerValidation.ValidateAccessTokenAsync(accessToken, log);
 
        if (validateAccessToken == null) return new UnauthorizedResult();
 
        var param = "page";
        var param1 = "size";
 
        if (req.Query.ContainsKey(param) && !req.Query.ContainsKey(param1))
        {
            if (int.TryParse(req.Query[param], out var number) && !int.TryParse(req.Query[param1], out var size))
            {
                if (number > 0 )
                {
                    var result = await _services.EtasjeQueryServiceAsync(req, number, 1000);
 
                    if (result == null) return new NotFoundResult();
 
                    if (result.Content.Count == 0) return new NoContentResult();
 
                    return new OkObjectResult(result);
                }
                return new BadRequestResult();
            }
            return new BadRequestResult();
        }
        else if(req.Query.ContainsKey(param1) && !req.Query.ContainsKey(param))
        {
            if(int.TryParse(req.Query[param1], out var size) && !int.TryParse(req.Query[param], out var number))
            {
                if(size > 0)
                {
                    var result = await _services.EtasjeQueryServiceAsync(req, 1, size);
 
                    if (result == null) return new NotFoundResult();
 
                    if (result.Content.Count == 0) return new NoContentResult();
 
                    return new OkObjectResult(result);
                }
                return new BadRequestResult();
            }
            return new BadRequestResult();
        }
        else if(req.Query.ContainsKey(param) && req.Query.ContainsKey(param1))
        {
            if(int.TryParse(req.Query[param], out var number) && int.TryParse(req.Query[param1], out var size))
            {
                if(number > 0 && size > 0)
                {
                    var result = await _services.EtasjeQueryServiceAsync(req, number, size);
 
                    if (result == null) return new NotFoundResult();
 
                    if (result.Content.Count == 0) return new NoContentResult();
 
                    return new OkObjectResult(result);
                }
                return new BadRequestResult();
            }
            return new BadRequestResult();
        }
        else if (!req.Query.ContainsKey(param) && !req.Query.ContainsKey(param1))
        {
            if (!int.TryParse(req.Query[param], out var number) && !int.TryParse(req.Query[param1], out var size))
            {                      
                var result = await _services.EtasjeQueryServiceAsync(req, 1, 1000);
 
                if (result == null) return new NotFoundResult();
 
                if (result.Content.Count == 0) return new NoContentResult();
 
                return new OkObjectResult(result);
            }
            return new BadRequestResult();
        }
         
        var response = await _services.EtasjeServiceAsync(1, 1000, req);
 
        if (response == null) return new NotFoundResult();
 
        if (response.Content.Count == 0) return new NoContentResult();
 
        return new OkObjectResult(response);
    }
    catch (Exception ex)
    {
        log.LogError(ex, ex.Message);
        throw;
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Apr 27, 2022 05:00 AM

Your code is already simlified.

As there are different condition checked no further simplification possible.

Richa
 
on Apr 27, 2022 07:20 AM

My team lead has asked me to write the code in a different way. The current approach in which the code is written is the only way I know as of now. 

May be I dont have his level of expertise.

 

var param = "page";
var param1 = "size";

Here, page is for pagination and size is the number of records per page.

 

Is there any other way to use page and size parameter and put it differently in If else condition ?? I have removed the unnecessary code:

if (req.Query.ContainsKey(param) && !req.Query.ContainsKey(param1))
{
	if (int.TryParse(req.Query[param], out var number))
	{
		if (number > 0 )
		{
			//var result = await _services.EtasjeServiceAsync(number, 1000, req);
			var result = await _services.EtasjeQueryServiceAsync(req, number, 1000);

			if (result == null) return new NotFoundResult();

			if (result.Content.Count == 0) return new NoContentResult();

			return new OkObjectResult(result);
		}
		return new BadRequestResult();
	}
	return new BadRequestResult();
}
else if(req.Query.ContainsKey(param1) && !req.Query.ContainsKey(param))
{
	if(int.TryParse(req.Query[param1], out var size))
	{
		if(size > 0)
		{
			//var result = await _services.EtasjeServiceAsync(1, size, req);
			var result = await _services.EtasjeQueryServiceAsync(req, 1, size);

			if (result == null) return new NotFoundResult();

			if (result.Content.Count == 0) return new NoContentResult();

			return new OkObjectResult(result);
		}
		return new BadRequestResult();
	}
	return new BadRequestResult();
}
else if(req.Query.ContainsKey(param) && req.Query.ContainsKey(param1))
{
	if(int.TryParse(req.Query[param], out var number) && int.TryParse(req.Query[param1], out var size))
	{
		if(number > 0 && size > 0)
		{
			//var result = await _services.EtasjeServiceAsync(number, size, req);
			var result = await _services.EtasjeQueryServiceAsync(req, number, size);

			if (result == null) return new NotFoundResult();

			if (result.Content.Count == 0) return new NoContentResult();

			return new OkObjectResult(result);
		}
		return new BadRequestResult();
	}
	return new BadRequestResult();
}
else if (!req.Query.ContainsKey(param) && !req.Query.ContainsKey(param1))
{                    
	var result = await _services.EtasjeQueryServiceAsync(req, 1, 1000);

	if (result == null) return new NotFoundResult();

	if (result.Content.Count == 0) return new NoContentResult();

	return new OkObjectResult(result);                 
}

 

 

Thank you in advance.

dharmendr
 
on Apr 27, 2022 10:39 AM
Richa says:
            var result = await _services.EtasjeQueryServiceAsync(req, number, 1000);
 
            if (result == null) return new NotFoundResult();
 
            if (result.Content.Count == 0) return new NoContentResult();
 
            return new OkObjectResult(result);

Since the above code is common you can make it as method with parameterised and call inside each condition.

Richa
 
on Apr 28, 2022 04:53 AM
on Apr 28, 2022 04:59 AM

Thank you for the reply sir.

 

Just curious to know, is there any other way (may be using "dictionary" and "for" loop) instead of using "If Else" in my code which would still check for page and size.

i.e,  kindly suggest some way to write the same functionality without If Else.

Bcoz in future, if conditions might increase.

 

Thank you in advance

dharmendr
 
on Apr 28, 2022 05:18 AM
on Jul 14, 2022 08:27 AM

Since there are multiple condition in the if or else part, no other way you can write the condition.