View selection based on data type

Topics: Questions
Jul 11, 2012 at 7:32 PM

Hello,

in my app I have a business class representing a Contact.
Based on a property, this contact can be a Person or a Corporation.
Many properties are the same on these both, but some are specific.

When visualizing such a contact, some details have to be displayed
dependent of the contact type.
For example a Person has Salutation, First- and Lastname.
A Corporation has CorporationKind, Name1 and Name2.

I have a ContactView and a ContactViewModel.
One approach I figured out is to switch the visibility of two blocks
within the XMLA according to the contact type.

Or should I design three Views?
ContactView, ContactPersonDetailView and ContactCorporationDetailView
all three with the same model, where ContactView embeds one of the other two?
But where and how switch the detail views on loading? 

How would you solve this kind of problem?

thanks and regards, alex. 

Coordinator
Jul 11, 2012 at 8:32 PM

Hello,

If the view has real separate display attributes, then I would create 2 separate views. This is easier to maintain then one single view with lots of converters to show/hide the right items.

If it is for display only, you might consider using a single property (DisplayName) that is correctly calculated by the model or view model, and the view doesn't care what is there to display. If it is an enum with a combobox that you need to select, it gets hard and you will have to do more work to keep it 1 single VM with 1 single V then writing 2 simple views and view models for person and corporation.

I would go for 2 views and 2 view models.

Jul 14, 2012 at 2:21 PM

I do not like the two-view solution.
Most (more than 90%) of the views will be exactly identical.
And these 90% are quite complex and extensive.

Only a small area of the view has to switch the design based on the data to display.

The question seems to be something like asked here
A possible solution is posted on stackoverflow, but I need a catel compatible solution. 

Any more ideas/hints?
Thanks alex. 

Coordinator
Jul 18, 2012 at 7:49 AM
Edited Jul 18, 2012 at 7:50 AM

In my experience, in the end the views will be different any way, so I still recommend the 2-view solution. However, you can do this:

1) Create a shared base view model and link this one to your UC.

2) In your view, override the GetViewModelInstance method and create the right sub VM, based on your model or datacontext. Then you can re-use the same view for multiple view model types.

Jul 18, 2012 at 7:20 PM

Hello Geert,

can you please explain what exactly GetViewModelInstance() does?
I know GetViewModelType(), but I think these two are less related to each othen then their names suggest....

Is there an example using this method?

thanks, alex. 

Coordinator
Jul 19, 2012 at 9:08 AM

See this documentation. I hope it does help you out.