谁拥有 MEF 应用程序中的 ViewModel
我正在设计一个应用程序(Silverllight 或 WPF,仍在争论中),但我陷入了一个设计问题,并且想知道您是否可以提供您的意见......
这是场景。这是一款针对人力资源的员工管理应用程序。一旦应用程序准备就绪,人力资源部可能会要求添加更多功能(这在大多数情况下意味着更多数据)。假设有一个员工表格,将员工记录保存在 数据库。员工表单有一个与之通信的关联 EmployeeViewModel。 EmployeeViewModel 使用实体框架将其数据保存在数据库中。
现在,如果人力资源部想要添加一组新字段(例如“配偶信息”),我计划提供一个插件,人力资源部可以在可用时将其简单地添加到表单中。这个插件 SpouseInfo 是一种具有自己的视图模型的用户控件。
问题是谁拥有这里的 ViewModel。
一个。我是否让 SpouseInfo 完全独立。每当保存或加载 Employee 表单时,SpouseInfo 都会调用其视图模型并加载或保存数据。在这种情况下,在保存或加载数据之前,需要先从父表单获取 EmployeeId,然后才能保存数据(这意味着,让 Employee 表单先保存数据,然后将 Employee Id 传递给 SpouseInfo)
或者
b 。 Employee 表单拥有 SpouseInfo 的 ViewModel。作为“父实体”,它负责加载和保存“子实体”,即 SpouseInfo viewModel。
谢谢, 一个
I am designing an application (Silverllight or WPF, still debating) but I am stuck at a design issue and was if wondering you could provide your opinion on it....
Here's the scenario. This is an Employee management application for HR. Once the application is ready, HR may ask for more features to be added(which most of the time means, more data). Say there is an employee form that saves employee record in the
database.The employee form has an associated EmployeeViewModel that it communicates with. The EmployeeViewModel uses Entity framework to persist its data in the database.
Now if the HR wants to add a new set of fields say "Spouse Information", I plan to provide a plug-in that HR can simply add to the form when available. This plug-in, SpouseInfo is sort of a usercontrol with a view model of its own.
The problem is who owns the ViewModel here.
a. Do I make SpouseInfo completely independant. Whenever Employee form is Saved or loaded, SpouseInfo calls its view model and loads or saves the data. In this case before it can save or load the data, It would need the EmployeeId from the Parent form before the data can be saved (which means, let the Employee form save the data first and then pass the Employee Id to SpouseInfo)
Or
b. The Employee form owns the ViewModel for SpouseInfo. That being the "parent entity" it is responsible for loading and saving the "Child Entity" i.e. SpouseInfo viewModel.
Thanks,
A
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这确实是一个设计选择。您使用 MEF 的事实并不意味着您不必决定什么“所有权”对您的应用程序最有意义,以及哪些组件应该对其他组件负责。
话虽这么说,我可能会让“SpouseInfo”信息符合已知接口,并从主员工的 View/ViewModel 使用该接口(通过 MEF 加载)。这将允许在运行时加载作为员工依赖项的任何注入控件,例如
如果您通过 MEF 导入该控件,则可以轻松处理布局,并使用正确的所需信息(可能是 EmployeeId)直接调用 VM保留“子”ViewModel。
This is really a design choice. The fact that you are using MEF doesn't mean you don't have to decide what "ownership" makes the most sense for your application, and which components should be responsible for others.
That being said, I would probably make the "SpouseInfo" information conform to a known interface, and use that interface (loaded via MEF) from the main Employee's View/ViewModel. This would allow any injected control that was an employee dependency to be loaded at runtime, something like
If you imported that via MEF, you could handle layout easily enough, and call into the VM directly with the correct required information (EmployeeId, probably) to persist the "sub" ViewModel.