views:

689

answers:

3

I'm wondering what strategies people are using to handle the creation and editing of an entity in a master-detail setup. (Our app is an internet-enabled desktop app.)

Here's how we currently handle this: a form is created in a popup for the entity that needs to be edited, which we give a copy of the object. When the user clicks the "Cancel" button, we close the window and ignore the object completely. When the user clicks the "OK" button, the master view is notified and receives the edited entity. It then copies the properties of the modified entity into the original entity using originalEntity.copyFrom(modifiedEntity). In case we want to create a new entity, we pass an empty entity to the popup which the user can then edit as if it was an existing entity. The master view needs to decide whether to "insert" or "update" the entities it receives into the collection it manages.

I have some questions and observations on the above workflow:

  • who should handle the creation of the copy of the entity? (master or detail)
  • we use copyFrom() to prevent having to replace entities in a collection which could cause references to break. Is there a better way to do this? (implementing copyFrom() can be tricky)
  • new entities receive an id of -1 (which the server tier/hibernate uses to differentiate between an insert or an update). This could potentially cause problems when looking up (cached) entities by id before they are saved. Should we use a temporary unique id for each new entity instead?

Can anyone share tips & tricks or experiences? Thanks!

Edit: I know there is no absolute wrong or right answer to this question, so I'm just looking for people to share thoughts and pros/cons on the way they handle master/details situations.

+1  A: 

There are a number of ways you could alter this approach. Keep in mind that no solution can really be "wrong" per se. It all depends on the details of your situation. Here's one way to skin the cat.

who should handle the creation of the copy of the entity? (master or detail)

I see the master as an in-memory list representation of a subset of persisted entities. I would allow the master to handle any changes to its list. The list itself could be a custom collection. Use an ItemChanged event to fire a notification to the master that an item has been updated and needs to be persisted. Fire a NewItem event to notify the master of an insert.

we use copyFrom() to prevent having to replace entities in a collection which could cause references to break. Is there a better way to do this? (implementing copyFrom() can be tricky)

Instead of using copyFrom(), I would pass the existing reference to the details popup. If you're using an enumerable collection to store the master list, you can pass the object returned from list[index] to the details window. The reference itself will be altered so there's no need to use any kind of Replace method on the list. When OK is pressed, fire that ItemChanged event. You can even pass the index so it knows which object to update.

new entities receive an id of -1 (which the server tier/hibernate uses to differentiate between an insert or an update). This could potentially cause problems when looking up (cached) entities by id before they are saved. Should we use a temporary unique id for each new entity instead?

Are changes not immediately persisted? Use a Hibernate Session with the Unit of Work pattern to determine what's being inserted and what's being updated. There are more examples of Unit of Work out there. You might have to check out some blog posts by the .NET community if there's not much on the Java end. The concept is the same animal either way.

Hope this helps!

Kevin Swiber
Thanks for your feedback. The problem with passing references to edit is that you can't just cancel or undo the changes without storing the original values of the entity.
Christophe Herreman
I think I might be missing something. For clarification, are you saying that your master list has different states, such as dirty/clean? Do you add and edit items from the master in the details window, click OK in the details window, and then have to commit or rollback changes using some control on the master window?
Kevin Swiber
Are your data objects bound to the form in some way that is not obvious from your example? I'm just wondering where you call your .set methods. I guess I'm thinking along the same lines as Kevin there.
angryundead
The master holds a cached list of persisted entities. When the user clicks an item from the master list, a popup opens with the details of the selected item which the user can edit. We use a copy of the selected item to edit, so we can undo the changes to the item just by closing the details window. However when we click OK in the details, we close the popup and need to merge the changes. The changes are not persisted immediately. There is a save action on the master that persists all changes in the database via (async) remote calls. The unit of work pattern might be interesting here.
Christophe Herreman
A: 

The CSLA library can help with this situation a lot.

However, if you want to self implement :

You have a master object, the master object contains a list of child objects.

The detail form can edit a child object directly. Since everything is reference types, the master object is automatically updated.

The issue is knowing that the master object is dirty, and therefore should be persisted to your database or whatnot.

CSLA handles this with an IsDirty() property. In the master object you would query each child object to see if it is dirty, and if so persist everything (as well as tracking if the master object itself is dirty)

You can also handle this is the INotifyPropertyChanged interface.

As for some of your other questions :

You want to separate your logic. The entity can handle storage of its own properties, and integrity rules for itself, but logic for how different object interact with each other should be separate. Look into patterns such as MVC or MVP.

In this case, creation of a new child object should either be in the master object, or should be in a separate business logic object that creates the child and then adds it to the parent.

For IDs, using GUIDs as the ID can save you quite a bit of problems, because then you don't have to talk to the database to determine a correct ID. You can keep a flag on the object for if it is new or not (and therefore should be inserted or updated).

Again, CSLA handles all of this for you, but does have quite a bit of overhead.

regarding undo on cancel : CSLA has n-level undo implemented, but if you are trying to do it by hand, I would either use your CopyFrom function, or refresh the object's data from the persistance layer on cancel (re-fetch).

Jason Coyne
A: 

Hi,

i just implemented such a model.but not using NH, i am using my own code to persist objects in Oracle Db.

i have used the master detail concept in the same web form.

like i have master entity grid and on detail action command i open a penal just below the clicked master record row.

On Detail Add mode, i just populate an empty entity whose id were generated in negative numbers by a static field.and on Save Detail button i saved that entity in the details list of the Master Record in Asp.NET Session.

On Detail Edit,View i populated the Detail Panel with selected Detail through ajax calls using Jquery and appended that penal just below the clicked row.

On Save Button i persisted the Master Session (containing list of Details) in database.

and i worked good for me as if multiple details a master need to fill.

also if you like you can use Jquery Modal to Popup that Panel instead of appending below the row.

Hope it helps :) Thanks,

Usama Khalil

related questions