Parent/child help needed

Parent/child help needed

Old forum URL: forums.lhotka.net/forums/t/7712.aspx


Vlam100 posted on Thursday, October 01, 2009

I’ve got a problem where I cannot access the methods and properties of my parent from one of my child objects. I am new to the framework and was wondering if anybody can help me with this. Thanks

My design is as follows

ParticipantImport (editable Root)
ParticipantImportItem(editable child)
ParticipantImportItems(editable child collection of participantImportItem)
GlobalDataItem(editable Child)
GlobalDataItems(editable child collection of ParticipantImportItem)

In my parent Object I have 2 business Methods the first one is Items which is of type of the collection

Items (of participantImportItems)

The other one is ItemsGlobalData of the globaldataItems Collection

ItemsGlobalData (of globaldataItems)

From my UI I will invoke the participantImport.Items.assign (parameters) method which will add the data from the UI to the BO, where participantImportItem will represent one record of the collection in this object.

I also check my validation Rules within participantImportItme and one of them is to check if a specific department exists in the department table. If it does not exist I want to add the name of department to the GlobaldataItems object. So that I can report it back to the user but.

From within my validation rule in participantImportItem I call

CType(target.Parent, ParticipantImport).ItemsGlobalData.Assign(CompanyId, DivisionName, "Division")

The moment it executes this line it gets a null reference exception error and I have also noticed that target.parent is empty, is there another way as to how I should add it to the list or is the parent not available from within this child object ?

Both my child objects ( the collections) are instantiated in the create method of the parent

msk replied on Monday, October 05, 2009

One way to tackle this may be to have your editable root object handle events raised when the child collections are edited. It could then be responsible for checking for the existence of the ParticipantImportItem in the GlobalDataItems collections and adding it if necessary.

I would only have a rule if I were notifying the user and expecting them to add the GlobablDataItem to fix the problem.

rsbaker0 replied on Monday, October 05, 2009

Vlam100:
...

My design is as follows

ParticipantImport (editable Root)
ParticipantImportItem(editable child)
ParticipantImportItems(editable child collection of participantImportItem)
...


I'm a little confused by this, as it seems like ParticipantImportItem contains a list of itself, unless you meant this:

ParticipantImport (editable Root)
ParticipantImportItems(editable child collection of participantImportItem)
ParticipantImportItem(editable child)

A quirk of CSLA is that the "interesting" ancestor object of a child in a collection isn't usually the collection, which is the direct parent, but instead is the grandparent, which is parent of the collection.

If you are using managed properties, CSLA will assign the parent at the time the object is added to the collection, or, in the case of a direct relationship between a parent and child BO as indicated in your original hierarchy, at the time the child property is set to a child BO.

When working with child in a collection, a way to get a child that already has the parent attached is to call AddNew on the collection, which will return the partially populated child.

Copyright (c) Marimer LLC