EditableRootList containing SwitchableObjects

EditableRootList containing SwitchableObjects

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


jrnail23 posted on Thursday, August 31, 2006

I'm trying to build an EditableRootList which contains SwitchableObjects, and I noticed that in the Templates, the SwitchableObject doesn't contain the code necessary to comply with the following DataPortal_Update method of the template's EditableRootList code:

Protected Overrides Sub DataPortal_Update()
   RaiseListChangedEvents = False
   For Each item As EditableChild In DeletedList
      item.DeleteSelf()
   Next
   DeletedList.Clear()

   For Each item As EditableChild In Me
      If item.IsNew Then
         item.Insert(Me)
      Else
         item.Update(Me)
      End If
   Next
   RaiseListChangedEvents = True
End Sub

Now I know that Rocky discourages the use of SwitchableObjects, but can anyone provide some insight on what I might need to do to my SwitchableObject to get it to comply with this code?

RockfordLhotka replied on Thursday, August 31, 2006

I'm not sure I understand the issue?

All switchable objects implement both the DataPortal_XYZ methods that are called by the data portal if the object is used as a root, and a set of equivalent Friend methods that are called by the parent if the object is used as a child. Typically the DataPortal_XYZ methods delegate their work to the Friend methods to avoid duplication of code, or both of these delegate to a third private method that does the real work.

So in the switchable object you'll have:

Protected Overrides Sub DataPortal_Insert()
  DoInsert()
End Sub

Friend Sub Insert(ByVal parent As ParentType)
  mParentId = parent.Id
  DoInsert()
End Sub

Private Sub DoInsert()
  ' implement actual data update code here
End Sub

This implementation, or variations on the theme, is the standard approach for implementing any switchable object.

jrnail23 replied on Thursday, August 31, 2006

Rocky, thanks for the quick reply... Makes sense to me, but the code you posted isn't in the switchable object template.  Is there an updated version of the templates project available for download?

Copyright (c) Marimer LLC