views:

198

answers:

2

Because of performance tuning I would like to return only necessary properties. Is there a possibility/workaround? Pseudo / sample code to understand:

[DataContract]
public interface IMemberOverview
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; }
}

[DataContract]
public interface IMemberDetail
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Status { get; set; }

    [DataMember]
    public string Infos { get; set; }
}

public class Member : IMemberOverview, IMemberDetail
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Status { get; set; }

    public string Infos { get; set; }
}

and the service:

[OperationContract]
[WebInvoke(
    Method = "GET",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "ListMembers")]
[KnownType(typeof(Member))]
public List<IMemberOverview> ListMembers()
{
    return MemberDAO.Instance().GetAll();
}

[OperationContract]
[WebInvoke(
    Method = "GET",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "Member/{idString}")]
[KnownType(typeof(Member))]
public List<IMemberDetail> Member(string idString)
{
    var id = int.Parse(idString);
    return MemberDAO.Instance().Get(id);
}
+1  A: 

If you want to expose a different view on your object model, then the simplest approach is simply to expose the data on a slimmer DTO - i.e. write a second class (typically in a different namespace) and translate the data between them. Either by hand (write some code, perhaps a static conversion operator, that does this), or with utilities like PropertyCopy (like so).

Having a separate DTO also allows you to extend your core object model without silently impacting client performance, and makes versioning much simpler.

Marc Gravell
A: 

If I understand your question correctly, [DataMember(EmitDefaultValue=false)] may help.

Eugene Osovetsky