4

I have 2 viewmodels that each have their own view.

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

PolicyProvider

PolicyType

PolicyNumber

the second view model has only 1 property being displayed by its view:

TypeOfInvestmentFund

There is a 1 to many relationship between the PolicyType and the TypeOfInvestmentFund.

Both of these view models and their views are being displayed as user controls inside a parent form.

The available choices for the TypeOfInvestmentFund is dependent on which PolicyType is selected in the other view.


To me this feels like these 2 view models could be combined, because

a) they are clearly somewhat coupled

b) the controls are both so small and simple that joining them will not create a complex and unmanageable object.

However this data is fairly unrelated; unrelated enough that the user would still want the data visible in seperate parts of the form (and therefore be placed on seperate views).

I personally feel that combining these 2 view models and having 2 seperate views connect to it to display different parts of it is a lot less overhead then managing the communication between the 2 objects.

I could however create loosely coupled events with the Prism Event Aggregator, and although I have never done this it is probably not much to manage, and keeping these 2 view models seperate would preserve seperation of concerns. Furthermore if other controls were to appear later on in development that also need this information, I cant keep absorbing them, so starting an event aggregator at this stage would prevent rework as the events would be available already to subscribe to. It is still more work then just combining the view models.

Which one of these 2 is 'more correct'? I understand its a judgement call, but I can't decide so I'm looking for opinions to help me make up my mind.

4

3 回答 3

3

ViewModel 反映的是视图,而不是数据

如果您的 View 显示 aPolicy和 a dynamic TypeOfInvestmentFund,那么您的 ViewModel 应该同时拥有这两个对象。

就我个人而言,我会让我的 ViewModelPolicy向 View 公开一个模型,并具有PolicyModel包含ProviderTypeNumberInvestmentFund

然后我可以使用 DataTemplates 告诉 WPF 如何绘制每个对象。这是一个粗略的示例,概述了您将如何做到这一点:

<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>

编辑

如果PolicyTypeOfInvestment是两个独立的对象,我Models会将它们分开,并将它们放在同一个ViewModel. Models用于建模您的数据,而ViewModels用于建模您的数据View

于 2012-01-04T15:40:07.243 回答
1

我认为这样的问题与数据库规范化非常相似。一方面,规范化是一种很好的做法,就像创建两个独立的视图模型一样。但与此同时,已知一定量的非规范化也有助于提高性能。

我个人认为,将这 2 个视图模型结合起来并让 2 个单独的视图连接到它以显示它的不同部分,这比管理 2 个对象之间的通信开销要少得多。

那句话说明了一切。虽然组合这两个视图模型可能不被认为是“最佳实践”,但我从您那里得到的感觉是它对您的应用程序有意义(基于您提到的耦合程度和复杂性)。我会说继续将它们结合起来并密切关注它的表现。

于 2012-01-04T15:37:48.797 回答
0

好的,谢谢您的回复。从 Rachels 的建议中得到了一堆想法(为你投票,女士),但没有一个成功。最后,项目经理不喜欢这个想法,并希望我实施一种更标准的方法来提高可读性并防止返工,所以我打算去消息根。

而不是 eventtaggregator 我只是要让父订阅子的 PropertyChanged 事件,Policy然后更改子的属性TypeOfInvestment

我有点沮丧我没有实现视图模型合并,因为它对我来说真的更有意义。

于 2012-01-05T08:37:00.250 回答