tags:

views:

142

answers:

1

I have a custom class that implements IResultTransformer.

It is easy to make this work for single values but what is the correct way to setup references if only the id is in the tuple? What if they are supposed to be lazy loaded? Should I just load them from the Session using the Get or Load methods?

For example:

public class FoobarResultTransformer : IResultTransformer
{
    public object TransformTuple(object[] tuple, string[] aliases)
    {
        var foobar = new Foobar();

        for (int i = 0; i < aliases.Length; i++)
        {
            switch(aliases[i])
            {
                case "IntProperty":
                    // This one is easy
                    foobar.IntProperty = Convert.ToInt32(tuple[i]);
                    break;
                case "ReferencedEntityId":
                    // Assuming the tuple contains a GUID identifier, what should I do here?
                    foobar.ReferencedEntity =
                    break;
            }
        }

        return foobar;
    }
}
A: 

You should load the referred entity by its primary key using Get or Load. If they should be lazy loaded, use Load.

Get will return null if the object does not exist. This usually results in a select against the database, but it will check the session cache and the 2nd level cache first.

Load will never return null. It will return a proxy.

Lachlan Roche