在 ASP.NET MVC3 应用程序中,ViewModel 是由服务层还是由控制器填充?
我有一个 C# 和 Razor 中的 ASP.NET MVC3。应用程序的架构分为数据访问层(EF 类 + 存储库)、服务层、控制器、视图模型和视图。
我的 ViewModel 公开了一个方法 Fill
,该方法接受要在 View 中显示的数据集合作为参数。为了不耦合组件之间,必须从服务层或控制器<调用Fill
方法/强>?
I have an ASP.NET MVC3 in C# and Razor. The architecture of the application is divided in Data Access Layer (EF classes + Repository), Service Layer, Controller, ViewModels and View.
My ViewModel exposes a method Fill
which accepts as parameter the data collection to display in the View. In order not to have coupling between the components, the Fill
method has to be called from the Service Layer or from the Controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
定义:模型(如 MVC 中)= 服务层(在本例中)
控制器应该作为视图和模型之间的粘合剂。视图不应该知道模型,反之亦然。
因此,您问题的答案是控制器应该包含用于将信息从模型移动到视图模型的所有逻辑。在视图模型中使用模型中的任何类都是完全可以的。
更新评论的回复
就像我说的。我不知道你的类是什么样子,也不知道你需要做什么来生成导航。
这意味着您可能必须在服务层中创建新的类才能实现这两个目标。
Definition: Model (as in MVC) = Service layer (in this case)
The controller should be conspired as a glue between the view and the model. The view should not be aware of the model and vice versa.
The answer to your question is therefore that the Controller should contain all logic used to move information from the model to the view model. It's perfectly fine to use any class from the Model within the view model.
Update in answer to comments
Like I said. I have no idea how your classes looks like or what you have to do to generate the navigation.
This means that you might have to create new classes in your service layer to be able to achieve those two goals.
最好从 ViewModel 中删除 Fill 函数,这样 ViewModel 就不会依赖于您的数据层。
然后,我会在返回视图之前在控制器中使用 Automapper 之类的东西将 DataObject 映射到 ViewModel。
行动方法:
It would be better to remove the Fill function from your ViewModel then the ViewModel wont depended on your data layer.
I would then use something like Automapper in the controller to map the DataObject To the ViewModel before returning the view.
ActionMethod: