Question re: children of root objects (no collection)

Question re: children of root objects (no collection)

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


willwise posted on Wednesday, November 29, 2006

Hi folks,

I've been digging into CSLA as my current client is using it.  Much to like!

I'm designing a new portal and have run into one small problem. I have a workaround but I'd rather do things the "right" way so here goes...

I have an editable root object that contains a child object directly; that is, it does not have a collection of child objects but just a single child object.  It's accessed like so:

EditableRoot.EditableChild

The only issue I'm having is figuring out how to mark the child object for deletion.  I'm using version 1.1 of the framework.  Calling Delete directly as the parent root object is not a collection.

Any help much appreciated!

Kind Regards,
Will

ajj3085 replied on Thursday, November 30, 2006

Will,

I agree, there is much to like!

A child as you have setup normally cannot be deleted by itself, it would only be deleted if the parent object is itself marked as deleted.

This usually makes sense.  What happens when you delete the child?  Does the EditableRoot.EditableChild property now return null?  An 'empty' object (kind of like String.Empty, or DbNull.Value)?

That said, you CAN delete the data behind the child based on some condition.  Alternately, I guess you could call the IEditableBusinessObject.DeleteChild() method... but I would expose a method on the Root object which will call that method on the child object.  I would think that makes the most sense for your scenario.

HTH
Andy

willwise replied on Thursday, November 30, 2006

My solution to this problem was to modify the first line of the Update method in the child object from:

If Not Me.IsDirty Then Exit Sub

to:

If Not (Me.IsDirty Or Parent.IsDeleted) Then Exit Sub

and also modify the test for Me.IsDeleted to include the parent in the test like so:

If Me.IsDeleted Or Parent.IsDeleted Then
    ' Delete the child object...
End If

This seems to do the trick without too much fuss. Would this be considered to follow CSLA best practices for this case if there are any that are applicable?

Thanks again!
Will

Copyright (c) Marimer LLC