4

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
      self.month.add_transaction(params)

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?

g_sue
  • 49

1 Answers1

5

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.

Christophe
  • 81,699