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 childBizObjectThe 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
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 MyBOPublic 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 ThenSo, 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