Turn off HasMany lazy loading in Fluent?

1,652 views
Skip to first unread message

Johan Danforth

unread,
Dec 22, 2008, 11:01:15 AM12/22/08
to Fluent NHibernate
I want to use Fluent NHibernate from within a WCF service but I'm
still working on a good session lifetime management solution and the
session seems to close before the serialization of lazy loaded
collections. I'm trying to turn off lazy loading but I just cannot
get
it to work well. I've set NotLazyLoaded() on my map-class, but it's
still being lazily loaded.

Any suggestions on how to turn it off properly (in fluent)?


Many thanks and seasons greetings!


/Johan

James Gregory

unread,
Dec 22, 2008, 11:13:50 AM12/22/08
to fluent-n...@googlegroups.com
I think you've encountered a flaw in one of my recent changes. I recently changed the default lazy-load to be true rather than false (as it used to be), but it's obvious now that we haven't fully exposed a way to override this in specific instances.

Do you need it to only affect this one collection or is it a project wide setting? If it's project wide, you can (re-)override the default lazy-load convention to be false.

Depending on how you're doing your mapping, you can either pass in a Conventions class to the MappingVisitor, or into the AutoMapper.

var conventions = new Conventions();
conventions.DefaultLazyLoad = false;

I'll look at adding the ability to invert the lazy-load on specific cases when I next can.

Andrew Stewart

unread,
Dec 22, 2008, 11:18:49 AM12/22/08
to fluent-n...@googlegroups.com
Hi James

There is an convention that you can use to set the hasmany constraint.

var conventions = new Conventions();
convention.OneToManyConvention = m =>
                                                                            {
                                                                                m.SetAttribute("lazy","false");
                                                                            };

Not sure if there is a strongly typed override or not yet though.

Andy
--
=================
I-nnovate Software - Bespoke Software Development, uk wirral.
http://www.i-nnovate.net

Johan Danforth

unread,
Dec 22, 2008, 1:32:21 PM12/22/08
to Fluent NHibernate
Thanks James. As I'm a complete NH newbie I just have to figure out
how to apply Conventions... any hint?

This is how my sample map-classes looks like, is this correct if I
want to eager load any Messages that belong to a Person?

public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.Id, "PersonId");
Map(x => x.Name);

HasMany<Message>(x => x.Messages)
.AsList()
.WithKeyColumn("PersonId");
}
}


public class MessageMap : ClassMap<Message>
{
public MessageMap()
{
Id(x => x.Id, "MessageId");
Map(x => x.Text);

NotLazyLoaded();
}
}

Thanks for any help. Getting NHibernate to work in a safe way with WCF
was not as easy as I first thought it was going to be :)

/Johan

On Dec 22, 5:13 pm, "James Gregory" <jagregory....@gmail.com> wrote:
> I think you've encountered a flaw in one of my recent changes. I recently
> changed the default lazy-load to be true rather than false (as it used to
> be), but it's obvious now that we haven't fully exposed a way to override
> this in specific instances.
> Do you need it to only affect this one collection or is it a project wide
> setting? If it's project wide, you can (re-)override the default lazy-load
> convention to be false.
>
> Depending on how you're doing your mapping, you can either pass in a
> Conventions class to the MappingVisitor, or into the AutoMapper.
>
> var conventions = new Conventions();
> conventions.DefaultLazyLoad = false;
>
> I'll look at adding the ability to invert the lazy-load on specific cases
> when I next can.
>
> On Mon, Dec 22, 2008 at 4:01 PM, Johan Danforth <johan.danfo...@gmail.com>wrote:
>
>
>
>
>
> > I want to use Fluent NHibernate from within a WCF service but I'm
> > still working on a good session lifetime management solution and the
> > session seems to close before the serialization of lazy loaded
> > collections. I'm trying to turn off lazy loading but I just cannot
> > get
> > it to work well. I've set NotLazyLoaded() on my map-class, but it's
> > still being lazily loaded.
>
> > Any suggestions on how to turn it off properly (in fluent)?
>
> > Many thanks and seasons greetings!
>
> > /Johan- Hide quoted text -
>
> - Show quoted text -

Johan Danforth

unread,
Dec 22, 2008, 2:24:26 PM12/22/08
to Fluent NHibernate
Sorry, forgot to add that I'm mapping in my session manager setup:

var configuration = MsSqlConfiguration.MsSql2000.ShowSql
().ConnectionString.Is(
@"Data Source=.\SQLEXPRESS;Initial
Catalog=TestDB;Integrated Security=True").ConfigureProperties(new
Configuration());
configuration.AddMappingsFromAssembly(GetType().Assembly);

/Johan
> > - Show quoted text -- Hide quoted text -

James Gregory

unread,
Dec 23, 2008, 3:59:50 AM12/23/08
to fluent-n...@googlegroups.com
Sorry it took me a little while to get back to you. Your config looks fine, you just need to use the PersistenceModel to utilise conventions.

Something along the lines of:

var configuration = MsSqlConfiguration.MsSql2000
  .ShowSql()
  .ConnectionString.Is("your connection string")
  .ConfigureProperties(new Configuration());

var model = new PersistenceModel();

model.addMappingsFromAssembly(GetType().Assembly);
model.Conventions.DefaultLazyLoad = true;
model.Configure(configuration);

I think that should do the trick. Basically you let the PersistenceModel handle adding the mappings, and then you can alter the system-wide conventions using the model.Conventions property (namely in this case, the DefaultLazyLoad).

Let me know how it goes.
Reply all
Reply to author
Forward
0 new messages