Click here to Skip to main content
15,890,845 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 

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.