For context, this project is a personal budget program I am working on while I try to learn to use MVC patterns effectively.

There is a SessionController that passes user commands to a MonthController. Each controller has a Parser that maps commands to controller methods.

Currently, the controller has a Month object as an attribute, and directly calls the model methods. It looks like this:

class MonthController(Controller):
    def __init__(self, budget, *args, **kwargs):
      Controller.__init__(self, *args, **kwargs) 
      self.month  = budget.get_current_month()

    def add_transaction(self, params):
      # get user data, do parameter validation here

Is this normal implementation of MVC? Are there pitfalls to letting the Control "own" a Model object I should look out for?

Also, I plan to have the SessionController "own" a number of Views. One of those views will also have a Month attribute (but only call read-only methods). I can't pin down why, but this feels wrong. Am I off track, or is this standard MVC?


Yes, this is quite common (python example, simple java example, java example using a list of models, and C++ example)

Why ? The controller must send commands to the model. So either it knows the model (i.e. it was injected at construction and stored for later reference) or there's a way to find the model on the fly. The later might however make the controller less flexible (for example, using a global reference or a singleton to find the model would mean that all controllers use the same model, unless additional complexity could circumvent this limitation).

A variant that you could consider is to use the command pattern. This could allow to enhance the design with the possibility for undo/redo.

  • I would add that the controller should not own the view. It should use some kind of render('template_file.html', obj_to_render) function which returns HTML to the controller, which the controller sends back in the response.
    – jpotts18
    Nov 23 '17 at 4:49
  • "or there's a way to find the model on the fly. The later would however make the controller less flexible" Why do yo say that? A model factory or some thing could be injected into the controller so it can ask that thing to find the right model.
    – Andy
    Aug 4 '18 at 16:20
  • @Andy the reasons are explained btw the brackets: if controller finds the model on the fly (global var or singleton) then all controllers would share the same model whereas Injecting the model at construction would allow to have an multi-model application. Of course, you could also inject an indirect info into the controller and ask on the fly for the model that matches this information. In this case you would indeed have the same flexibility: You are right; But conceptually speaking, that information would be a kind of reference to the model (i.e. via an additional level of indirection).
    – Christophe
    Aug 4 '18 at 18:38
  • Your premise that the controller must get the model via global variable or singleton is flawed. And a reference to a factory isn't the same as a reference to a model.
    – Andy
    Aug 4 '18 at 19:20
  • @Andy I'm not sure to understand how a factory (which creates individual objects) may address all the controller-model interaction needs. Could you explain your alternative with more details in a separate answer ?
    – Christophe
    Aug 4 '18 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.