14

According to good programming practices, at the beginning of the program runtime, which of the Controller, Model and View components should be created first and which of them should create the other two?

I mean, should the main function first create the controller, then the controller should create both the model and the view and make itself known to them somehow?

Or should I rather begin with creating the view, which, before displaying itself, would initialise the controller, which would create the model?

Or maybe the model should come first? Or they all should be created in the main function in parallel? What's the right way of implementing MVC?

edit: I'm interested in a general answer, though currently I'm working with Java Swing and Windows Phone 7.

1
  • Question has nothing to do with C#. Tag deleted. Commented Dec 4, 2011 at 15:30

2 Answers 2

7

I'd say that Controller and Model objects could be created by main function while View should be created by Controller (possibly based on Model data).

Controller should be created by application main function (working trhead? whatever) whenever new request comes to application. A request may contain some serialized data which could be deserialized by main thread to create and fill new Model object which is in turn passed to Controller for further processing. When controller finishes processing it may (or may not) create a View to pass processing results to client.

Also a Model class could be created by Controller to serve data processing needs (save data to database etc.) or to serve as a base for return View.

Summarizing:

  1. Controller is always created by main function
  2. Model can be created by main function or Controller (maybe also by View? Depends on MVC implementation)
  3. View should be created by Controller (possibly based on data from Model).
Sign up to request clarification or add additional context in comments.

3 Comments

I agree with you. In theory a controller could be used to display different views, where these views would probably implement a common interface or derive from a common base class. In such a case you could also inject the desired view into the controller.
If the controller is responsible for the creation of the view, how are you going to deal with multiple views for the same model/controller combination ? Controller and view should just agree on how data is passed between them on an interface level, but connecting the view and the controller should not necessarily be included in either one of them.
@Robin From my point, View is just a representation for data from Model - it's presented by some file with markup and (almost) no logic. If there could be multiple views per model/controller combination then it's controller's job to determine what view is the most suitable for current request. P.S. I'm mostly using Microsoft's ASP.NET MVC and that's why we may talk a bit different languages.
5

I think each of them can be created individually.

  • If the controller is responsible for the creation of the model, this would mean you can't have a model without having a controller, and that there always is a one-to-one mapping between a model and a controller. For example for a website you could have a controller for the regular data, and one for an xml version of this data (although typically this is the same controller and you just specify the protocol you want to use).
  • If the controller is responsible for the creation of the view, you would end with a one-to-one mapping between controller and view. The controller just handles an incoming request, performs some logic, and provides an answer. It shouldn't care who's asking for the data. For example for a web application you can have an RSS feed and a HTML page, both using the same controller. Another example is your typical Rails application, where one controller maps to multiple views (an index view, a show view, an edit view, ... )

It is however someone's responsibility to tie them all together, but that would be your main application and neither one of those components

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.