BusinessBase.Parent OnChangedEvent ?

BusinessBase.Parent OnChangedEvent ?

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


ShawnCamp posted on Saturday, January 26, 2008

As a developer I often need the ability to allow the user to use a grid control to enter one or many child objects. Under the covers this grid control is of course bound to a CSLA child collection. In many cases I’ve needed the ability to implement a rule that says a value is Unique within the parent collection. Sometimes this is because of a unique key constraint in the database, and other times its just a business requirement.

CSLA business rules are great out of the box for validating the values in the current object, but gets slightly more involved when the value you are validating is actually in another object.
In a past project I implemented a UniqueValue rule by passing in the parent collection to the child factory method and storing a reference to the parent in a member variable. It was then available by the time the CheckRules() method was fired. This worked well, but passing in the parent is tedious, especially in code generation scenarios.

In the latest release of CSLA I notice that the Parent property in Csla.Core.BusinessBase is now marked Protected. This makes it easier to get reference to the parent object, but it’s not populated until it’s been added to the parent collection (after the CheckRules() method has been called);

I've looked at the Parent code section in the Csla.Core.BusinessBase class and the definition looks like:
   
#region Parent/Child link

[NotUndoable()]
[
NonSerialized()]
private Core.IParent _parent;

/// <summary>
/// Provide access to the parent reference for use
/// in child object code.
/// </summary>
/// <remarks>

/// This value will be Nothing for root objects.
/// </remarks>

[EditorBrowsable(EditorBrowsableState.Advanced)]
protected Core.IParent Parent
{
     
get { return _parent;
}

/// <summary>
/// Used by BusinessListBase as a child object is
/// created to tell the child object about its
/// parent.
/// </summary>
/// <param name="parent">A reference to the parent collection object.</param>
internal void SetParent(Core.IParent parent)
{
   _parent = parent;
}

#endregion


Is there a specific reason the Parent property doesn't expose an OnChanged event handler or and overridable OnChangedMethod?  If not, Rocky is a possibility one could be added?


Thanks,

Shawn Camp

 

RockfordLhotka replied on Monday, January 28, 2008

Child objects do raise an event - either PropertyChanged or ListChanged, because a child can only be one of BusinessBase or BusinessListBase.

In CSLA .NET 3.5 child objects' events are automatically handed by their parent, which raises its corresponding changed event - all the way up the chain.

So in your case, you can simply handle the parent list's ListChanged event to check the rule, because that event will (in 3.5) be raised when the list itself changes, or when any of its child objects are changed.

Copyright (c) Marimer LLC