views:

39

answers:

2

I am using NHibernate 2.0.1 and .NET I am facing issues with Lazy loading an association

I have a BusinessObject class that has associations to other BusinessObject in it, and it can go deeper.

The following function is in the BusinessObject to read the values of a collection in the BusinessObject.

    public virtual object GetFieldValue(string fieldName)
    {
        var fieldItems = fieldName.Split(AppConstants.DotChar);
        var objectToRead = this;
        for (var i = 0; i < fieldItems.Length - 1; i++)
        {
            objectToRead = (BusinessObject) objectToRead.GetFieldValue(fieldItems[i]);
        }
        //if (objectToRead._data == null) return objectToRead.SystemId + " Error: _data was null";
        return objectToRead.FieldValue(fieldName.LastItem());
    }  

The FieldValue function is described below

    private object FieldValue(string fieldName)
    {
        return _data.Contains(fieldName) ? _data[fieldName] : null;
    }

The BusinessObject has a dictionary_data which stores the field values.

Assume the fieldName is BusinessDriver.Description and the BusinessObject which has this field is StrategyBusinessDriver

This code breaks down the field name into two - BusinessDriver & Description. The first iteration reads the BusinessDriver object from StrategyBusinessDriver. It is cast into a BusinessObject type so that I can call the GetFieldValue again on it to read the next field i.e Description in the BusinessDriver.

The problem is that when I read the BusinessDriver in the first iteration and cast it, I get the Ids and all other details of the BusinessObject but the field dictionary _data and other collections are not fetched. This should be fetched lazily when I read the _data of the BusinessObject. However, this does not happen and I get an error that _data is null.

Is there something wrongly coded because of which the collection is not fetched lazily? Please ask for more clarifications if needed. Thanks in advance.

UPDATE: I have some more insight into the problem. The code to load the fields of the BusinessObject is not lazily loaded if the function is called recursively inside the BusinessObject. If I move this logic out of the BusinessObject, lazy loading works! Is there a way for this to work inside the BusinessObject?

UPDATE: Mapping file

    <joined-subclass name="Japt.Core.Domain.Data.BusinessObject, Japt.Core, Version=0.0.1608.0, Culture=neutral, PublicKeyToken=null" table="BusinessObject_BusinessDriverStrategyMap" entity-name="BusinessDriverStrategyMap">
  <key column="SystemId" />
  <dynamic-component name="_data" insert="true" update="true" optimistic-lock="true">
    <many-to-one class="Japt.Core.Domain.Data.BusinessObject, Japt.Core, Version=0.0.1608.0, Culture=neutral, PublicKeyToken=null" name="Strategy" entity-name="Strategy">
      <column name="Strategy" />
    </many-to-one>
    <many-to-one class="Japt.Core.Domain.Data.BusinessObject, Japt.Core, Version=0.0.1608.0, Culture=neutral, PublicKeyToken=null" name="BusinessDriver" entity-name="BusinessDriver">
      <column name="BusinessDriver" />
    </many-to-one>
  </dynamic-component>
  <dynamic-component name="_collections" insert="true" update="true" optimistic-lock="true" />
</joined-subclass>
A: 

is StrategyBusinessDriver a BusinessObject? can you post your mapping? is _data a public property or a private field? because the proxy needs to override its getter...

statichippo
Yes, StrategyBusinessDriver is also a BusinessObject.I am attaching the mapping file above._data is a private field. However, it is a dictionary and is used to map dynamic components. (I have dynamic columns)
Zuber
I dont know how to attach a file and the entire mapping file is not being accepted in the code markup
Zuber
A: 

I think I got the bottom of this issue. Since I am trying to read properties lazily of an object within the same object, it is not able to load those properties lazily. When I moved that code out using an extension method, the reading of the property was not in the scope of the same object, which is why it worked.

Zuber