views:

323

answers:

1

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).

+1  A: 

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).

Adam Fyles
I've actually spent the better part of the day getting accustomed to AutoMapper and some lighter DTO's to send across the wire as opposed to the domain entities.
Alexis Abril