Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple Response Object for ASP.NET

0.00/5 (No votes)
13 Jul 2014 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here