Adding a Child Object to a Child Collection

Adding a Child Object to a Child Collection

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


MadGerbil posted on Monday, August 18, 2008

I've an EditableRoot object (Customer) that contains an EditableChildCollection(Addresses).

I want to add a child object(Address) to the EditableChildCollection (Addresses) from the UI but the example code (pg 385-387 C# VS2005) seems to indicate that the child object should be added to the collection through the EditableRoot (Customer) object.  This makes sense, especially if the child object in teh collection is going to have a parentid from the EditableRoot.

In the past I've changed the code somewhat and made it so I could create the child objects on their own, populate them with data, and then add them to the child collection - however, this time around I'm trying to follow the CSLA design more closely than I ever have before. (Good way to learn).

So how do you guys handle this - for some reason it looks odd to me - but would you have a method like Customer.NewAddress()?   That seems "odd" to me since I'd expect to write Addresses.NewAddress() or even more naturally, Address.NewAddress(). 

So if you were going to create a new Address object which would (after editing) be added to the Addresses collection of a Customer object which one would you use?

Customer.NewAddress()

Addresses.NewAddress()

Address.NewAddress()

 

ajj3085 replied on Monday, August 18, 2008

I'm not sure it really matters one way or the other, as long as your library is consistent with itself.  I usually have Address.NewAddress, if the address can be created outside the context of a Customer and then added to the customer's address list without issue.

If it can't, then Addresses.NewAddress seems more natural.  In addition, since I usually have the address list in an editable grid, I override AddNewCore in the BLB class so that it calls the appropriate factory method to add the address.  That is probabaly always a good idea to do.

Copyright (c) Marimer LLC