When can I combine 2 view modes into 1 instead of using some form of viewmodel-viewmodel?

I have 2 viewing modes, each of which has its own view.

The first view model has 3 properties displayed by the view:

PolicyProvider PolicyType PolicyNumber 

the second representation model has only 1 property displayed by its representation:

 TypeOfInvestmentFund 

Between PolicyType and TypeOfInvestmentFund.

there is a ratio from 1 to many.

Both of these view models and their representations appear as user controls inside the parent form.

The options available for the TypeOfInvestmentFund depend on which PolicyType selected in another view .


It seems to me that these two viewing models can be combined because

a) they are clearly somewhat related

b) the controls are so small and simple that combining them will not create a complex and uncontrollable object.

However, this data is rather unrelated; that the user still wants the data to be visible in separate parts of the form (and therefore be placed on separate views).

I personally believe that combining these two presentation models and having two separate views to connect to them to display different parts is much less overhead, and then managing the relationship between the two objects.

I could create loosely coupled events with the Prism event aggregator, and although I have never done it, it is probably not much to handle, and keeping these two view models separately will save the difference in problems. In addition, if other controls appear in the development process that also need this information, I cannot absorb them, so starting the event aggregator at this stage will prevent further development, since the events will already be available for subscription. This is even more work, and then combining view models.

Which of these 2 is "more correct"? I understand his decision, but I can’t decide, so I am looking for opinions to help me decide.

+4
source share
3 answers

Ok thanks for the answers. Got a bunch of ideas from Rachel's proposal (upvote for you ma'am), but none of them went down. In the end, the project manager did not like this idea and wants me to implement a more standard approach for reading and preventing alterations, so I'm going to go to the root of the message.

Instead of an eventaggregator, I'm just going to make a parent subscriber to the PropertyChanged event of the child of the Policy , and then change the property of the child of TypeOfInvestment .

I kind of joked. I was unable to implement the viewmodel merge, as it really made more sense to me.

0
source

ViewModel reflects view, not data

If your view displays Policy and dynamic TypeOfInvestmentFund , then of course your ViewModel should have both of these objects.

Personally, I would like my ViewModel to display the Policy model for the view and have the PolicyModel properties for Provider , Type , Number and InvestmentFund

Then I could use DataTemplates to tell WPF how to draw each object. Here is an example that describes how you will do this:

 <DataTemplate DataType="{x:Type local:PolicyModel}"> <StackPanel> <local:PolicyView /> <ContentControl Content="{Binding InvestmentFund}" /> </StackPanel> </DataTemplate> <DataTemplate DataType="{x:Type local:InvestmentFundA}"> <local:InvestmentFundA /> </DataTemplate> <DataTemplate DataType="{x:Type local:InvestmentFundB}"> <local:InvestmentFundB /> </DataTemplate> 

Edit

If Policy and TypeOfInvestment are two separate objects, I would save their Models separately and put them in the same ViewModel . Models are for modeling your data, and ViewModels are for modeling your View

+3
source

I consider such problems as very similar to database normalization. On the one hand, normalization is good practice, as two separate presentation models are created. But at the same time, a certain degree of denormalization is known, which also helps in productivity.

I personally believe that combining these two presentation models and having two separate views to connect to them to display different parts is much less overhead, and then managing the relationship between the two objects.

And this statement says it all. The combination of the two models of views cannot be considered "best practice", the feeling that I get from you is that it makes sense for your application (based on the grip levels and complexity that you mentioned). I would say continue to combine them and keep track of how this works.

+1
source

Source: https://habr.com/ru/post/1389293/


All Articles