Bug in Roles object?

Bug in Roles object?

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


Cuong posted on Friday, July 14, 2006

Bug reproduce:
+ Run PTWin and login with admin role
+ Choose Admin | Edit Roles
+ Insert 2 roles with the same id value. E.g:
Role1 = (7, "test1")
Role2 = (7, "test2")
There will be an error icon in the id cell of role2 (because of NoDuplicates rule)
+ Now delete the role1 row => the role2 becomes totally valid but the error icon still exists in its id cell
+ Continueing to click on Save button will throw a data exception although all data is valid.

I think the error is Roles does not call its childrent's CheckRules method when it Removes child objects. Unfortunatelly the BusinessBase does not have CheckRules method and its ValidationRules property is protected.

Does anyone have an idea to resolve this problem? My current project must create some business objects like Roles but I have not found any good solution.


RockfordLhotka replied on Monday, July 17, 2006

That's an interesting scenario - yes, I can see where the problem comes in here. I'll have to give some thought to a good solution. I don't like the idea of making CheckRules() public, for instance, as that seems like serious overkill.

The real issue is that child objects, in general, may need to know when the collection's contents have changed. Or more broadly, when the parent object has changed. It is up to the child what it might, or might not, do in response to a change in its parent.

So what's really needed, is a mechanism by which a child can know that the parent changed. One solution would be for Core.IEditableCollection to define a method that is invoked by the parent on each child. A default (empty) implementation would be in Core.BusinessBase, but you could override that and call CheckRules() if it was important for your particular object.

Another option that would work today, is that the child automatically has a Parent property, referring to the collection. You could just hook the existing ListChanged event and thus be notified of any changes to the parent collection. In that event handler you could call CheckRules(). That would require casting Parent to IBindingList, and so it isn't as elegant, but it would certainly work.

Cuong replied on Wednesday, July 19, 2006

Thank Rockford, I will follow your suggestion to hook the parent collection's ListChanged event.  I still a little bit wonder where and when to hook this event. I will do something like bellow  but I am not sure if it is the best solution.

public class MyChild : BusinessBase<MyChild>
{
    internal static MyChild NewChild(MyParentCollection parent)
    {
       MyChild child = DataPortal.Create<MyChild>();
       parent.ListChanged += delegate { child.ValidationRules.CheckRules(); };
       return child;
    }
}

I hope you will have some small improving in Csla in the future version. to resolve this issue easier


Copyright (c) Marimer LLC