I have an existing WCF REST / JSON service that returns lists of different types of Data. I'd like to add to each response a single attribute which represents a revision number.
Say I have a 'Car' class
[DataContract]
public class Car {
[DataMember]
public String make;
[DataMember]
public String year;
}
Currently /cars/ returns an array of Cars, like the following
{ [ {year: "1990", make: "bmw"}, {year: "2010", make: "ferrari"} ] }
Now, I'd like for the response to be
{ revision:"1234", cars:[ {year: "1990", make: "bmw"}, {year: "2010", make: "ferrari"} ]}
This is trivial if I only have a single class of Cars, but my service has hundreds of simple entities and I'd like for each to return the revision attribute and the list of entities. I thought I could do something like the following where I create a generic class to wrap the existing item.
[DataContract]
public class VersionedItem<T> {
String revision;
T item;
[DataMember]
public String revision {
get{}
set{}
}
[DataMember]
public T item {
get{}
set{}
}
}
This almost works great except where I need the following to be returned:
{ revision:"1234", cars:[ {year: "1990", make: "bmw"}, {year: "2010", make: "ferrari"} ] }
This is actually returned
{ revision:"1234", item:[ {year: "1990", make: "bmw"}, {year: "2010", make: "ferrari"} ] }
Question 1: is there any way to specialize an instance of the generic class to specify the correct name for the item attribute (cars in this case)? IE, (total hogwash, but to help get the point across)
public class VersionedCar : VersionedItem<Car>
{
[DataMember(Name="cars")]
public Car item{
get{}
set{}
}
}
Question 2: if not, whats the best way to achieve wrapping all the responses and including a new attribute in all the responses?