WPF/MVVM - 我们应该为每个 ViewModel 创建一个不同的类吗?
我正在尝试使用 MSDN 中 Todd Miranda 的精彩“How Do I”视频中的 MVVM 示例。
我正在尝试根据我的学习目的调整该示例。
在示例中,他有一个名为EmployeeListViewModel的 ViewModel。现在,如果我想包含部门,我是否应该创建另一个 ViewModel,例如 DepartmentListViewModel?
该示例将EmployeeRepository作为数据源。就我而言,我尝试使用 Entity 对象作为数据源(Model 文件夹中的 Employees.edmx 和EmployeeRepository.cs < em>DataAccess 文件夹)。如果我想显示部门列表,是否应该创建一个名为 DepartmentRepository 的单独类并将所有与部门相关的方法定义放在那里?
如果我想同时检索员工姓名及其部门名称怎么办?我应该在哪里放置此方法?
我对 WPF 和 MVVM 非常陌生,如果以上任何内容需要重新措辞,请告诉我。
感谢您的所有帮助。
I'm attempting the example from the excellent "How Do I" video for MVVM by Todd Miranda found in MSDN.
I'm trying to adapt the example for my learning purpose.
In the example, he has a ViewModel called EmployeeListViewModel. Now if I want to include Departments, should I create another ViewModel such as DepartmentListViewModel?
The example has EmployeeRepository as the Data Source. In my case, I'm trying to use an Entity object as the datasource (Employees.edmx in Model folder and EmployeeRepository.cs in DataAccess folder). If I want to display the list of Departments, should I create a separate class called DepartmentRepository and put all department related method definitions there?
What if I want to retrieve the employee name and their department's name together? Where should I place the methods for this?
I'm very new to WPF and MVVM and please let me know if any of the above needs to be re-phrased.
Thank you for all the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,通常每个视图(页面、窗口、屏幕)都应该有它自己的 ViewModel。因此,如果您想要一个列出一些员工的屏幕,您的 ViewModel 将具有某种员工集合 (IEnumerable) 作为属性。然后,您的员工类型将包含其姓名、部门、电话分机等属性。
从您的问题中我不清楚您是否想在同一页面上显示员工和部门的列表。如果这就是您想要做的,那么您的 ViewModel 中有两个属性,它们是类似这样的集合:
...这将允许您在视图上显示这两个集合。
Yes, generally each View (Page, Window, Screen) should have it's own ViewModel. So, if you want to have a screen that lists some employees, your ViewModel would have some kind of collection of employees (IEnumerable) as a property. Your employee type would then contain properties for their name, department, phone extension (etc).
I'm not exactly clear from your question whether you want to display the list of employees and departments on the same page. If that is what you are trying to do, then you'd have two properties in your ViewModel that are some kind of collections like this:
...which would allow you to display both collections on your view.
这取决于,因为每种模式都更像是想法/概念,而不是您需要严格遵循的东西。通过这样说,您会注意到,有时是的,建议为每个 ViewModel 使用一个类,或者如果适用,可以使用通用 ViewModel。
我知道这很难,因为我曾经(现在仍然)和你处于同样的境地。
在问题 2 中,我有时所做的是 Retrieve 和 IQueryable,然后将返回对象“翻译”为 ViewModel。存储库/域不应该对 ViewModels 一无所知,因为它只是一个演示文稿。
回答第3点,如果您需要将控件与Employee和Departments绑定在一起,
也许你可以做这样的事情:
希望这能澄清你的疑虑。
It depends, as every pattern is more like and idea/concept than something you need to follow strictly. By saying this you will notice that sometimes yes, is advisable to use a class for each ViewModel or maybe use a generic ViewModel if applies.
I know it's hard because I was (and still I am) in the same position as yours.
In question 2, What I do sometimes is to Retrieve and IQueryable and then "translate" the returning object to a ViewModel. Repositories/Domain should not know nothing about ViewModels, since is just a presentation thing.
Answering point 3, if you need to bind a control with Employee and Departments together,
Maybe you can do something like this:
Hope this clarifies your doubts.
这里没有硬性规定。我将分别解决每个问题:
There are no hard and fast rules here. I'll address each concern individually: