Click here to Skip to main content
15,891,864 members
Articles / Web Development / HTML
Tip/Trick

Simple Response Object for ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.22/5 (7 votes)
13 Jul 2014CPOL 15.6K   77   8   3
Simple response object for ASP.NET

Introduction

The aim is to create a simple response object from Web services which will have:

  • A generic data holder to hold specific typed data
  • A flag to trace if the execution completed without any error or not
  • If there is any error, store the exception too.

Background

Here is the simple response object, which has a generic container to hold data.

/*Older Version: if you have used this one avoid it and use the new version*/
/*Or see Comments and Discussions why it was important to change to code*/
//public class Response<TSource>
//{
//    /*if any response data, success true*/
//    public readonly bool? IsSuccess;
//    public readonly TSource Data;
//    /*if exception, error would be true*/
//    public readonly bool? IsError;
//    public readonly Exception Exception;
//    private Response()
//    {
//        IsSuccess = IsError = null;
//        Data = default(TSource);
//        Exception = null;
//    }
//    private Response(bool isSuccess)
//        : this()
//    {
//        IsSuccess = isSuccess;
//    }
//    /*sets response data*/
//    public Response(TSource data)
//        : this(true)
//    {
//        Data = data;
//    }
//    /*sets error*/
//    public Response(Exception exception)
//        : this(false)
//    {
//        IsError = true;
//        Exception = exception;
//    }
//}

/*New Version*/
public class Response<TSource>
{
    /*if any response data and not exception type, success true*/
    public readonly bool IsSuccess;
    public readonly TSource Data;
    /*if any exception data, success false*/
    public readonly Exception Exception;

    private Response()
    {
        Data = default(TSource);
        Exception = null;
    }
    private Response(bool isSuccess)
        : this()
    {
        IsSuccess = isSuccess;
    }

    /*sets response data*/
    public Response(TSource data)
        : this(true)
    {
        Data = data;
    }

    /*sets error*/
    public Response(Exception exception)
        : this(false)
    {
        Exception = exception;
    }
}

Using the Code

We can use this response object like:

/*Specify what type of data this response can hold, here it is string type*/
Response<string> response = null;

/*Adding data to response*/
response = new Response<string>("Hello World"); //IsSuccess = true, Exception = null
              //Data = "Hellow World"
/*Adding error to response*/
response = new Response<string>(new Exception("Error.")); //IsSuccess = false, 
                    //Exception = exceptionData
                    //Data = null

/*Important thing to know*/
/*Adding exception as expected data to response is not acceptable*/
Response<Exception> response1 = null;
response1 = new Response<Exception>
    (new Exception("Error.")); //IsSuccess = false, Exception = exceptionData
                                         //Data = null

Using it in ASP.NET MVC:

public JsonResult GetAll()
{
    Response<List<Student>> response = null;
    try
    {
        List<Student> students = _logic.Get();
        response = new Response<List<Student>>(students);
    }
    catch
    {
        response = new Response<List<Student>>(new Exception("Error."));
    }
    return Json(response);
}

Using it in Web Service:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Get(string name)
{
    Response<Student> response = null;
    try
    {
        Student student = _logic.Get(name);
        response = new Response<Student>(student);
    }
    catch
    {
        response = new Response<Student>(new Exception("Error."));
    }
    return new JavaScriptSerializer().Serialize(response);
}

Or with any method:

public Response<String> Get(string name)
{
    //
    //Do what we need to do.
    //
    return Response<Student>("done");
}

Find the VS2010 console project solution in the attachment.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHello DiponRoy Pin
Member 1340703812-Sep-17 19:59
Member 1340703812-Sep-17 19:59 
General[My vote of 2] Poor state management Pin
John Brett14-Jul-14 3:42
John Brett14-Jul-14 3:42 
GeneralRe: [My vote of 2] Poor state management Pin
DiponRoy14-Jul-14 8:34
DiponRoy14-Jul-14 8:34 
Hi jhon,

lets illustrate your confusion in short note first.
Q1. why IsSuccess and IsError are nullable?
Q2. what would it mean if IsSuccess and IsError were both true?, or both null?
Q3. why use both ?


lest start
Q.2
----
there is no chance of having IsSuccess and IsError as both true/false/null. Why,
I have only two public constructor and two private constructor. and public constructor is chained with the private constructor. as you can see you have to use atleast one constructor.

And I am not giving any chance to user to set the IsSuccess or IsError
they could only be seted using public constructors.

if you put expected data, which you specified at new Response<tsource>
IsSuccess = true,
IsError = null(IsSuccess is true means no error, no need to care about IsError, why would you check it again)
Data = (expected data)
Exaception = null

But if you put exception data, even if you specified at new Response<exception>
IsSuccess = false,
IsError = true
Data = null
Exaception = exaction it self



But yes what your are saying is right, at fist glance any new contributor for the project may face confusion considering what if the its null. although it would obviously not expose something like this. hmmm .... !!


Yes i should change it right know


Thank you

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.