WPF/MVVM - 我们应该为每个 ViewModel 创建一个不同的类吗?

发布于 2024-09-05 07:34:29 字数 621 浏览 3 评论 0原文

我正在尝试使用 MSDN 中 Todd Miranda 的精彩“How Do I”视频中的 MVVM 示例。

我正在尝试根据我的学习目的调整该示例。

  1. 在示例中,他有一个名为EmployeeListViewModel的 ViewModel。现在,如果我想包含部门,我是否应该创建另一个 ViewModel,例如 DepartmentListViewModel

  2. 该示例将EmployeeRepository作为数据源。就我而言,我尝试使用 Entity 对象作为数据源(Model 文件夹中的 Employees.edmxEmployeeRepository.cs < em>DataAccess 文件夹)。如果我想显示部门列表,是否应该创建一个名为 DepartmentRepository 的单独类并将所有与部门相关的方法定义放在那里?

  3. 如果我想同时检索员工姓名及其部门名称怎么办?我应该在哪里放置此方法?

我对 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.

  1. In the example, he has a ViewModel called EmployeeListViewModel. Now if I want to include Departments, should I create another ViewModel such as DepartmentListViewModel?

  2. 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?

  3. 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 技术交流群。

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

发布评论

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

评论(3

独自←快乐 2024-09-12 07:34:29

是的,通常每个视图(页面、窗口、屏幕)都应该有它自己的 ViewModel。因此,如果您想要一个列出一些员工的屏幕,您的 ViewModel 将具有某种员工集合 (IEnumerable) 作为属性。然后,您的员工类型将包含其姓名、部门、​​电话分机等属性。

从您的问题中我不清楚您是否想在同一页面上显示员工和部门的列表。如果这就是您想要做的,那么您的 ViewModel 中有两个属性,它们是类似这样的集合:

public class EmployeeListViewModel {
     public IEnumerable<Employee> Employees { get; set; }
     public IEnumerable<Department> Departments { get; set; }
}

...这将允许您在视图上显示这两个集合。

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:

public class EmployeeListViewModel {
     public IEnumerable<Employee> Employees { get; set; }
     public IEnumerable<Department> Departments { get; set; }
}

...which would allow you to display both collections on your view.

喜爱皱眉﹌ 2024-09-12 07:34:29

这取决于,因为每种模式都更像是想法/概念,而不是您需要严格遵循的东西。通过这样说,您会注意到,有时是的,建议为每个 ViewModel 使用一个类,或者如果适用,可以使用通用 ViewModel。
我知道这很难,因为我曾经(现在仍然)和你处于同样的境地。

在问题 2 中,我有时所做的是 Retrieve 和 IQueryable,然后将返回对象“翻译”为 ViewModel。存储库/域不应该对 ViewModels 一无所知,因为它只是一个演示文稿。

回答第3点,如果您需要将控件与Employee和Departments绑定在一起,
也许你可以做这样的事情:

public class EmployeeDepartmentsViewModel : BaseViewModel //Base View Model has INPC stuff
{
    public Employee Employee { get; set; }
    public Department Department { get; set; }
}

希望这能澄清你的疑虑。

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:

public class EmployeeDepartmentsViewModel : BaseViewModel //Base View Model has INPC stuff
{
    public Employee Employee { get; set; }
    public Department Department { get; set; }
}

Hope this clarifies your doubts.

在梵高的星空下 2024-09-12 07:34:29

这里没有硬性规定。我将分别解决每个问题:

  1. 您拥有的每个独特视图都会有一个 ViewModel。我认为将其称为 [Entity]ListViewModel 可能会混淆问题。将其命名为视图的名称...如果您正在查看 DepartmentDashboard,请将其命名为 DepartmentDashboardViewModel。
  2. 对于您的模型,没有任何规则,并且视图模型与视图之间的模型对象比例当然不必是 1:1:1。一般来说,在直接处理数据库(如 TheBigDatabase.edmx)时,我有一个 OR/M 设置,而这恰好由许多模型(员工、部门、帐户、交易等)组成。在这里做任何感觉好的事情。
  3. 由于您将创建一个包含多个实体的 edmx(正如我在 #2 中建议的那样),因此您将能够利用这些实体之间的关系(我猜员工有部门,所以 Employee.Department 将是那里)。获得此信息后,如果您愿意,可以将相关数据全部显示在一个位置。

There are no hard and fast rules here. I'll address each concern individually:

  1. You would have one ViewModel for each unique View you had. I think that calling it [Entity]ListViewModel might have confused the issue. Name it the name of the view... if you are looking at the DepartmentDashboard, call it the DepartmentDashboardViewModel.
  2. For your model, there are no rules and there certainly doesn't have to be a 1:1:1 ratio of model objects to view models to views. Generally I have one OR/M setup when dealing directly with a database (like TheBigDatabase.edmx) and this happens to be made up of many models (Employees, Departments, Accounts, Transactions, etc). Do whatever feels good here.
  3. Since you are going to create an edmx with multiple entities (as I suggested in #2) in it, you will be able to take advantage of the relationships between these entities (Employees have Departments, I would guess, so Employee.Department would be there). Once you have this, you can display the related data all in one place if you wish.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文