views:

283

answers:

2

I'm currently learning NHibernate and I would like to data-bind to Web controls (i.e. GridView).

In my current example I am using Fluent NHibernate to map two tables to their business objects (Project and ProjectStatus). I also have a "Project has a ProjectStatus" (many-to-one) relationship.

Structure of Project class:

Project.ID
Project.Name
Project.ProjectStatus.Name
Project.ProjectStatus.Description

When I bind a list of Project objects (with lazy="proxy") to a GridView, the GridView does not trigger the load of the referenced table. As a result the fields Name and Description of ProjectStatus are left out by the GridView control.

Is there a way of having true lazy loading in combination with a data-bound GridView?

Thanks in advance.

+2  A: 

Are you storing your ISession in the request context and flushing it at the end of the request? If not, you should start a new session in the begin_ request method of the global.asax.cs, store it in request context, then grab it back in the end_request method, and flush it.

Josh Pearce
Thanks, I ran into this right after I had the data binding fixed. It still seems like a pain to store the session so that the repository can use it. Do you have any tips how to do it more easily? Thanks
motto
+2  A: 

This should work without any problems. I suspect the problem is in the data binding itself not accessing the properties of a child object. How are you binding to the ProjectStatus properties? As far as I know, you can't directly bind to child objects. You have to set values in an OnRowDataBound event handler or by using Eval.

I would first create an OnRowDataBound handler and break on a DataRow. Examine e.Row.DataItem in the immediate window to verify that the child object is being loaded. If it is, then the problem lies with the binding.

Jamie Ide
If you don't want to haldle the OnRowDataBound event, you could make wrapper properties for the child object properties like: public string StatusName { get { return ProjectStatus.Name; } }
Josh Pearce
Thanks Jamie Ide, yes you were right, accessing the children manually and adding them to the row in the RowDataBound handler fixed my problem. Do you know if ADO.NET Entity objects/proxies behave the same way? Or is the data-binding easier?
motto
@Motto - It's a limitation of data binding, unfortunately, so it doesn't matter where the object comes from. I think you can work around it without RowDataBound by putting an import statement into your page and using eval.
Jamie Ide
Yes, I also found another way which is without handling the RowDataBound event. I access the child properties through an asp:TemplateField with Text='<%# Container.DataItem.ProjectStatus.Name %>' (note: the '' are a must and can't be "")
motto