views:

399

answers:

8

I was originally going to make this a longer question, but I feel like the shorter I make it, the better you'll understand what I mean.

  • The MVC architectural pattern has 3 dependencies. The View depends on the model. The Controller depends on the View and Model. The Model is independent.

  • The Layers architectural pattern defines N - 1 dependencies, where N is the number of Layers.

Given three Layers: Model, View, and Controller, there are only 2 dependencies, as opposed to 3 with traditional MVC. The structure looks like this:

View ---> Controller ---> Model

[View depends on Controller, Controller depends on Model]

It seems to me that this style accomplishes the same goals and produces looser coupling. Why isn't this style more common? Does it truly accomplish the same goals?

Edit: Not ASP.NET MVC, just the pattern.

With regard to griegs's post:

  • As far as mocking, Layers still allows you to use the Command Processor pattern to simulate button clicks, as well as any other range of events.
  • UI changes are still very easy, perhaps even easier. In MVC, the Controller and View tend to mesh together. Layers creates a strict separation. Both Layers are black boxes, free to vary independently in implementation.
  • The Controller has 0 dependencies on the View. The View can be written, and time can still be saved with loose coupling.
+10  A: 

Because you decouple the interface from the controller making changes easier.

Also consider the scenario where you need to get started on a project but the artwork won't be ready for weeks or months. Do you wait or do you write all the code required for the pages and simply then wire up the view to the controller.

At least that's what we did and we saved months.

Also it made UI changes easier to cope with because there wasn't any code in our aspx pages that did anything.

Our tests were also better as we could mock up anything including button clicks etc.

And if you're talking about the asp.net-mvc framework, there is no code in the aspx files and no viewstate etc.

griegs
You are assuming he is referring to ASP.NET MVC, not the MVC pattern, which is what he asked about.
Andy_Vulhop
No i'm not. Only in my last sentance am I doing that and I even say "If you're talking about..."
griegs
The view and the controller and inherently coupled in MVC anyway. Since both are modeled as black boxes, each can be mocked and/or modified. I don't feel you pointed out a difference, so much as similarities.
Mike
Edited my question with a few points, directed at this post.
Mike
+3  A: 

In proper MVC the controller doesn't depend on the view afaik. Or maybe I'm not understanding it correctly.

The model defines the data.

The view defines what the output looks like.

And the controller is a translator from a model-understood grammar to view-understood grammar.

So essentially the controller is independent. The view is independent. And the model is independent.

Yes? No?

Swizec Teller
This was my impression:http://prajwal-tuladhar.net.np/wp-content/uploads/2008/10/mvc-architecture.png
Mike
Each is (more or less) independent, but you have the role of the controller wrong. The controller basically takes user input, and modifies the model or view accordingly (though some implementations bypass the controller for actions that only modify the view).
Jerry Coffin
A: 

In my opinion ,you'd better try it in your programme , you can use ruby on rails ,or codeigniter( for php ),these great framework may be helpful to your understanding the MVC.

keepyouliking
@keepyouliking : CakePHP is also nice MVC framework for PHP. With Perl, you may try Catalyst. .NET and Java, well, I guess everybody already knew the big names :)
Michael Mao
+1  A: 

I'll be bold, and try to explain why your method didn't catch on.

The MVC pattern basically requires the view and model layers to agree on an API. Since one serves the other and there are no dependencies inside the code it leaves the controller to behave generically, all it needs to do is take a certain structure in the view layer and call the matching API on the model layer.

You'll note that agreeing on an API between the view and model isn't really such a big deal it has to happen anyway. And what you get is good separation between back-end front-end development.

In your proposed solution a lot of development is required on the controller side. The controller will be required to understand all the elements in the view and to map them to the specific calls required on the model layer. Since the controller is a single access point connecting many views to many models this can quickly get out of hand and end up being an incomprehensible controller module.

Look at some Struts2 examples to see what I mean...

Asaf
The Controller layer requires absolutely no dependencies to the View layer. In fact, the pattern restricts it. Also, MVC states that there is one Controller per View, with multiple Views, and one Model. So that is taken care of too.
Mike
So if I submit a form on the web page (view) how is the appropriate business logic applied (model)?If your view and model are truly independent the controller must have a definition of:( input1 --> call methods 1,2,3 )( input2 --> call methods 2,3,5 )...I believe this is what most implementations of the pattern are trying to avoid
Asaf
If methods 1, 2, 3 are Model methods, ironically, yes, I am trying to achieve this. It makes a lot of sense. Smells even like an easy clean up for the Command pattern.
Mike
Asaf
Nope. You're getting it exactly as I imagined. In this version, View has 0 dependencies on the Model. It then becomes Layered, not MVC.
Mike
+1  A: 

I think I'm understanding your point:

Yes you can make the View only depend on the Controller only by making the Controller transform (using PHP as an example) the Model objects to non-Model objects like simple arrays.

As we already know, performing this transformation can be more effort than it's worth if the decoupling isn't actually needed. If the View uses the Model objects then it has this dependency. However, this can be relieved a bit by having the View depend solely on the Controller for its required input, which can be Model objects.

The Symfony PHP framework promotes this style of skinny controller shuffling between Model and View. You can still directly call upon the Model layer to retrieve objects within the View layer but it's strongly urged against for the coupling issues you bring up. Within the View you can call include_component() which actually goes back up to the Controller if you need to query the Model.

Rob Olmos
Yep, you've got it spot on, @Rob Olmos. So it is used sometimes. I'm just surprised it isn't used more, especially given that for a while no one really understood what I was talking about.
Mike
Even in my org we're still debating whether to force the Controller to pass only non-Model variables to the View.. No decision yet but we're trialling it for feasibility...
Rob Olmos
A: 

Choosing a presentation pattern for a new or enterprise web development on the Microsoft platform is a daunting task, in my opinion there are only three; View Model, Model-View-Presenter (MVP) or ASP.NET MVC (a Model2 derivative).

You can read the full article here ASP.NET MVC Patterns

MetalLemon