Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

c# - NHibernate and WCF Serialization(Unidirectional)

Object graph for type '[type]' contains cycles and cannot be serialized if reference tracking is disabled.

I have a simple class hierarchy setup using NHibernate as my persistence layer.

For example:

public class Parent
{
    public virtual IList<Child> Children{get;set;}
}

public class Child
{
    public virtual Parent Parent{get;set;}
}

This is just done to relate a Child to a single parent and is persisted via foreign key in the database. The NHibernate part and persistence works just fine. The issue occurs when exposing this relationship via WCF web service. I realize there is a cyclical reference here and have read a few solutions for LINQ to SQL allowing for unidirectional serialization, however am unable to find a solution when not using a dbml file.


UPDATE

An additional question would be, is it common practice to abstract a set of DTO's to expose via web service as opposed to the original entities? This would solve the serialization issue as the DTO classes would not necessarily need cyclic references(as they are not NHibernate entities).


Additional Update

I came across an article that may be in the right direction. I'm testing this out currently and will post if successful(for article worth's sake).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is best practice to protect your domain and not let it go across process boundaries. I would recommend a DTO implementation to abstract your domain. There are other benefits as well including that you can provide an intention revealing interface(clients shouldn't have to think about how to use the service).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...