Child_Update with ObjectFactory pattern?

Child_Update with ObjectFactory pattern?

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


Dane posted on Monday, January 11, 2010

I have a parent class MyParent which inherits from BB.  MyParent has a property TheChildren of type MyChildList which is a list of objects of type MyChild.  MyChildList inherits from BLB and MyChild inherits from BB.  The default constructors of both the MyChildList and MyChild call the MarkAsChild method.  I'm using an ObjectFactory pattern so each of these classes specifies an ObjectFactory which resides in a separate assembly.  I've implemented a Child_Update method on MyChildList and an Update method on the ObjectFactory for MyChildList, however, when I perform a Save on a new MyParent object with a new MyChildList containing new MyChild objects neither the MyChildList's Child_Update method nor the Update method on the ObjectFactory for MyChildList seem to be firing.  I'm obviously missing something key here.  Can someone point me in the right direction?

Thanks for any help you can provide,

Dane R. Vinson

RockfordLhotka replied on Monday, January 11, 2010

Have you watched the data access video and/or read chapter 18?

When using the ObjectFactory model your object factory assumes complete responsibility for the object graph. The data portal steps entirely out of the picture after calling your factory method.

This means there's no child data portal concept, or automated metastate management like there is with the DataPortal_XYZ model. Instead, your factory is responsible for all persistence behaviors across your entire object graph.

Normally, btw, you no longer call MarkAsChild() in your business class code. If you are using the DataPortal_XYZ model the IsChild property is managed automtically along with the other metastate (IsNew, etc). If you are using the ObjectFatory model nomally the factory takes care of all the metastate, and the ObjectFactory base class has protected methods to allow you to write that code.

Dane replied on Monday, January 11, 2010

Thanks for the response.  I thought I was going crazy.  I do have the book and I have read it but it was 6+ months ago and right now it's on loan to someone (who doesn't seem in a hurry to get it back) so I don't have it for reference.

The reason I used the MarkAsChild method in MyChild and MyChildList is because I didn't implement the static New methods to utilize ObjectFactory classes.  In these two cases there's no reason for me to incure the overhead accessing the server (through their respective ObjectFactorys) to instantiate a new instance of those objects so their static New methods call the private constructors which call MarkAsChild.  I removed these and found that the MyParent object seems to still understand that the MyChildList and it's MyChild objects are children and it propgates IsValid from the children up to the parent, i.e. if any MyChild.IsValid = false in the list the MyParent object also will show IsValid = false.  That being the case, and since all updates to child object need to be done server side in an ObjectFactory is there a reason to mark the child object as children?

RockfordLhotka replied on Monday, January 11, 2010

CSLA is designed to follow certain conventions, one of which is that an editable object contained by another editable object is a child object. If you violate that convention parts of CSLA may or may not work as expected.

Some elements, like IsValid, are independent of whether an object is a child, so they happen to work. Other elements, like the data portal, may throw exceptions or just not work properly.

It is important that a child object be marked as a child - but that happens automatically when using the child data portal and Child_XYZ methods.

Please note that you can mix the object factory and child data portal models if you know what you are doing. If you call DataPortal.CreateChild() it will work, even if some root object in the graph has an ObjectFactory attribute. This is because the child data portal has no knowledge of the object factory concept, so it just creates an instance of the desired type, marks it as a child and calls Child_Create() on that object.

Copyright (c) Marimer LLC