使用ViewModel和对System.Web.Mvc的引用是否违反了MVC模式?

时间:2021-03-24 11:24:25

I've seen it all over SO, blogs, and books, where the authors tell you add ViewModels specific to your View in your Model projects as wrappers for your underlying model objects. The idea is to make it very simple and targeted when you go to do model binding to the View. Here is a good example: Rendering and Binding Drop Down Lists using ASP.NET MVC 2 EditorFor

我已经在SO,博客和书籍上看到了这一点,作者告诉您在模型项目中将ViewModel特定于View作为底层模型对象的包装。我们的想法是,当您对View进行模型绑定时,使其变得非常简单和有针对性。这是一个很好的例子:使用ASP.NET MVC 2 EditorFor渲染和绑定下拉列表

However, it irks me a little that there are references now to System.Web.Mvc in my model, which otherwise could have been used for multiple outlets (maybe WCF API, Silverlight, etc), but now I have specific references to MVC dll's that will be required to get my model project to build.

但是,我觉得我的模型中现在有一些对System.Web.Mvc的引用让我感到烦恼,否则本来可以用于多个出口(可能是WCF API,Silverlight等),但是现在我对MVC dll有特定的引用。这将需要我的模型项目来构建。

My question is: does this violate MVC patterns when we start adding IEnumerable<SelectListItem> to our model classes? And is there a viable alternative layer to move this to and how, ie Controller?

我的问题是:当我们开始将IEnumerable 添加到我们的模型类时,这是否违反了MVC模式?是否有一个可行的替代层来移动它和如何,即控制器?

Any thoughts or comments appreciated.

任何想法或评论赞赏。

3 个解决方案

#1


6  

I personally only create the select list on the fly in the view, from a more re-usable IEnumerable list in my model, which means my model doesn't have anything related to SelectLists, SelectListItems or anything MVC specific.

我个人只在视图中动态创建选择列表,从我模型中更易于使用的IEnumerable列表中创建,这意味着我的模型没有与SelectLists,SelectListItems或任何MVC特定相关的任何内容。

Example as promised - create the SelectList in the view, using all the normal view engine bits...

承诺的示例 - 使用所有普通视图引擎位在视图中创建SelectList ...

<%= Html.ListBox("SelectedStuff", 
        new SelectList(Model.SomeOptions, "id", "name", Model.SelectedStuff)) %>

#2


3  

does this violate MVC patterns when we start adding IEnumerable to our model classes?

当我们开始将IEnumerable添加到我们的模型类时,这会违反MVC模式吗?

Not really BUT if your trying to use a Domain Driven Design or separation of concerns between you business layer and MVC/presentation layer than it is a violation.

如果您尝试使用域驱动设计或业务层与MVC /表示层之间的关注点分离而不是违规,那不是真的。

  • Models are your entities, your domain, your business layer objects.

    模型是您的实体,您的域,您的业务层对象。

  • ViewModels are your screens, form postings, display data buckets.

    ViewModels是您的屏幕,表单发布,显示数据桶。

Models map to ViewModels which can contain MVC dependent items. Think if it this way, ViewModels are directly for MVC. Models could be for a service, winform, WPF or any programmatic presentation of the business sytem system.

模型映射到ViewModels,它可以包含MVC依赖项。想想如果这样,ViewModels直接用于MVC。模型可以用于服务,winform,WPF或业务系统系统的任何程序化表示。

#3


1  

No, ViewModels are meant to be consumed by the View and should be located in your web project. However, your actual Model should have no reference to MVC or your web project. Think of your ViewModel as bridging that web gap from your Model to your View.

不,ViewModel应该由View使用,并且应该位于您的Web项目中。但是,您的实际模型应该没有引用MVC或您的Web项目。将您的ViewModel视为桥接从您的模型到视图的Web间隙。

#1


6  

I personally only create the select list on the fly in the view, from a more re-usable IEnumerable list in my model, which means my model doesn't have anything related to SelectLists, SelectListItems or anything MVC specific.

我个人只在视图中动态创建选择列表,从我模型中更易于使用的IEnumerable列表中创建,这意味着我的模型没有与SelectLists,SelectListItems或任何MVC特定相关的任何内容。

Example as promised - create the SelectList in the view, using all the normal view engine bits...

承诺的示例 - 使用所有普通视图引擎位在视图中创建SelectList ...

<%= Html.ListBox("SelectedStuff", 
        new SelectList(Model.SomeOptions, "id", "name", Model.SelectedStuff)) %>

#2


3  

does this violate MVC patterns when we start adding IEnumerable to our model classes?

当我们开始将IEnumerable添加到我们的模型类时,这会违反MVC模式吗?

Not really BUT if your trying to use a Domain Driven Design or separation of concerns between you business layer and MVC/presentation layer than it is a violation.

如果您尝试使用域驱动设计或业务层与MVC /表示层之间的关注点分离而不是违规,那不是真的。

  • Models are your entities, your domain, your business layer objects.

    模型是您的实体,您的域,您的业务层对象。

  • ViewModels are your screens, form postings, display data buckets.

    ViewModels是您的屏幕,表单发布,显示数据桶。

Models map to ViewModels which can contain MVC dependent items. Think if it this way, ViewModels are directly for MVC. Models could be for a service, winform, WPF or any programmatic presentation of the business sytem system.

模型映射到ViewModels,它可以包含MVC依赖项。想想如果这样,ViewModels直接用于MVC。模型可以用于服务,winform,WPF或业务系统系统的任何程序化表示。

#3


1  

No, ViewModels are meant to be consumed by the View and should be located in your web project. However, your actual Model should have no reference to MVC or your web project. Think of your ViewModel as bridging that web gap from your Model to your View.

不,ViewModel应该由View使用,并且应该位于您的Web项目中。但是,您的实际模型应该没有引用MVC或您的Web项目。将您的ViewModel视为桥接从您的模型到视图的Web间隙。