Add a child object without using AddNewCore

Add a child object without using AddNewCore

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


SethSpearman posted on Monday, December 17, 2007

In Rocky's book he says this on page 367 (VB Version):

"Child objects are usually created when the UI code calls an Add() method on the collection object that contains the child object.  Ideally, the child class and the collection class will be in the same assembly, so the Shared factory methods on a child object can be scoped as Friend, rather than Public.  Ths way, the UI can't directly create the object, but the collection object can create the child when the UI calls the collection's Add() method"

Rocky also wrote in the note in the same page:  "Child object can optionally be created through data binding, in which case the addition is handled by overriding the AddNewCore() method in the collection class"

The background is that I have succeeded in writing my Collection classes by overriding the AddNewCore but have NOT successfully determined (and therefore written) how to add children to a collection if you DON'T override AddNewCore.  Rocky has an Assign method that seems similar to the Add method described above but I cannot find any example that is coded using an Add method.

Help will be greatly appreciated here.  Here is what my code looks like.  This code is from the Unit Test for this object. 

This code works UNTIL I add a Required field validation rule to the child object:

Dim objChild As childBizObject
objChild=obj.AddNew  'obj is the parent object

The book basically SAYS that this code will fail if you have a required field.

What would the code look like if you were NOT using AddNew?

I have tried this:

Dim objChild as childBizObject
obj.Add(objChild)

This causes a compiler warning and a runtime error (that matches the warning) - A Null reference error. 

So I thought I could change the first line to this:

Dim objChild as childBizObject = childBizObject.NewChildBizObject

But this doesn't work becasue the factory method on the childObject is scoped as friend and of course my unit tests are in a different project as will be my UI.

Any suggestions

Seth B Spearman

ajj3085 replied on Tuesday, December 18, 2007

Well, if you ever want your list to work with data binding, you MUST override AddNewCore.  You won't be left with a choice.

If that's not the case though, you'll have to make your child's factory method public; otherwise how would the UI create an instance to add either?

On a related now, if you just want your test assembly to see the internals (friend) of your BO assembly, use the InternalsVisibleTo attribute on the BO assembly.  See documentation for more information.

JoeFallon1 replied on Tuesday, December 18, 2007

I use code like this to add children to a collection:

Public Function AddChild(ByVal key As Long, Optional ByVal testForExistence As Boolean = False) As MyBO
  Dim obj As MyBO= MyBO.NewMyBO(key)
  Return DoAdd(obj, testForExistence)
End Function

Public Overloads Sub Add(ByVal key As Long, Optional ByVal testForExistence As Boolean = False)
  Dim obj As MyBO= MyBO.NewMyBO(key)
  DoAdd(obj, testForExistence)
End Sub

In my Base class, which is derived from CSLA and which all my collections derive from, I have:

Protected Overridable Function DoAdd(ByVal obj As C, Optional ByVal testForExistence As Boolean = False) As C

If testForExistence = True Then
 
If Not Contains(obj) Then
   
MyBase.Add(obj)
   
Return obj
 
End If
 
Return Nothing
Else
 
MyBase.Add(obj)
 
Return obj
End If

End Function

So, the collection creates the child object using the AddChild method and then returns it to the caller so other properites can be set if needed. Or it also creates the child using the Add method and then does not bother returning the object to the caller. Your choice.

Joe

Copyright (c) Marimer LLC