简体   繁体   English

NHibernate和WCF序列化(单向)

[英]NHibernate and WCF Serialization(Unidirectional)

Object graph for type '[type]' contains cycles and cannot be serialized if reference tracking is disabled. 类型“ [type]”的对象图包含循环,并且如果禁用了引用跟踪,则无法序列化。

I have a simple class hierarchy setup using NHibernate as my persistence layer. 我有一个使用NHibernate作为持久层的简单类层次结构设置。

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. 这样做是为了将一个Child与一个单亲相关联,并通过外键保留在数据库中。 The NHibernate part and persistence works just fine. NHibernate部分和持久性工作得很好。 The issue occurs when exposing this relationship via WCF web service. 通过WCF Web服务公开此关系时,会发生此问题。 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. 我意识到这里有一个循环引用,并且已经阅读了一些关于LINQ to SQL的解决方案,允许单向序列化,但是当不使用dbml文件时找不到解决方案。


UPDATE 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? 另一个问题是,将一组DTO抽象化以通过Web服务公开而不是原始实体是一种常见的做法吗? This would solve the serialization issue as the DTO classes would not necessarily need cyclic references(as they are not NHibernate entities). 这将解决序列化问题,因为DTO类不一定需要循环引用(因为它们不是NHibernate实体)。


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). 我目前正在对此进行测试,并且会成功发布(出于文章的缘故)。

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. 我建议使用DTO实现来抽象您的域。 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). 还有其他好处,包括您可以提供意图显示界面(客户端不必考虑如何使用该服务)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM