Object Model Question

Object Model Question

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


David posted on Friday, October 27, 2006

I have an editable root object, and two editable collections, and I can’t determine if the collections should be children of the root object, or root objects themselves.

Working from the use cases, I started out by making them children of the root object as this seemed to make the most sense. However, I need to edit each collection on a separate form, and things became complicated when I needed to do a save. In this configuration, the save needs to be initiated by the root object (which is on a 3rd form), so I guess it would go something like this:

  1. The root form notifies each child form that it is about to save
  2. Each child form uses the binding source control to end the current edit, checks the object IsValid, and returns a result to the root that it is (or is not) OK to proceed.
  3. The root form clones and saves the object, and updates the references to the new object
  4. The root form notifies each child form that the object has changed.
  5. Each child form resets the data source to the new object passed to it by the root form.

At first I thought this was much to complex for the UI design and reverted to a model where the collection objects were both EditableRootCollections and simply made the Root object responsible for getting a new instance of each one. However, this just produces complexity elsewhere because my root object has properties and methods that rely on a reference to the two collections.

So (he finally gets to the question!), do you think my initial design is OK, and has anyone else written a UI like this? Basically I’m looking for reassurance that this is a reasonable approach.

Thanks in advance.

David

 

guyroch replied on Friday, October 27, 2006

According to your use case this is a perfect candidate for a switchable object, p387 of c# book.

But beware, a switchable object is usually a sign of bad design - but not always.  So it's up to you to decide if this fits your requirement.

A switchable object can be instantiated either as a root object or as a child object.

Hope this helps

David replied on Saturday, October 28, 2006

Thanks for the comment, but I don't see how a switchable object solves my problem. It does give me the option to use either scenario from the UI, but I would then have to write the code to cater for both scenarios. So I would have to deal with both complexities.

I thought switchable objects were used to avoid duplication of code - when you needed to use a similar object in different use case scenarios. This is not really the problem here.

 

guyroch replied on Saturday, October 28, 2006

David:

I have an editable root object, and two editable collections, and I can’t determine if the collections should be children of the root object, or root objects themselves.

If you _code_ your two editable collections as switchable collections then you'll be able in instantiate your collections as child collections of your editable root object AND you'll also be able to instantiate the collections as _root_ editable collections as well.  This way you will be able to call the save method directly from the now _root_ editable collections.

I think your collections should be root objects because the responsibility to update the content of your collections seems to be different from the responsibility of your editable root object.  In the case, the responsibility of your root object is to display the collections and not necessarily update the content.

Maybe the solution here would be to have an editable root object with 2 read only collections for display purposes only.  Then your 2 root collections for update purposes.

This looks to me like a classic case of 2 distinct reponsibility - I could be wrong though because I don't know the context of your application.

 

 

guyroch replied on Saturday, October 28, 2006

Ummm... on second thoughts...

Maybe what you need is to look at Petar's ActiveObjects at http://csla.kozul.info/.  Its an extention to Csla with an extensive _observer_ pattern implementation.  It is meant to do something similar to what you are trying do to.  I toyed with ActiveObject last year while still in 1.5x, but I havn't done any ActiveObject stuff with Csla 2x.

Hope this helps

Bayu replied on Saturday, October 28, 2006

guyroch:

Ummm... on second thoughts...

Maybe what you need is to look at Petar's ActiveObjects at http://csla.kozul.info/



I agree with this last comment of guyroch.

To me it sounds like you have 3 independent forms:
- one for your 'root' object
- one for each of your collections

Imo this validates a BO design where the collections are editable root collections. This complies with the 'use case driven design principle' often referred to on this forum. The fact that your 'root' object bases some of its behavior on the contents of the collections could then be seen as a 'creational constraint' or so:
- whenever one of the collections is displayed, the root object must be instantiated as well
- this enables the root object to monitor the collections (e.g. using petar's observer pattern)

To ensure that you root object is always instantiated when required you can do as guyroch suggests: make those collections exclusively available to the UI through properties on this root object.

I guess then you would meet all your requirements/wishes:
- each form can update/save independently
- the logic of having another object monitoring those collections is where it belongs: in the BO, and not in your UI, so your UI can remain 'dumb'
- you ensure that your root object observes the collections by applying a creational pattern that satisfies your creational constraint

Last but certainly not least:
- you avoid the ugliness/complexities of switchables

Good luck!
Bayu

Copyright (c) Marimer LLC