Design Question

Design Question

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


pfowler posted on Wednesday, September 27, 2006

Hi I have a design question I would like some feedback on.

I have the following three classes; EditableRoot, EditableChildList and EditableChild. (EditableChildList is contained by EditableRoot.)

The scenario is to create an EditableRoot and then setup an EditableChild and then call save on EditableRoot to save them both in the same transaction. Something roughly like the following:

EditableRoot er = EditableRoot.NewEditableRoot();

er.Name = "ABC";

EditableChild ec = er.NewEditableChild();

ec.Desc = "ZXY abc";

er.AddEditableChild(ec);

er.Save();

What is the recommended best practice to achive this with CSLA. Are there any standard naming conventions and or approaches to this problem?

Thanks in advance.

Peter

ajj3085 replied on Wednesday, September 27, 2006

Well, it depends.

Usually you have a property on the ER to get to the collection.  So you'd have something like this:

public EditableChildList Children {
    get { return ecl; }
    private set {
            if ( ecl != value ) {
                  if ( ecl != null ) {
                     ecl.ListChanged -= new ListChangedEventHandler( ListChanged );
                  }
                  elc = value;
                  if ( ecl != null ) {
                     ecl.ListChanged += new ListChangedEventHandler( ListChanged );
                  }
             }
      }
}

private void ListChanged( object sender, ListChangedEventArgs e ) {
   PropertyHasChanged( "Children" );
}

Then you could do:

er.Children.Add( ec );

or if you override AddNewCore in ECL, you can do:

ec = er.Children.AddNew();

but only if you can create a new child without filling in any extra information.

HTH
Andy

pfowler replied on Wednesday, September 27, 2006

Andy, thanks for the response.

Ok, what I think I will do is the following:

EditableRoot er = EditableRoot.NewEditableRoot();

er.Name = "ABC";

EditableChildList ecl = er.Children;

EditableChild ec = EditableChild.NewEditableChild();

ec.Desc = "ZXY abc";

ecl.Add(ec);

er.Save();

What is the primary purpose of the events in the property? Are they required for binding purposes?

Thanks

Peter

 

ajj3085 replied on Thursday, September 28, 2006

That is perfectly valid.

Yes, the purpose of catching the ListChanged event is so that the parent can 'bubble' the event up, although in this case you're changing the event raised. 

The reason is that you typically listen to the CurrentItemChanged events from the bindingsource to which your root object is bound so that you can properly enable / disable the button / toolbar that allows saving.

Andy

Copyright (c) Marimer LLC