65.9K
CodeProject is changing. Read more.
Home

Simple Response Object for ASP.NET

Jul 13, 2014

CPOL
viewsIcon

16134

downloadIcon

77

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.