The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Should I have to manually update the parent reference in each child?

Last post 07-17-2008, 8:33 AM by pdwhite. 7 replies.
Sort Posts: Previous Next
  •  07-15-2008, 6:43 PM 10254

    Should I have to manually update the parent reference in each child?

    Hi there

    I am having a problem with the hierarchical model.  I have tried this in both 2007 and 2008 versions.

    I had assumed that if I create the child collection using the Parent-Child property on the parent that all the children would automatically have the object reference back to the parent set.  This appears not to be the case .(Edit: some confusion when I first posted this in that I might be refering to foreign keys)

    So do I need to iterate through all the children setting the UpToparentById property manually to the parent record which I just created the child collection from?  Please tell me this is not so.  If it is there is bug in the set portion of the Many to One property where it tests if the value of the property is changing.  If the private member variable for the parent is null it will error which is likely with the lazy initialization.

    Code:
    Dim changed as Boolean = Not Me._UpToInterviewersByInterviewerId.Equals(value)
     

    The up shot of this which I really do not like is that if I try and reference the parent on each child using the UpToparentById property, each child will load its own copy of the parent from the database.   One of the reasons I want them all pointing at the same instance of the parent is that I have total properties on the parent which are updated from the children like in a classic order/item scenario.

    I hope I have missed something here.

    Cheers

    Paul

  •  07-16-2008, 12:51 AM 10255 in reply to 10254

    Re: Should I have to manually update the parent reference in each child?

    Hi Paul

    I've just mocked up a quick test sub and all worked as expected, the new "Parent" (Supplier) got added fine, as did the children (Products) - see if the following helps:

    Code:
    Private Sub TestAddChildObjects
    Dim Supplier As New Suppliers
    Supplier.CompanyName = "New Supplier"
    
    Dim i As Integer
    For i = 0 To 5
    	Dim Product As New Products
    	Product = Supplier.ProductsCollectionBySupplierID.AddNew
    	Product.ProductName = "Product number " + i.ToString()
    	Product.CategoryID = 1
    	Product.UnitPrice = i		
    Next
    Supplier.Save()
    End Sub
    

    Cheers

    Martin

  •  07-16-2008, 3:45 AM 10258 in reply to 10255

    Re: Should I have to manually update the parent reference in each child?

    Hi Martin

    Thanks for the quick response.  I should have made myself a little clearer but it was 2am when I wrote it.  I am quite happy with the hierarchical model saving and maintaining referential integrity, that's fine and not a problem.

    The problem is with the collections and entities when in memory.  The 'UpToParent' property on the child objects do not have a reference to the parent object that created them by default.  In your sample if you call Product.UpToSupplierBySupplierID.some_Memeber before or after the save it will do a lazy initialisation and create a new instance of Suppliers and load it from the database by the primary key which it gets from Product.SupplierID. 

    As the product was added to a collection instantiated by an instance of  Suppliers I would expect each child to reference back to parent object that created it, not load up another instance.  If you created a collection of Products and loaded it by a query and not hierarchically then fine it should load the supplier as needed. 

    If you iterate through your 5 Product objects and set the Product.UpToSupplierBySupplierID to your instance of Suppliers without referencing the property first it will error as the private member variable for the parent Supplier is null and it tries to compare it to the value you assigning it to.  If you fix the problem by forcing the property to initialise first then when you save any changes you get a StackOverflow exception.

    If I you added a property to the Supplier and tried to update it from the Products records using the 'UpTo' property it would not work as you would be creating n instances of Supplier, not much good if that property was some sort of total.

    Hopefully that makes my problem a little clearer. 

     Cheers

     Paul

     

  •  07-16-2008, 4:02 AM 10260 in reply to 10258

    Re: Should I have to manually update the parent reference in each child?

    Hi Paul

    I've only skimmed your reply as I'm a bit hectic at the moment but I think the issue here is coming from the fact that you're using "xxxUpToxxx" - to the best of my knowledge EntitySpaces only saves (and therefore operates as expected) Down the tree, not up - that was the reason (I think) behind the properties being named "UpTo" so that you knew you were going "in the wrong direction" so to speak.  I'm sure one of the EntitySpaces team will give you a better/more complete answer but if you search the forum for "UpTo" you might find your answer.

    Cheers, sorry it doesn't give you a more complete answer but it may point you in the right direction

    Martin

  •  07-16-2008, 6:04 AM 10262 in reply to 10260

    Re: Should I have to manually update the parent reference in each child?

    Martin thanks for taking the time to answer.  I know what it is like to busy :)

  •  07-16-2008, 10:53 AM 10264 in reply to 10262

    Re: Should I have to manually update the parent reference in each child?

    Our hierarchical model lazy-loads the properties from the database, and automates downstream saves. It makes no attempt to maintain an in-memory hierarchical representation of the database. If you add a parent with an auto-incrementing PK and 5 children, when you Save() the parent, EntitySpaces saves the parent first, retrieves the autokey, fills in each child's FK column with the parent's id, then saves the children. It does not set the in-memory children to all point to the same in-memory parent. If you call UpToXxxx on one of the children, it lazy-loads the parent from the database.

    David Neal Parsons
    www.entityspaces.net
  •  07-16-2008, 12:03 PM 10265 in reply to 10264

    Re: Should I have to manually update the parent reference in each child?

    Hi David

    Thanks for the confirmation and I am really happy with the way the hierarchical model works and appreciate how much easier it makes writing my applications.  However I was a surprised that the creation and population of the child collection from the parent does not use the object reference that exists on the child.  In all the strongly typed hierarchical collections and datasets I have worked on prior to using Entity Spaces the reference back to the parent object has been very useful.  I can implement my own parent object property and maintain it but I was always dislike duplicating effort. 

    What really did me in though was that the 'UpTo' property has a set accessor which not only errors when the private member variable for the parent object is null but also causes a stack overflow in the ApplyPreSaveKeys method if you manage to set it programmatically.  It would be nice to be able to use it but for now I'll do a workaround.  Is there an event raised when an entity is added to a collection?  I could solve my problem elegantly then.

     Cheers

     Paul

  •  07-17-2008, 8:33 AM 10278 in reply to 10265

    Re: Should I have to manually update the parent reference in each child?

    Ok for anyone who wants to maintain a reference on the child object back to the parent object here's a solution. 

     Each entity has a property called Collection which references its parent collection if any.  This is populated for entities loaded into the collection or added.  Simply add a 'Parent Object' property to the collection and set it after calling child collection property.  You'll need to cast the collection and cater for null values but it will allow you to reference the Parent object from any child object.

    Code:
    This sample has a PayClaimPending object which has a collection of child PayItems
    Parent Object property on the Collection
    
    #Region "Property 'ParentObj'"
        Private _ParentObj As PayClaimPending
        ''' <summary>
        '''    Reference to the Parent PayClaimPending Obj which owns this collection
        ''' </summary>
        Public Property ParentObj() As PayClaimPending
          Get
            Return _ParentObj
          End Get
          Set(ByVal Value As PayClaimPending)
            _ParentObj = Value
          End Set
        End Property
    
    #End Region 'Property 'ParentObj'
    
    Add something like this to your entity:
    
    #Region "Property 'ParentCollection'"
    
        ''' <summary>
        '''     Typed reference to parent collection
        ''' </summary>
        Public ReadOnly Property ParentCollection() As PayItemPendingCollection
          Get
            Return DirectCast(Me.Collection, PayItemPendingCollection)
          End Get
        End Property
    
    #End Region 'Property 'ParentCollection'
    
    
    when you instantiate the child collection from the parent set the Parent Object property
    
    payClaim.PayItemPendingCollectionByPayClaimId.ParentObj = payClaim;
    
    then from the child objects you can reference the properties of the parent
    
     If Me.ParentCollection.ParentObj IsNot Nothing Then
        Me.ParentCollection.ParentObj.TotalFee += MyBase.Fee.GetValueOrDefault(0)
     End If
    
      What would be really nice is if the childCollectionBysomeID property was overridable so you could set the ParentObj property when the child collection is created. 

    Also you could add a generic parent object property to the custom CollectionBase if you want to implement this in all your collections

View as RSS news feed in XML