MVC教程/的演练,是为人们熟悉的MVVM? [英] MVC tutorial/walkthrough that is meant for people familiar MVVM?

查看:123
本文介绍了MVC教程/的演练,是为人们熟悉的MVVM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经习惯了在WPF的MVVM设计模式的工作,但我最近一直要求做在ASP.Net的东西。我想使用MVC,因为我看到了学习MVVM时,引用了很多尝试,但我不知道任何事情。

I'm used to working in WPF with the MVVM design pattern, but I've recently been asked to do something in ASP.Net. I'd like to try using MVC because I saw it referenced a lot when learning MVVM, however I don't know anything about it.

我可以找到大量的是为了解释MVVM的人谁是熟悉MVC网站上,但我不能找到一个很好的解释了MVC的人谁是用来MVVM。有迹象表明,在它自己的解释MVC网站,但我有一个很难理解他们,因为我的脑海里不停地尝试申请MVVM逻辑。

I can find plenty of sites that are meant to explain MVVM to someone who is familiar with MVC, however I cannot find a good one that explains MVC to someone who is used to MVVM. There are sites that explain MVC on it's own, but I'm having a hard time understanding them because my mind keeps trying to apply MVVM logic.

那么,有没有可以解释MVC而言是用于MVVM有人能理解什么好的网站吗?或者,可以在这里有人向我解释?

So, are there any good sites that can explain MVC in terms that someone used to MVVM can understand? Or can someone explain it to me here?

推荐答案

当你来自MVVM模式和MVC模式(特别是ASP.NET MVC)开始我会建议认为MVC模式的更好的 VMVC,因为M在MVC不是的模型的由意思是在MVVM M。它实际上对应的视图模型的。我不知道这是否再presents MVC的一般定义,但是当你在ASP.NET MVC工作(虽然你看到飘飞的例子或问题在这里所以它是真实,最常用的和最佳实践其中域实体在一个视图中使用(这有时是在提到的问题正是原因))。

When you come from MVVM pattern and start with MVC pattern (especially ASP.NET MVC) I would suggest to think of the "MVC" pattern better as the "VMVC" because the "M" in MVC is not the Model meant by the "M" in MVVM. It actually corresponds to the ViewModel. I don't know if that represents the general definition of MVC but it is true and the most used and best practice when you work with ASP.NET MVC (although you see every now and then examples or questions here on SO where domain entities are used in a view (which is sometimes exactly the reason for the problem described in the question)).

当我从Visual Studio的一个模板创建一个ASP.NET MVC项目,我通常做的第一件事就是到创建的文件夹模型重命名为视图模型。如果你看一看模板code确实与模特你看,他们直接用于看法,他们有数据注解输入验证,显示格式,也许领域命名的观点是什么。这些注释部分地由HTML辅助直接用于生产HTML和不重present域或业务逻辑。换句话说:他们是在你使用的ViewModels在MVVM在WPF / Silverlight中/ Phone7的XAML中的观点相同意义上的剃刀/ HTML视图的ViewModels。

The first thing I usually do when I create a ASP.NET MVC project from one of the Visual Studio templates is to rename the created folder "Model" into "ViewModel". If you take a look what the template code does with those "Models" you see that they are directly used for the views, that they have data annotations for input validation, for display formats and perhaps field naming on the view. These annotations are partially used directly by the HTML helpers to produce the HTML and don't represent domain or business logic. In other words: they are ViewModels for an Razor/HTML view in the same sense as you use ViewModels in MVVM for your XAML views in WPF/Silverlight/Phone7.

域名模式其实并不是MVC模式的一部分,因为它是在MVVM模式的一部分。所以,当你与MVC比较MVVM缩写是有点误导。作为一个非常简单的翻译表可以说:

The domain "Model" is actually not part of the MVC pattern as it is a part in the MVVM pattern. So, the abbreviations are somewhat misleading when you compare MVVM with MVC. As a very simplified "translation table" one could say:

MVVM                         MVC
----                         ---
M  -> Domain Model           not part of the pattern
V  -> View (XAML)            V -> View (HTML, Razor)
VM -> ViewModel              M -> ViewModel
not part of the pattern      C -> Controller

我不知道有关MVVM控制器的相应的事情。在MVC控制器通常是转换域对象进入的ViewModels,然后进入视图(反之亦然)模块 - 示意图:

I'm not sure about the corresponding thing of a controller in MVVM. In MVC the controller is usually the module which translates domain objects into ViewModels and then into views (and vice versa) - schematically:

ControllerActionForGetRequest ( params )
{
     objects = GetDomainObject(params)    - entities, queryables or DTOs
     viewModel = CreateViewModelFromDomainObjects(objects)
     view = CreateViewFromViewModel(viewModel)
}

ControllerActionForPostRequest ( viewModel )
    // ModelBinder makes "viewModel" from HTML input fields, etc
{
     if (IsValid(viewModel))
     {
         data = CreateDomainObjectsOrDtosFromViewModel(viewModel)
         WriteData(data)  - back to data store
         RedirectToActionForGetRequest
     }
     else
         GoBackToView
}

在哪一部分MVVM有这个责任吗?我不确定。我看到那里的视图模型包含一些参考库,翻出模型(域模型)来填充自己的属性,并重新写入通过ICommand的处理程序库的设计。这将意味着在MVVM的ViewModels也有责任要控制,而在MVC中的ViewModels是简单得多:他们都或多或少的唯一属性袋的元数据提供和格式的数据的视图

Which part in MVVM has this responsibility? I am not sure. I have seen designs where the ViewModel holds some reference to a repository, pulls out the Model (Domain Model) to fill its own properties and writes back into the repository through ICommand handlers. That would mean that ViewModels in MVVM also have the responsibility to be a "controller" while ViewModels in MVC are much simpler: They are more or less only property bags with metadata to provide and format data for a view.

最后一点:我个人发现WPF MVVM模式更加难以掌握比MVC模式。 ASP.NET MVC从地面旨在支持MVC模式友好发展并没有需要(甚至不是possibilty)离开这种方式。这不是为WPF的情况。最初的设计与看法和code隐藏文件于心,不与MVVM模式构建的。我经常发现的情况下这是非常困难的绑定视图元素或属性到视图模型,并在code-behing文件处理,这是容易得多,因而违反MVVM原则位。

As a final note: Personally I found the MVVM pattern with WPF much more difficult to master than the MVC pattern. ASP.NET MVC is designed from the ground to support MVC pattern-friendly development and there is not need (or even not the possibilty) to leave this way. This is not the case for WPF. The original design was built with views and code-behind files in mind, not with the MVVM pattern. I found often situations where it was very difficult to bind view elements or attributes to a ViewModel and handling this in code-behing files was much easier, thereby violating MVVM principles a bit.

我认为你不会有任何问题,进入MVC当你有MVVM模式的经验。

I would think that you won't have any problems to get into MVC when you have experience with the MVVM pattern.

这篇关于MVC教程/的演练,是为人们熟悉的MVVM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆