Skip to content

Instantly share code, notes, and snippets.

@mteece
Created November 2, 2011 20:07
Show Gist options
  • Save mteece/1334732 to your computer and use it in GitHub Desktop.
Save mteece/1334732 to your computer and use it in GitHub Desktop.
C# wrapper for generic objects response. Useful for objects in WCF with Ajax and JSON.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
/*
using My.Domain.Namespace;
[DataContract(),KnownType(typeof(ExampleObject))]
*/
public class RecordResponseObject<T>
{
private T _data;
private bool _success = true;
private int _total;
private string _message;
[DataMember()]
public bool success
{
get { return _success; }
set { _success = value; }
}
[DataMember()]
public string message
{
get { return _message; }
set { _message = value; }
}
[DataMember()]
public T data
{
get { return _data; }
set { _data = value; }
}
public RecordResponseObject(ref T data)
{
_data = data;
}
}
/*
Example method for RecordResponseObject returning List of ExampleObject wrapped.
public RecordResponseObject<List<ExampleObject>> Save(List<ExampleObject> data)
{
List<ExampleObject> response = new List<ExampleObject>();
foreach (ExampleObject item in data) {
int id = ExampleObject.Save(item);
ExampleObject record = ExampleObject.Find(id);
response.Add(record);
}
return new RecordResponseObject<List<ExampleObject>>(response);
}
*/
@UlyssesAlves
Copy link

Thank you very much.

@widnu
Copy link

widnu commented May 11, 2021

Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment