Adapter: Fields mapped onto relations and null return values

Posts   
 
    
IanG
User
Posts: 1
Joined: 17-Oct-2009
# Posted on: 17-Oct-2009 02:49:09   

Hi,

I am using latest build of LLBLGen and adapter and have just searched quite hard to find an answer to my question before posting. I wondered if there was a reason a new entity is not created for a 1:1 relation when it is null inside the containing class (field mapped onto relation). Let me further explain what I mean; here is some of my generated code

    public virtual AddressEntity Address
    {
        get
        {
            return _address;
        }

If this is a new entity and the .Address property has not been set to anything then this will obviously return null. This is not a feature request, I can change my local templates but I wondered if there is a problem doing the following;

    public virtual AddressEntity Address
    {
        get
        {
                    if (_address == null && IsNew)
                    {
                            _address = new AddressEntity();                     
                    }

            return _address;
        }

This will allow the following syntax without assigning a new entity to the Address property first:

booking.Address.Address1 = "address1";

I know I definitely wouldn't want to create a new _address entity unless the containing entity is new. This could lead to overwriting of a foreign key if a prefetch path was not supplied. Is there a similar issue with the code I have proposed?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Oct-2009 04:43:30   

Your approach look good. It however assumes you have the main entity fetched. That is ok in general as you are checking whether the entity IsNew. So I think the approach should work.

David Elizondo | LLBLGen Support Team