AddNewCore and OnAddingNew

AddNewCore and OnAddingNew

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


daniel_higgins posted on Tuesday, December 09, 2008

I've been under the impression that the way to handle adding new items to a collection in Csla is to override the AddNewCore method. However, poking around in the BindingList base class suggests that it may be more appropriate to override the OnAddingNew method, and set the NewObject field of the AddingNewEventArgs object to the newly created child object. Perhaps something like:

protected override void OnAddingNew(AddingNewEventArgs e){
    base.OnAddingNew(e);
    if (e.NewObject == null) e.NewObject = Child.New();
}

ajj3085 replied on Tuesday, December 09, 2008

I think that you'll get an exception, because if you're following Csla design, you're constructor will be private and the standard AddNewCore will fail.

daniel_higgins replied on Tuesday, December 09, 2008

The AddNewCore base class method will raise the OnAddingNew event. If you override the OnAddingNew (or simple register for the event (e.g. in UI code)), and set the AddingNewEventsArg.NewObject property to the new child object (i.e. you create it using your static factory method), when AddNewCore finds that AddingNewEventArgs is not null, it will use this instead of trying to call the constructor.

Copyright (c) Marimer LLC