Collecting all child deletedlist items for a given BusinessBaseCollecting all child deletedlist items for a given BusinessBase
Old forum URL: forums.lhotka.net/forums/t/6295.aspx
Devman posted on Friday, January 30, 2009
Hi
Has anyone tried this before? I am trying to scan through a businessbase for children that contain deletedlist collections and then adding them to a stack. This needs to be recurrsive to search the whole object graph.
Regards
Devman
JoeFallon1 replied on Friday, January 30, 2009
Here is an old thread that might give you some ideas:
http://forums.lhotka.net/forums/post/7444.aspx
This recursively loops through root BO to find all other BOs contained in the graph. The goal was to be able to produce a single list of Broken Rules for the entire graph.
I still use a similar concept today - but I re-wrote some of the code.
Joe
Devman replied on Thursday, February 05, 2009
Cheers for the reply!
I ended up with the following ;
Created an interface which is implelemented in both PBussinesBase and PBussinessListBase. These derive from the csla business bases.
The interface had one method; void GetDeletedItems(Stack itemsToDeleted).
I created an Attribute to mark bi directional properties so to ignore them otherwise i was getting stuck in a loop.
for PBusinessBase ive got the following implementation;
public void GetDeletedItems(Stack itemsToDeleted)
{
/// TODO: Look at using FieldManager/FieldData instead of reflection.
//cascade the call to all the relevant children
Type currentType = this.GetType();
do
{
// get the list of properties in this type
foreach (var p in currentType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
// check to see if the property is not bi-directional ([IgnoreForDeletedList]) and is of correct type (IDeletedListManager)
if (typeof(IDeletedListManager).IsAssignableFrom(p.PropertyType) & Attribute.IsDefined(p, typeof(IgnoreForDeletedList)) != true)
{
var value = (IDeletedListManager)p.GetValue(this, null);
// make sure the variable has a value
if (value != null)
{
// it is a child object so cascade the call
value.GetDeletedItems(itemsToDeleted);
}
}
}
// move up the inheritance tree and try again
currentType = currentType.BaseType;
} while (currentType != typeof(PBusinessBase<T>));
}
For PBussinessListBase i have this;
public void GetDeletedItems(Stack itemsToDeleted)
{
foreach (IDeletedListManager child in this)
{
child.GetDeletedItems(itemsToDeleted);
}
foreach (var obj in DeletedList)
{
itemsToDeleted.Push(obj);
}
}
Im not sure how "Good" this code is, i was rooting around the csla source for hints and found AcceptChanges() in UndoableBase to be of help, hence the code looking similar to that.
Regards
Devman
Copyright (c) Marimer LLC