开发者

Interface segregation in WCF for JSON

I am a newbie in WCF. I was wondering if we can retrive properties from base interface in the REST output.

Please consider following structure. Product includes IVenueView not Venue. Is it possible to only have Venue.Id in Product JSON response?

[DataContract]
public class Product {
        [DataMember]
        public Guid? Id { get; set; }
        [DataMember]
  开发者_运维百科      public string Name { get; set; }
        [DataMember]
        public IVenueView Venue { get; set; }
}

public interface IVenueView {
        [DataMember]
        Guid? Id { get; set; }
}

[DataContract]
public class Venue : IVenueView
{
        [DataMember]
        public Guid? Id { get; set; }
        [DataMember]
        public string Name { get; set; }
} 


Data contracts are all about data - interfaces define behaviors, so they don't really mix up well. The data contract that you have likely will not work (because the serializer doesn't "know" about the Venue type (it only knows about IVenueView), it won't be able to serialize / deserialize instances of Product.


No it is not possible because serialization and deserialization works with the implementation (actual data) not with interfaces. Moreover for pure serialization you will have to use something like:

[DataContract]
[KnownType(typeof(Venue))]
public class Product
{
    [DataMember]
    public Guid? Id { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public IVenueView Venue { get; set; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜