简体   繁体   中英

Issues in WCF Service with NHibernate

I'm trying to create a service layer with Fluent NHibernate and WCF. How to work with LazyLoad? I'll get some object, for example. This object contains a collection mapped with 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.

If you want to stick with using NHibernate and accessing your data over WCF, you need to switch to eager loading.

If you are willing to drop WCF, you could allow your clients to access the database directly.

If you are willing to drop Nhiberhate you could use WCF Data Services.

Lazy loading will occur, but it will ALL occur when your return value is being serialized to a WCF response. 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. You query for a person, without loading his address and want to return this object to the client.

Enter WCF. WCF will serialize all the [DataMember] s, and Address is one of them. So the getter will be called, and that will execute the lazy loading if you're still within your using (ISession) scope. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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