2

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.

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.

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • 1
    Why not skip the explanation and just switch to using MVVM in your web development: http://knockoutjs.com/ – Steve Greatrex Oct 28 '11 at 16:08
  • 1
    @SteveGreatrex I considered that, however I read somewhere that MVVM is mostly meant for WPF/Silverlight development due to the way the binding system works, and is not optimized for use with ASP.Net – Rachel Oct 28 '11 at 16:15
  • According to our [on-topic](https://stackoverflow.com/help/on-topic) guidance, "**Some questions are still off-topic, even if they fit into one of the categories listed above:**...Questions asking us to *recommend or find a book, tool, software library, tutorial or other off-site resource* are off-topic..." – Robert Columbia Feb 28 '18 at 02:04

2 Answers2

5

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)).

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.

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

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
}

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.

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.

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

Slauma
  • 175,098
  • 59
  • 401
  • 420
  • Good explanation, especially pointing out the differences between MVC Models and MVVM Models. It took me a bit to figure out that because I was expecting the two M's to be the same. I have started learning MVC, and I realize now that MVC's `M`+`C` is equal to MVVM's `VM`, and MVC's `M` contains a mix of both MVVM's `M` and `VM` pieces. – Rachel Nov 04 '11 at 18:08
  • @Rachel: Ah, good that you confirm that `VM` = `M`+`C`. I wasn't sure about that. For the second part: I'd probably leave out the `M` of MVVM (domain model) completely out of the ViewModel in MVC. For example the `StaffViewModel` in this post (http://stackoverflow.com/q/8010566/270591, doesn't it look very MVVM-like?) is an antipattern because 1) there is no PropertyChanged event on a web page which would help to update a model and 2) for security reasons: Exposing more properties to the model binder than a user should edit on a page is a potential risk (manipulating HTTP post requests). – Slauma Nov 04 '11 at 19:00
  • The way I am understanding it so far is MVC's `M` contains properties that you'd usually find in MVVM's `VM`, but with all the functionality removed (so it acts like MVVM's `M`). The `C` is the functionality that you'd find in MVVM's `VM`, such as Data Access, advanced business rule validation, etc. – Rachel Nov 04 '11 at 19:13
2

Rachel, I have not run into any ASP.NET MVC sites that are geared towards the MVVM crowd, but from first hand experience I thought the ASP.NET MVC Music Store was absolutely fantastic.

I am originally a WebForms developer, and I can absolutely attest to having a certain technology in the back of your mind while learning another, forcing your logic to shift a certain way. That was especially difficult going from Webforms -> MVC. The best advice I have is to just analyze every aspect of that Music Store tutorial as a separate entity.

Good luck, and I hope that helps.

  • I actually downloaded the code for that earlier and get errors when trying to build it. I'm not a strong web developer, so I didn't spend a lot of time trying to fix it, but I can take another look at it. – Rachel Oct 28 '11 at 16:14
  • @Rachel I actually found it easier just to write the code as you progress through the tutorial. I think it helps learning more when you're writing the code, as oppsed to just reading some source code already written. Let me know the errors you got, maybe I can help out. –  Oct 28 '11 at 16:15
  • +1 for the great MVC tutorial. I agree that you do learn more writing the code than just reading it, but I also like viewing the finished product first to see what I'm working towards. The code in your link runs without any errors, so I must have downloaded an older version of the code before. – Rachel Oct 31 '11 at 18:18