简体   繁体   English

NHibernate在WCF服务中的问题

[英]Issues in WCF Service with NHibernate

I'm trying to create a service layer with Fluent NHibernate and WCF. 我正在尝试使用Fluent NHibernate和WCF创建服务层。 How to work with LazyLoad? 如何使用LazyLoad? I'll get some object, for example. 例如,我会得到一些对象。 This object contains a collection mapped with LazyLoad. 该对象包含一个与LazyLoad映射的集合。 So, my service method creates a session, retrieve the object and close the session. 因此,我的服务方法创建一个会话,检索对象并关闭该会话。 What about the lazy collection? 那懒惰的收藏呢? My service caller won't access the data, because I don't have an opened session. 我的服务呼叫者将无法访问数据,因为我没有打开的会话。 How to treat this? 怎么治疗呢?

Thanks, guys!! 多谢你们!!

You can only use lazy loading with NHibernate if you have a connection to the database. 如果您与数据库有连接,则只能对NHibernate使用延迟加载。

If you want to stick with using NHibernate and accessing your data over WCF, you need to switch to eager loading. 如果您要坚持使用NHibernate并通过WCF访问数据,则需要切换到紧急加载。

If you are willing to drop WCF, you could allow your clients to access the database directly. 如果您愿意删除WCF,则可以允许您的客户端直接访问数据库。

If you are willing to drop Nhiberhate you could use WCF Data Services. 如果您愿意放弃Nhiberhate,则可以使用WCF数据服务。

Lazy loading will occur, but it will ALL occur when your return value is being serialized to a WCF response. 会发生延迟加载,但是当您的返回值被序列化为WCF响应时,它将全部发生。 For example, we have these classes: 例如,我们有以下类:

[DataContract]
public class Person
{
    [DataMember]
    public virtual string Name { get; set; }

    [DataMember]
    public virtual Address Address { get; set; }
}

[DataContract]
public class Address
{
}

Let's say that Person.Address is lazy. 假设Person.Address是惰性的。 You query for a person, without loading his address and want to return this object to the client. 您查询一个人,但未加载其地址,而是想将此对象返回给客户端。

Enter WCF. 输入WCF。 WCF will serialize all the [DataMember] s, and Address is one of them. WCF将序列化所有[DataMember] ,并且Address是其中之一。 So the getter will be called, and that will execute the lazy loading if you're still within your using (ISession) scope. 因此,将调用getter,如果您仍在using (ISession)范围之内,它将执行延迟加载。 If you're not using the using scope and just defining a variable ISession (that you probably don't close), the lazy query will execute as well. 如果您不使用using范围,而只是定义一个变量ISession (您可能没有关闭),则惰性查询也会执行。

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

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