C# Castle ActiveRecord: How to elegantly (XML) serialize ActiveRecord objects?

三世轮回 提交于 2019-12-04 19:10:42

Fetch everything you need eagerly, then use AutoMapper to map it to DTOs and serialize those DTOs.

The proxy error you're getting suggests that your DTOs are actually using NHibernate's proxied collections, when they should be using plain List<T> or arrays instead. Make sure you use ToList() or similar.

Also, you don't need to use interfaces to map DTOs.

Try the DataContractSerializer.

Its equivalent of [XmlInclude] is [KnownType], which includes a version that can dynamically supply the type to include the first time the type is reflected. See http://msdn.microsoft.com/en-us/library/ms730167.aspx

Also, I think (though not 100% sure) that .NET4.0 will include a "Type Mapper" feature in the DataContractSerializer which specifically makes scenarios like this easier.

If you do end up trying the DCS and will run into questions, post as comments to this answer and I'll try to answer them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!