I'm creating a web applicati开发者_StackOverflow中文版on using the mcv pattern. I would like to know, from your experience and from studies in the field, from which component is believed is better to start with, the model the controller or the view?
Starting with the View may motivate you more, and is good because it helps put yourself in the mindframe of the user (always a good thing).
It will also help you realize what you really do and don't need for the Model. Simplicity of Model is quite useful, and working with the View helps you realize that. It will also help you focus on the Model-View interface, which is very important and the primary means of interactions between the two.
That said, I actually often start on the Model, because I can already envision the ideal View, and it is very easy to overdesign a View.
Iterating back and forth between Model and View works well, so as to not over-engineer them.
The concept of a Controller is overhyped; a Controller is the necessary code to make interactions between View and Model as elegant and clean as possible. It's thus something I would inadvertently make while refactoring, and would not start with (or only add to as necessary, keeping it very lean and simple).
Keep in mind that if you do your views first, you won't be able to benefit from any of the scaffolding that the MVC Tooling could do for you if you're Models were already created. This alone should motivate you to do some sort of rudimentary Model first.
I usually opt for Models first, and I find that I switch back and forth from there. The whole thing is much more iterative rather than hammer out the models to be "perfect" on the first try. But the bulk is done up front and I later go back to tweak or modify as needed when additional things are needed by the view/controller along the way. As with anything though, this can lead to some scope creep so keep a firm view of what the overall goal is and stick to that and get it done first. Anything you come across that might be a good feature to add that was not in the initial scope, write it down in detail if you can. Then later on you can go through all of the things that you wrote down and use them for the next version.
From my experience it is best to start with the model.
I would say start with the model first because the model pretty much defines what is going to be in your view. Then create controllers and create your views from the controllers.
I suppose you could start with either the model or controller.
I mostly start my projects thinking about what elements I would need.
Once I know what I want / need I'll start with the design (the static template(s)).
Once I have a static template I'll mostly know what code I'll need.
After that I'll start with the basic Controller and expand it to my needs. When I'm working on the Controller and I find something which should be placed in the Model layer I start programming the Model layer.
I think it all depends, but this works for me most of the time.
As long as you keep it all separated it's all fine I guess.
Just my two cents
精彩评论