NHibernate: Separate Lazyload Property

I have currently translated my blogging from Linq2Sql to NHIbernate.

I ran into the following performance issue: I have one table: Messages, which has identifiers, headers, PostContent, DateCreated columns, etc.

The problem is that when I create the "Recent Posts List", I don’t need all PostContent.

In Linq2Sql, you can set lazy loading on a single property, so it will not be part of the SQL query until you ask about this property.

I tried to do this using Fluent NHibernate by doing the following:

Map(x => x.PostContent).LazyLoad();

This did not work. It seems that NHibernate does not support this, so my question is: how can I fix this?

Is it really lazy to upload my property without moving the contents to a separate table?

Thanks in advance!

+3
source share
3 answers

Update: This feature is now available on the NHibernate trunk.

Read more about the Ayende blog , where the sample is exactly the scenario that you describe here.

+3
source

Here's how you can achieve what you want (like lazy loading, but not the same)

var posts = NHibernateSessionManager.Session.CreateQuery("Select p.Id as Id, p.Title as Title, p.DateCreated as DateCreated from Post p")
                .SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(Post)))
                .List<Post>();

What AliasToBean is for, it makes a selection for specific columns (usually from more than one object) and returns a strongly typed collection instead of System.Object [].

, Bean () - Post, .

, , NHibernate. .

, , " ", "" , , .

: NHibernate: ,

+3

This is not possible if PostContent is mapped to the same table because the performance difference is negligible in 99% of situations. When you are at 1%, you can use sql for handbuild instead of orm for this case.

Lazy / Eager loading is not possible at all from linq to sql (out of the box), as far as I know.

What you can do is create another class with the data you want to select and just select that data in a new object.

0
source

Source: https://habr.com/ru/post/1724868/


All Articles