Using JSON.NET to serialize proxied NHibernate objects

952 views
Skip to first unread message

Roy.Jacobs

unread,
Jul 27, 2011, 7:52:10 AM7/27/11
to nhu...@googlegroups.com
Hi all,

Just thought I'd let you know that I ran into issues with JSON.NET not being able to serialize NHibernate objects that contained any proxies due to lazy loading. With a bit of googling and hacking I was able to solve it by adding a converter and resolver to JSON.NET that automatically proxies any objects that implement INHibernateProxy.

http://www.royjacobs.org/2011/07/27/using-json-net-to-serialize-proxied-nhibernate-objects/

--
Roy

Jason Meckley

unread,
Jul 27, 2011, 12:14:15 PM7/27/11
to nhu...@googlegroups.com
this topic has been covered many times over. most times the best approach is to create contextual dtos from the domain objects and serialize the dto. Keeping the domain within the scope of the NH session.

José F. Romaniello

unread,
Jul 27, 2011, 12:30:27 PM7/27/11
to nhu...@googlegroups.com
I second what Jason says.
Martin Fowler first law of Distributed Objects Design is "don't distribute your objects"; http://martinfowler.com/bliki/FirstLaw.html

2011/7/27 Jason Meckley <jasonm...@gmail.com>
this topic has been covered many times over. most times the best approach is to create contextual dtos from the domain objects and serialize the dto. Keeping the domain within the scope of the NH session.

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/VZoRv22HGo0J.

To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.

Roy Jacobs

unread,
Jul 28, 2011, 2:59:25 AM7/28/11
to nhu...@googlegroups.com
Oh certainly, but the way we build contextual dto's as an interim solution is by marking objects as [DataContract] certain properties as [DataMember], basically. This should control the amount of properties that get serialized, essentially building up dto's that way. However, when JSON.NET walks this tree, it will find the proxy which does not contain a [DataContract] and it will therefore serialize the *entire* object, which is something we don't want.

Roy
Reply all
Reply to author
Forward
0 new messages