2

In various articles on the web about ASP.NET MVC I've people mention that Microsoft's implementation of MVC is somehow different form the 'classic' MVC pattern.

But I've never seen anyone go into any detail on this, and at first glance I don't see how ASP.NET MVC is any different from what is described, say, here.

So my question is: does ASP.NET MVC really violate any MVC pattern principles?

Community
  • 1
  • 1
Andre Borges
  • 1,360
  • 14
  • 37
  • 1
    Can you describe what parts of ASP.NET MVC you think not match the MVC design principle? Otherwise I'm afraid this is becoming quite opinion-based. Or turn the question around, like _"Does ASP.NET MVC implement the core principles of the Model-View-Controller design pattern?"_. Or even more globally applicable: _"Is it possible and feasible for an HTTP application to properly implement principles X, Y and Z from the MVC design pattern?"_. – CodeCaster Feb 16 '16 at 15:26
  • @CodeCaster, my question states that I don't think that any parts of ASP.NET MVC violate the MVC design principle. My intention was to find out which parts other people think violate this principle. – Andre Borges Feb 18 '16 at 10:00
  • _"other people think"_ - yeah, so that makes it opinion-based. – CodeCaster Feb 18 '16 at 10:05
  • Probably, but I find many of SO questions to be somewhat opinion-based but nevertheless useful: "_What are best practices for X?_", "_Proper way to handle errors using technology Y_", etc. – Andre Borges Feb 18 '16 at 10:19
  • Those questions aside, I gave a few hints for wording your question differently so you may get more answers, or answers that better answer your actual question and not get your question closed, but whatever floats your boat. – CodeCaster Feb 18 '16 at 10:57
  • "parts of ASP.NET MVC violate the MVC design principle". You mean, what you create may violate that. The framework itself is not MVC - remember that – T.S. Feb 18 '16 at 15:37

3 Answers3

6

The problem is really that MVC is a loosely defined pattern. The "M" could be interpreted as "everything in the business domain, from entities to business logic".

I wouldn't say that ASP.NET violate MVC, but rather interprets it loosely.

Some official MVC examples uses entity framework directly in the controllers which in my opinion is incorrect. Because then it doesn't act as a bridge between the model and the view. Instead it takes care of business logic (even if it's very basic logic) AND map the model info to the view.

ASP.NET MVC has something called view models which are not the M in MVC. They are instead used as an adapter between the model (or rather one of more entities in the Model) and the view, thus taking over a part of the responsibility that the controller has.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • 1
    I always interpreted "M" to be the view models since MVC is a UI technology. The business logic can be in a separate layer, web services, or even in the database. – Ryan Feb 16 '16 at 15:34
  • 1
    @Ryan: From wikipedia: `The central component of MVC, the model, captures the behavior of the application in terms of its problem domain, independent of the user interface` – jgauffin Feb 16 '16 at 16:13
  • 1
    "entity framework directly in the controllers which in my opinion is incorrect" - Exactly! where is domain? And what if I don't want to use EF tomorrow? So, we repeat same mistake. We have controller, instead being a mediator or bridge, now it is BLL and DA – T.S. Feb 17 '16 at 14:56
  • hmm... But if we don't use view models in MVC, how does the controller adapt the problem domain object to fit the view? – Andre Borges Feb 18 '16 at 10:08
1

Generally yes, Microsofts implementation of the MVC pattern follows the main tenant of the MVC pattern. It has a Model, Controller and View that are responsible for slightly different aspects of the UI (remember MVC is a presentation pattern in this instance so it's only responsible for the UI (mostly))

Where Microsoft start to deviate is the introduction of some of the less clean (as in not 100% MVC) features, specifically

Some of the above are more contentious than others. Ultimately all these things are designed to speed up development, but move away from the idea that the controller builds a model that the view then renders (full stop).

Microsoft also has a tendency to explain MVC as a business logic pattern (as mentioned above it's a UI pattern).

So in the Microsoft world you often see example where the controller gets a model from (for example) Entity Framework does some business logic (business logic shouldn't really be in the controller) then passes it into a ViewModel (not really 100% MVC) and the view then renders it (manipulating the data using helper functions).

So they try and sell MVC as a one pattern fits all solution when it's not.

Community
  • 1
  • 1
Liam
  • 27,717
  • 28
  • 128
  • 190
1

Although, I agree with other answers [in some respect], let me put it into a different perspective.

ASP.NET MVC is not a pattern but rather a framework in which you can write your software using MVC pattern, or rather its specific web-application version.

You shouldn't worry about Microsoft's implementation. Their implementation of MVC Framework has little to do with MVC pattern. To implement it, MS use many different patterns, I am sure. The key here, is to understand that MVC framework is not MVC. It allows you to develop using MVC pattern. And in this case, this pattern includes certain rules that you need to follow, like in other patterns - name controllers with Controller at the end, for example. But this is MS's implementation for you. So, you have View, Model and Controller. MVC Framework tells you, follow the rules and we will connect all these for you.

Now, if you go to MVC pattern as original pattern, all it tells you - separate view from controller and model. And separate model from controller. Because this way, you can reuse model and controller with different view, for example. Now, how you implement it - it is up to you. As long as it has characteristics I described above, this is MVC. So, again, MS gave you MVC framework, in which you have separate M-V-C. Therefore, when you write your M,V and C, you have separation which is original pattern describes. MVC framework takes care of the wiring for you.

T.S.
  • 18,195
  • 11
  • 58
  • 78