[英]Stop lazy loading or skip loading a property in NHibernate? Proxy cannot be serialized through WCF
Consider I have a parent, child relationship class and mapping. 考虑我有父,子关系类和映射。 I am using NHibernate to read the object from the database, and intended to use WCF to send the object across the wire.
我正在使用NHibernate从数据库中读取对象,并打算使用WCF通过线路发送对象。
[DataContract]
, it cannot be serialized as the property is lazy load proxy instead of real known type. [DataContract]
,它不能被序列化,因为属性是延迟加载代理而不是真正的已知类型。 lazy=false
or lazy=true
doesn't work. lazy=false
或lazy=true
不起作用。 Former will eagerly fetch all the relationships, latter will create a proxy. null
would be the best. null
是最好的。 null
for those references that I don't want to fetch. null
。 A null, but not just a proxy. GetObjectToSerialize
, I never observe an proxy being passed through GetObjectToSerialize
, leaving no chance to un-proxy it. GetObjectToSerialize
,我从未观察到代理通过GetObjectToSerialize
传递,没有机会取消代理它。 After reading the comments, more surfing on the Internet... 阅读评论后,更多在网上冲浪......
It seems to me that DTO would shift major part of the computation to the server side. 在我看来,DTO会将计算的主要部分转移到服务器端。 But for the project I am working on, 50% of time the client is "smarter" than the server and the server is more like a data store with validation and verification.
但对于我正在开发的项目,50%的时间客户端比服务器更“智能”,服务器更像是一个带有验证和验证的数据存储。 Though I agree the server is not exactly dumb - I have to decide when to fetch the extra references already, and DTO will make this very explicit.
虽然我同意服务器不是很愚蠢 - 我必须决定何时获取额外的引用,而DTO将使这一点非常明确。
Maybe I should just take the pain. 也许我应该忍受痛苦。 I didn't know http://automapper.codeplex.com/ before, this motivates me a little more to take the pain.
之前我不知道http://automapper.codeplex.com/ ,这激励我多一点痛苦。
On the other hand, I found http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/ , which seems to be working with IDataContractSurrogate.GetObjectToSerialize
. 另一方面,我发现http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/ ,这似乎与
IDataContractSurrogate.GetObjectToSerialize
。
Was getting the same issue with WCF-Silverlight transferring of DTO/Entities. WCF-Silverlight转移DTO / Entities时遇到了同样的问题。 I use following code which works fine:
我使用以下代码工作正常:
Fluent NHibernate: 流利的NHibernate:
References(c => c.DataSource)
.Column("DataSourceId")
.Cascade.None()
.Not.LazyLoad();
HasManyToMany(c => c.Categories)
.Table("Categories")
.ParentKeyColumn("ItemId")
.ChildKeyColumn("CategoryId")
.Not.LazyLoad()
.Cascade.None()
.Inverse();
XML: XML:
<many-to-one name="DataSource" column="DataSourceId" lazy="false" cascade="none" />
Hope this'll work for you! 希望这对你有用!
I have run into similar issues with performance, what I did instead was preload the information I want by using an explicit call to the DB using a DetachedCriteria . 我遇到了与性能类似的问题,我所做的是通过使用DetachedCriteria对DB进行显式调用来预加载我想要的信息。
So similar to what you are doing I would probably do something like this. 与你正在做的事情类似,我可能会做这样的事情。
public DetachedCriteria BuildMyCriteria()
{
var criteria = DetachedCriteria.For<ParentClass>();
criteria.CreateCriteria("this.ChildClass", "Child Class").SetFetchMode("this.ChildClass", FetchMode.Eager);
criteria.Add(Restrictions.IsNotNull("ChildClass.Property");
return criteria;
}
Then from my facade, I would get those entities with the properties that do not have null attributes on the child property 然后从我的外观,我会得到那些具有属性的实体,这些属性在子属性上没有null属性
var myClasses = _repository.ExecuteDetachedCriteria<ParentClass>(BuildMyCriteria);
Then I would have all ParentClass entities that have a null property. 然后我将拥有所有具有null属性的ParentClass实体。
So instead of taking a hit by constantly asking the DB for info, I store it in memory so it is ready for me to use. 因此,不是通过不断询问数据库获取信息,而是将其存储在内存中,以便我可以使用它。
This is just an alternative solution to DTOs, good luck :) 这只是DTO的替代解决方案,祝你好运:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.