2

i faced with some problems when designing architecture of my extensible programm.

I'm using MEF, MMVM Light Toolkit and AvalonDock.

The first problem is how display view for some ViewModel imported from another assembly using MEF. To solve it, i'm exporting ResourceDictionary where i'm defining DataTemplate's for views declared in this assembly.

Dictionary:

<ResourceDictionary 
  ...>
  <DataTemplate DataType="viewmodels:MyViewModel">
    <views:MyViewForViewModel/>
  </DataTemplate>
</ResourceDictionary>

And in constructor of MainWindow i'm importing all ResourceDictionaries and merging them with MainWidow.ResourceDictionary. Is it good? It's also possible to specify 'scope' of ResourceDictionary to import it not to MainWindow, but to Application for example.

The second problem is ICommands and CommandBindings. To populate Menu i'm exporting 'MenuItems' where i'm defining ICommand, Text and other stuff, but i don't know how to export CommandBinding, should i use RelayCommand for cases when i can't create CommandBinding?

The third problem is dialogs. I found great article Showing Dialogs When Using the MVVM Pattern and easily adapt it to MEF. But, for example, I have an IDatabaseService which don't have any View. The Workspace, main ViewModel, storing instance of IDatabaseService and creating menu item: Connect to Database. Using IDialogService Workspace opening some imported IConnectToDbDialog so Workspace don't know anything about it. When dialog closed, the SqlConnectionString should be passed to IDatabaseService. So who must pass this SqlConnectionString, IConnectToDbDialog or Workspace.

The fourth problem is how to communicate with IDatabaseService correctly. For example. In some View i have Button: 'Create Item In Database'. And how should i call IDatabaseService method CreateItem(ElementType elementType) when button clicked? The problem, that there are a lot of buttons which create Items with different ElementType in database, so, i think, it's right to create some ICommand with parametr and create only one handler for this command which will invoke some method in IDatabaseService. But i don't know how. The other solution is to send messages to IDatabaseService from ViewModel to create item. which way better?

4

1 回答 1

2

试着回答你的问题。

  1. 这很好。您可以在 XAML 或代码后面合并,但我更喜欢 XAML。您可以将它放在 MainWindow.Xaml 上,它在主窗口范围内或 App.Xaml 上,它在应用程序范围内。

  2. 我之前没有导出视图。在我看来,如果您将 CommandBindings 放在 Menu 下,如果事件处理程序在导入的环境范围内,则何时导出然后导入都没有关系。

  3. 这取决于。从理论上讲,您可以将服务调用放在所有者的视图模型或对话框的视图模型中。例如,如果您的对话框有一个创建/提交按钮,并且您希望对话框在提交成功之前一直保持活动状态,则将其放入对话框的视图模型中,以便在处理异常时可以保持打开状态。如果您不需要对话框保持打开,那么您可以在对话框关闭后将逻辑放在所有者的视图模型中。

  4. 指挥更好。考虑到视图模型从 IoC 容器获取 IDatabaseService 对象,您可能有一个接受 ElementType 参数的 ICommand 属性,或者一个参数可以映射到 ElementType。在执行方法中,您调用 CreateItem 直接或从映射器传递参数。在您的 XAML 上,您将 type 放入命令绑定中。是否有意义?

希望它可以提供帮助。

于 2013-07-24T17:11:13.323 回答