Using lazy properties

Consider a class representing a scientific article, with properties for author, title, abstract, full text, and so on.

This looks simple enough, but having the full article text stored in the entity could cause serious issues. The text may be several megabytes in size and if we're, for example, just listing the available articles, it's very unnecessary to load all that from the database and storing it in memory.

Thankfully, NHibernate has a solution for this, called lazy properties. Just as with lazy loading of referenced entities and collections, this function will not load the data until it is accessed.

Getting ready

Complete the Getting ready instructions at the beginning of this chapter.

How to do it…

  1. Add a new folder named ...

Get NHibernate 4.x Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.