Better ways of doing 1-n relationships in Cinch?

Aug 1, 2011 at 9:42 AM
Edited Aug 1, 2011 at 9:43 AM

Hey, I'm struggling to figure out the best way to code my ViewModels to support 1-n relationships in my domain entities (e.g. Customer has many Orders) and was hoping someone could point me in the right direction to, hopefully a cleaner solution than mine. The stupid thing is I don't even know what question I'm asking here so bare with me...

I've looked through Sacha's CinchV1 demo which has the Customer.Orders relationship in its entities. This demo uses Model classes (e.g. CustomerModel for Customer and OrderModel for Order) which copy entity properties to & from their own DataWrappers and uses the DispatcherNotifiedObservableCollection class for the 1-n relationship. This seemed like a good idea so I went down this path but it seems to clash with how I need to implement it (and I think Sacha denounced using these Model classes in the documentation somewhere). I like how this way copies the Customer entity and its Order entities into its own DataWrappers (instead of simply wrapping the entity properties). The problem seems to start when I need to transfer this data back into the Entities when I want to save the data. My entities have Timestamp properties to assist with Concurrency Conflict detection, so I need to make sure I preserve these. To do this, I merge the Models back into the entities that I loaded when the ViewModel loaded - this merge process looks quite ugly and will become uglier as I'd have to duplicate this merge code for each and every 1-n relationship in all of the Model classes.

Another problem I face with this is that whatever I decide to go with now will be used as the foundation for future projects, so it needs to be flexible enough to support the potentially different needs of different apps e.g. some apps will want to force the user to save the Customer before allowing adding/editing/deleting of Orders, whereas other apps may want to allow the adding/editing/deleting of Orders before the Customer has been saved i.e. manage it all in memory and save the changes to Customer and all Orders in one hit at the end, or allow undo'ing of all changes.

Has anyone dealt with this problem before? If so, what did you do with your ViewModels, Models etc? did you bind directly to your domain entities? or, did you copy them into Model objects and bind to them?

Any help would be much appreciated as I've ground to a halt on ideas right now.




Aug 7, 2011 at 6:42 PM

You are correct I no longer encourage using the Model classes in Cinch V1, instead use ViewModels. And we too use the timestamp idea, and what we do is have models coming from the database (which have timestamp), and then we have these models exposed through the ViewModel where the ViewModel writes to the Model through the VM properties, and we then persist the model back to the database where the timestamp is checked and comes back with exception to the UI.

In terms of managing collections we would create a new VM for each Model in a collection, but that VM would no what properties of the model to expose.


That is how I do it.