0

Suppose I have web api application using ASP.NET. In one of the actions I'm checking if I can activate a free trial period for the given user. Now from one point of view, result of this action is either successful or unsuccessful. But in case of an unsuccessful result, I'd like to inform the client why the request has been denied and then the client can decide what to do.

There are multiple if-checks in this action and each one of them can deny the incoming request. Further more, we can have different successful result conditions as well.

What is the right way to return these different conditions so the client can easily distinguish between them?

What I'm doing right now is something like this:

public IHttpActionResult ActivateFreeTrial()
{
     BaseResult result = new BaseResult();  // This class has a property called Status

     if(!FirstCondition)
     {
        // Unsuccessful
        result.Status = 90;
        return Ok(result)
     }
     if(!SecondCondition)
     {
        // Unsuccessful
        result.Status = 91;
        return Ok(result)
     }

     // Some more checks...

     if(!SixthCondition)
     {
         // Successful
         result.Status = 1;
         return Ok(result);  
     }

     else {
         // Successful
         result.Status = 2;
         return Ok(result); 
     }       
}

Client needs to know which condition generated the result so that's why I'm assigning a number to distinguish between the different conditions. So in client-side I can do something like:

CallActivateFreeTrial()
.then(function(res){
    if (res.Status == 1)
    {
       // Successful result but with status 1
    }
    else if (res.Status == 2)
    {
       // Successful result but with status 2
    }
    else if (res.Status == 90)
    {
       // Unsuccessful result but with status 90
    }
    else if (res.Status == 91)
    {
       // Unsuccessful result but with status 91
    }          
}, function(error){

})

I hardly think what I'm doing right now is considered to be a good practice. Is there a better way to return different conditional results to the client?

1 Answer 1

0

I would like to know where you get !FirstCondition, !SecondCondition values to confirm my answer but I believe it's a bad practice. If you have a different result, then you'll change ActivateFreeTrial method. I believe a strategy or state pattern could help you. So instead of return "status code" to your client, you can return the name of a function that client should call.

Strategy pattern

http://www.dofactory.com/net/strategy-design-pattern

State pattern

http://www.dofactory.com/net/state-design-pattern

Sign up to request clarification or add additional context in comments.

4 Comments

You can see FirstCondition and SecondCondition and so on, as some database query result. For example if a user has already used a trial period then FirstCondition would be false and lead to a unsuccessful result. But as a client I'd like to know it was because of this reason that my request is being rejected and then perform an appropriate action.
Got it. So it's better to setup a View Model with your possibilities (FirstCondition, SecondCondition,etc) and share it with all strategies. Then, send a json object to your client handle it. Example: var json = { success = false, message = "already used trial period"};
Let's see. What if in the client, I would like to do a specific action when the result is "already used trial period"? Then I'd have to check if( res.message == "already used trial period") ? Specific action could be redirect user to a specific page/state or something similar that is only done for that condition. Remember there are some other conditions as well so I have to check the specific message for them too?
I'd go with the responsibility in the server side. So in case of "already used trial period", set the name of the function that should be fired in client side. stackoverflow.com/a/17145752/1384539

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.