MVVM 中的依赖项

发布于 2024-07-30 08:51:30 字数 626 浏览 1 评论 0原文

我刚刚开始深入研究 WPF 的 MVVM 模式,但因为我不是专业开发人员,所以有时我会感到有点迷失。

我遇到的主要问题是决定谁应该创建谁:假设我有一个存储狗和猫的数据库。 然后,我将拥有一个与数据库(工作单元!?)通信的 DatabaseManager、DogRepository / CatRepository 和 Dogs / Cats 对象。

另一方面,我有一个 MainWindow,用户可以用它打开/关闭数据库,其中包含一个用于猫的列表框和一个用于狗的列表框,所以我需要一个 MainWindowViewModel、CatsViewModel 和 DogsViewModel。

但我如何创建这些对象呢? 在我的应用程序中,我显然创建了主窗口。 进而? 我是创建一个 MainWindowViewModel 并将其传递给 MainWindow 还是 MainWindow 创建自己的 MainWindowViewModel? 数据库管理器是如何创建的? 通过MainWindowViewModel?

那么 CatsViewModel 和相应的 CatRepository 呢? MainWindowViewModel 是否创建了 CatsViewModel,而后者又创建了 CatRepository? 我真的很困惑。

I'm just starting to dig into the MVVM pattern for WPF but because I'm not a professional developer, I sometimes feel a little lost.

The main problem I have is deciding who should create whom: Let's say I have a database storing dogs and cats. I would then have a DatabaseManager which communicates with the database (Unit of Work!?), a DogRepository / CatRepository and the Dogs / Cats objects.

On the other side I have my MainWindow with which the user can open/close the database and which contains a Listbox for cats and one for dogs, so I need a MainWindowViewModel, CatsViewModel and DogsViewModel.

But how do I create these objects? In my application I create the MainWindow obviously. And then? Do I create a MainWindowViewModel and pass it to the MainWindow or does the MainWindow create its own MainWindowViewModel? How is the DatabaseManager created? By the MainWindowViewModel?

What about the CatsViewModel and the corresponding CatRepository? Does the MainWindowViewModel create the CatsViewModel which in turn creates a CatRepository? I'm really confused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落成白 2024-08-06 08:51:31

视图-模型-视图模型 (MVVM) 模式没有定义谁负责创建不同的类。 因此,您会在 MVVM 社区中发现很多不同的方法。

我喜欢使用应用程序控制器或用例控制器来处理应用程序的工作流程,因此它们负责创建 ViewModel 和 Repository 类。

项目的 ViewModel 示例中显示了其工作原理的具体示例:

WPF 应用程序框架 (WAF)

http://waf.codeplex.com

The View-Model-ViewModel (MVVM) pattern doesn't define who is responsible to create the different classes. Therefore, you find a lot different approaches in the MVVM community.

I like to use an Application Controller or use-case controllers that handle the work-flow of the application and so they are responsible to create the ViewModel and Repository classes.

A concrete example how this works is shown in the ViewModel sample of the project:

WPF Application Framework (WAF)

http://waf.codeplex.com

居里长安 2024-08-06 08:51:30

您很困惑,因为所有这些选项都是有效的。 如果您没有使用依赖注入,并且 ViewModel 拥有它们自己所需的所有信息,则没有理由不让视图创建视图模型(通常通过为视图创建一个 StaticResource 来完成:

<Window.Resources>
     <local:CatViewModel x:Key="catVM" />
</Window.Resources>
<Grid DataContext="{StaticResource catVM}">
...
</Grid>

如果您 正在使用依赖项注入,在其构造函数中将VM声明为视图的依赖项仍然是完全有效的:

public CatView(CatViewModel vm) : this()
{
     this.DataContext = vm;
}

最后一个选项是创建视图和视图模型的第三方的概念(但是合适)并从两者之外与他们结婚。

无论如何,我确信您应该选择最适合您的一个。有人会过来声称亵渎,但这实际上取决于您的需求和您的项目。

You are confused because all of these options are valid. If you aren't using dependency injection and the ViewModels have all of the information they need on their own, there's no reason not to have the view create the viewmodel (usually done by creating one via a StaticResource for the View:

<Window.Resources>
     <local:CatViewModel x:Key="catVM" />
</Window.Resources>
<Grid DataContext="{StaticResource catVM}">
...
</Grid>

If you are using dependency injection, it's still perfectly valid to declare your VM as a dependency of your view in its constructor:

public CatView(CatViewModel vm) : this()
{
     this.DataContext = vm;
}

The last option is the concept of a third party that creates both the view and the viewmodel (however is appropriate) and marries them from outside of both. Usually this is called a presenter.

In any case, all of these are valid options. You should pick the one that is most appropriate for you. I'm sure someone will come along and claim blasphemy, but it's really up to your needs and your project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文