开发者

Webservice Serialize IEnumerable Property

I have a rather tricky problem that has been bugging me the last 2 days.

开发者_如何学C

This is my class:

public class MyClass: BaseEntity<int>
    {
        //Some other properties here...

        public virtual Categorie Categorie { get; set; }
        public virtual Bron Bron { get; set; }
        public virtual IEnumerable<Label> Labels { get; set; }
    }

I'm forced to use NHibernate. So I have my mapping done and the IEnumerable can't be a List<T> (It can be a IList<T>). But when you call my webservice it can't serialize my IEnumerable since it is an Interface, and the same goes for IList. And since I use nhibernate I can't change my class layout as far as I know.

So my question basicly is: Can I tell the Serializer that it should threat it as a List? Or is there a generic collection class that will serialize the Labels and won't make nhibernate angry? Or anything else that would work?


Not exactly an answer to your question, but maybe an answer to your problem :)

Sending entities across process boundaries is not a very good idea. You sould instead project your entities to DTOs and send those.

If you expose your entities structure in your service contract then your entities are coupled to every consumer of your service (Bad). If you define the service contract in therms of simple DTOs the consumers will only depend on them and not on your entities (Good).

The projection of the entities to DTOs can be also done automatically ( with automapper ).

Also you will avoid a few other problems related to lazy loading entities.

If you still want to send the entity over the wire than maybe you can mark your IEnumerable property as non serializable and use a another property of an array type that just returns the IEnumerable Property.ToArray(), and don't map the array property in NHibernate.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜