Child object initialization

Child object initialization

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


jasona22 posted on Thursday, September 20, 2012

I saw some 2010 posts on this topic, but they seemed dated.  So I will ask what is likely been beaten down before.

We are using .NET, CodeSmith generation, MEF, Prism, WPF and MVVM technologies/patterns.

The short version is: How can I initialize a child object on creation?

Explanation:

I have a "Flight" record (Editable Root).  This Flight record has a child collection on it AirCrew (EditableChild).  From the UI, there is a list of Personnel available for selection that are added as AirCrew.  For the most part, an AirCrew object is an ID, a FlightID, a PersonnelID and some detail data that is unimportant for this discussion.  One of the things that I would like to do is display the name of the person selected on the UI as a readonly property when they are added to the AirCrew; however name is not stored with the AirCrew object (don't want to repeat data if possible).  So when AddNew is called to add an AirCrew object, I would like to provide the Personnel.Name as a parameter. 

A solution that we do not like is to create read/write properties such that the UI can set the Name after the AirCrew is created.  However, this makes the UI know something about the business purpose.  For 'separation of concerns' purposes, we prefer the UI to not do any kind of business object initialization.

Anyone have any ideas on a good way since AddNew/AddNewCore does not accept parameters?

Thanks

StefanCop replied on Thursday, September 20, 2012

In the ebook DataAccess "Creating New Grandchild Objects" describes differebnt options:

You can use the built-in AddNew functionality, or the Add method, or a custom AddItem method. - however, AddNew() is no option for you.

I would prefer AddItem(parameters). 
The other option is:  Add( ChildType.NewChild(parameters) ) -- I don't want developers get used to "create" child objects just like root objects.

jasona22
However, this makes the UI know something about the business purpose. 

Most of the time only at UI level (ViewModel) you know the context. So, that's not an concern to me.

 

JonnyBee replied on Thursday, September 20, 2012

You may also consider to create 

 

 

Copyright (c) Marimer LLC