point of the Controller in MVC?

In the MVC pattern, what’s the point of the Controller?

I found this way of thinking about MVC:

Input --> Processing --> Output
Controller --> Model --> View

…but why shouldn’t one thing take care of input and output,
since both are handled by GUI code (e.g. .SetValue and
.GetValue). To add to my confusion, in the wxPython

doc about MVC, often the two words are combined into
one, as in View/Controller or ViewController.

One source I found said that for desktop GUI apps,
the distinction between View and Controller is not
important, but that it is important for web apps.

A good answer about the point of the Controller
would take the form: “Unless you have a Controller,
you will not be able to do x.” (or it will harder to do x).

I am trying to do something like MVC now, and also
trying to create “ravioli code”, and wondering if it is
worth my time to create a separate Controller. To
start, I am just trying to separate any GUI aspect
(display and user selection) from logic like database
queries and other processes. I will have some
wxTimers involved, too, and realize these can only
run in the main loop, so I’ll need to think about how
that factors in here as well.

Any ideas about this are appreciated.

Che

Hi Che,

C M wrote:

In the MVC pattern, what's the point of the Controller?

I don't have an answer, but maybe you find it on this page.

http://wiki.wxpython.org/wxPython%20Patterns

...

I am trying to do something like MVC now, and also
trying to create "ravioli code",

It is pasta too, but I think you meant "Spaghetti" :wink:

Werner

Hi Che and Werner,

Although I am only an amateur and consequently an eternel newbie,
I'll try explain what I understood from the MVP, which is quite
similar to MVC.

Python and wxPython Gurus, please correct me if I am misunderstanding
something.

In the MVP, you have several components: the model, the view and the
interactor, and the Presenter.

According to ModelViewPresenter - wxPyWiki, "the
difference between MVC and MVP is that in MVP the presenter does not
handles the GUI events directly as the controller does in MVC but
through delegation via the interactor".
Thus, in the MVC, the bindings are done in the Controller while they
are kept seperated in the interactor in the MVP.

The model:
- manages all data coming in / from the database,
- defines methods to get data from the db (queries,...)

The View:
- is a gui with a frame, panels, buttons, fields,...
- is modified through:
    - the presenter which has methods calling, for instance,
        view.TextCtrl_1.ChangeValue()
    - the interactor which binds all widgets of the view to methods
that
        send back to methods defined in the presenter

The Interactor:
- binds all the View widgets to methods
- these methods call Presenter's methods that update the view

The Presenter:
- takes care of the logic of the application
- has methods that :
    - update the View (send data to the View)
    - get data from the View through the Interactor
    - get data from the model
    - send data to the model
Everything goes through the Presenter

For instance, if a button is pressed:
- the button is defined in the View
- the button is bound in the Interactor, with a method that calls
    a Presenter's method such as:
        def OnButtonPressed(self, evt):
            # This is in the Interactor
            self.presenter.UpdateView()
- the Presenter's method is called:
        def UpdateView(self):
            # This is in the Presenter
            self.view.SetTextCtrl_1(value= self.view.GetTextCtrl_2())
            self.model.UpdateDb()
    which:
    - updates the View (the value of the TC_1 is set to the value of
the TC_2)
    - calls the updateDb method of the Model class, which updates the
db, doing
        this or that

As a consequence, the Presenter manages the logic of the app, since
when the user acts on the gui elements, it goes to the interactor and
then to the Presenter where the action in response to the user actions
is defined:
gui action --> interactor (bindings) --> Calls Presenter's method -->
action managed by the Presenter (on the View, database,...).

To my understanding, the Presenter (or Controller) makes it easier to
modify the gui without putting the mess in the app.
You just modify your gui and the bindings in the interactor.
Since the logic of the app is in the presenter, you keep it clean.

To me, if the app becomes a little bit complex, it is certainly
worthwhile to use the MVP or MVC.

In the MVC pattern, what's the point of the Controller?

I found this way of thinking about MVC:

Input --> Processing --> Output
Controller --> Model --> View

...but why shouldn't one thing take care of input and output,
since both are handled by GUI code (e.g. .SetValue and
.GetValue). To add to my confusion, in the wxPython
doc about MVC, often the two words are combined into
one, as in View/Controller or ViewController.

One source I found said that for desktop GUI apps,
the distinction between View and Controller is not
important, but that it is important for web apps.

A good answer about the point of the Controller
would take the form: "Unless you have a Controller,
you will not be able to do x." (or it will harder to do x).

I am trying to do something like MVC now, and also
trying to create "ravioli code", and wondering if it is
worth my time to create a separate Controller. To
start, I am just trying to separate any GUI aspect
(display and user selection) from logic like database
queries and other processes. I will have some
wxTimers involved, too, and realize these can only
run in the main loop, so I'll need to think about how
that factors in here as well.

Regarding your wxTimer point, I don't really understand.
The app is defined in the __init__ method of the View:
wx.InitAllImageHandlers()# if necessary
self.app = wx.App(0)

Then, the mainloop is launched FROM the presenter (Controller) calling
for instance view.Start(), method defined in the View:
self.Show()
self.app.MainLoop()

Consequently, once the app is launched, you can put your timers
wherever you want:
- in the presenter,
- in the view,
and interacts with them without problems in the interactor.

If you need a small very simple app, let us know.

I hope this can help you.

Dominique

···

On Jul 20, 11:25 pm, C M <cmpyt...@gmail.com> wrote:

That is about the state of affairs in GNUmed as well - it
works quite nicely for us.

However, consider the following situation:

The Model contains a class of, say, Vaccination.

The View has a dialog which can display fields for adding a
new or editing an existing vaccination.

Our code (a listctrl on-activate handler, to be precise)
hands a vaccination instance to the edit-vaccination dialog
which displays it.

When the user clicks "Save" two things are supposed to
happen:

1) validation of the fields - whether they are valid-for-saving

2) the actual saving

Now, the Vaccination class knows how to save its values to
the database. However, we certainly don't want to fill the
vaccination instance with possibly invalid data from the
dialog just to discover on saving that now we've got a
vaccination instance with invalid data contained in it (in
the model, not in the database).

The validation can potentially take part in several places:

- inside the Vaccination class upon setting a value
- inside the dialog before letting the Save handler do its thing
- in some unbound method before passing the dialog data
  to the vaccination object

Then the question is - who takes action ?

The dialog could upon invoking the Save handler validate the
data and pass it to the vaccination instance.

But why would business logic (validation) be
done by a dialog ?

The dialog could tell the vaccination instance that it
was told to save the data.

Now the vaccination object would have to know how to
get the data from that particular dialog into the model
class. There could be several different dialogs ...

This is AFAIU the job of the controller - it provides the
glue between Model and View: The View tells the controller
that it wants to be saved. The controller knows

1) how to transfer data from the dialog to the vaccination class

  which the vaccination class shouldn't need to know

2) how to effect validation of the data

3) how to tell the vaccination class to save itself

  which the dialog shouldn't need to know

I am not sure whether this helps any but there's
my 2 Cents anyway :slight_smile:

Karsten

···

On Mon, Jul 20, 2009 at 05:25:21PM -0400, C M wrote:

I am trying to do something like MVC now, and also
trying to create "ravioli code", and wondering if it is
worth my time to create a separate Controller. To
start, I am just trying to separate any GUI aspect
(display and user selection) from logic like database
queries and other processes.

--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

A small additional twist I run into is that most IDEs (Boa in my case,
and I recall, Glade) mix the View and Interactor/Controller by binding
events within the same files as the View. I think it’s a fairly small
penalty for extreme convenience of GUI design, and I effect a compromise
by making the IDE-generated event handler stubs in the View files call
dedicated methods in a “FrameEvents” file. Not strictly MVC,
but the idea is that I don’t do any hand coding of the GUI files and the
IDE can’t mess with my event methods.

Although in Boa, if you delete the event in its Inspector it really only
deletes the binding, not the method. And, the Controller is instantiated
in the main app.py under def main():

My current design issue is to do something like pub/sub across processes.
My Model is shared memory, and the Controllers work in separate processes

  • I’m leaning toward a mailbox approach using shared memory data stores,
    but still testing.

  • Ray Schumacher

···

At 02:37 AM 7/21/2009, you wrote:

According to

http://wiki.wxpython.org/ModelViewPresenter
, "the

difference between MVC and MVP is that in MVP the presenter does not

handles the GUI events directly as the controller does in MVC but

through delegation via the interactor".

Thus, in the MVC, the bindings are done in the Controller while they

are kept seperated in the interactor in the MVP.

RayS wrote:

According to ModelViewPresenter - wxPyWiki, "the
difference between MVC and MVP is that in MVP the presenter does not
handles the GUI events directly as the controller does in MVC but
through delegation via the interactor".
Thus, in the MVC, the bindings are done in the Controller while they
are kept seperated in the interactor in the MVP.

To me, the benefit of MV* is that, ideally, the same Model could be used
with different Views, and the same View with different models.

As a basic example, you might want a desktop and web interface to the
same model, and at the very least an automated testing and GUI interface
to the model, and your gui app may want to use two different models
(perhaps a flat file store, and RDBMS)

However, you need a Controller (or presenter) to tie the two together,
and it seems to me that that Controller needs to know a lot about the
View, and thus is really tied to it, which makes me wonder what the
point is -- for each view you need a different controller -- so why not
just put them together?

For instance , in the Presenter example in the wiki, the Presenter needs
to work with a view that has the following methods:

view.setTitle()
view.setArtist()
view.setClassical()
view.setComposer()
view.setComposerEnabled()
view.setAlbums()
view.setSelectedAlbum()
view.setWindowTitle()
view.setApplyEnabled()
view.setCancelEnabled()

That's a lot of coupling, and I can't image that a web interface would
have the same set of methods.

Actually, I'd love to see a small but real example of MVC(P) with more
than one of each of the components.

-Chris

···

At 02:37 AM 7/21/2009, you wrote:

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

That's pretty much why GNUmed doesn't have a controller
separate from the View either. In small to medium
applications this probably works just fine.

Karsten

···

On Tue, Jul 21, 2009 at 09:50:16AM -0700, Christopher Barker wrote:

RayS wrote:
> At 02:37 AM 7/21/2009, you wrote:
>> According to ModelViewPresenter - wxPyWiki, "the
>> difference between MVC and MVP is that in MVP the presenter does not
>> handles the GUI events directly as the controller does in MVC but
>> through delegation via the interactor".
>> Thus, in the MVC, the bindings are done in the Controller while they
>> are kept seperated in the interactor in the MVP.

To me, the benefit of MV* is that, ideally, the same Model could be used
with different Views, and the same View with different models.

As a basic example, you might want a desktop and web interface to the
same model, and at the very least an automated testing and GUI interface
to the model, and your gui app may want to use two different models
(perhaps a flat file store, and RDBMS)

However, you need a Controller (or presenter) to tie the two together,
and it seems to me that that Controller needs to know a lot about the
View, and thus is really tied to it, which makes me wonder what the
point is -- for each view you need a different controller -- so why not
just put them together?

--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

Werner F. Bruhin wrote:

I am trying to do something like MVC now, and also
trying to create "ravioli code",

It is pasta too, but I think you meant "Spaghetti" :wink:

Compared to Spaghetti, Ravioli is a good thing: http://en.wikipedia.org/wiki/Ravioli_code:

"""
Ravioli code is a type of computer program structure, characterized by a number of small and (ideally) loosely-coupled software components. The term is in comparison with spaghetti code, comparing program structure to pasta; with ravioli (small pasta pouches containing cheese, meat, or vegetables) being analogous to objects (which ideally are encapsulated modules consisting of both code and data).
"""

···

--
Robin Dunn
Software Craftsman

Hi Che,

C M wrote:

In the MVC pattern, what’s the point of the Controller?

I don’t have an answer, but maybe you find it on this page.

http://wiki.wxpython.org/wxPython%20Patterns

Thanks, I did look at that, but that was where I was getting the
use of “View/Controller” as one unit. But it is helpful and bears
going over more than once or twice. I have not really thought

structurally about my code yet, so it is a new mental exercise.

I am trying to do something like MVC now, and also
trying to create “ravioli code”,

It is pasta too, but I think you meant “Spaghetti” :wink:

Sadly, what I have is already spaghetti, if not even angel
hair pasta, which is harder to untangle! :smiley: I had heard about
“ravioli” code on this list some time back from Peter

Damoc, and it seemed like perhaps a good idea to try to learn
how to do. I’m not sure whether this pattern is compatible w/
MVC or not…

Thanks,
Che

···

On Tue, Jul 21, 2009 at 2:34 AM, Werner F. Bruhin wbruhin@gmail.com wrote:

To me, the benefit of MV* is that,
ideally, the same Model could be used

with different Views, and the same View with different models.

As a basic example, you might want a desktop and web interface to the

same model, and at the very least an automated testing and GUI interface

to the model, and your gui app may want to use two different models

(perhaps a flat file store, and RDBMS)

I agree; you should be able to put a different graphics/events system in
front of the Model - the Wiki posits that "the program should in
essence be fully functional even without a View/Controller attached
"

However, you need a Controller (or
presenter) to tie the two together,

and it seems to me that that Controller needs to know a lot about the

View, and thus is really tied to it, which makes me wonder what the

point is – for each view you need a different controller – so why not

just put them together?

I only separate them to make the files readable and keep anyone (and me)
from hand-modifying the IDE generated files.

For instance , in the Presenter
example in the wiki, the Presenter needs

to work with a view that has the following methods:

That’s a lot of coupling, and I can’t image that a web interface would

have the same set of methods.

Which is why I look at it like Model-View-Presenter-Controller

Actually, I’d love to see a small
but real example of MVC(P) with more

than one of each of the components.

If I jin one in testing this stuff I’ll post.
For an example, I actually have a similar issue; I have separate Classes
for various ADC “drivers” and the data. The data is certainly
Model, but where does the ADC class fit? Functionally it fills the Model
data but must be separable to allow for any ADC hardware - otherwise it
looks like a Model. Controller commands go to ADC Class, ADC fills Model
via ctypes.memmove( ), Controller gets() data from Model to
transform and display in View.

There must be another Model class as well for application state (to set
radio button state etc. when opening the 10+ dialogs)

I have one Controller per core.

View only talks to Controller, never ADC directly.

There must be a “notifications system” as the Wiki puts it, to
allow anonymous synch of components as needed.

I want to allow future development to be able to insert C/C++ Controllers
if needed.

I’m also trying to get my head around multiprocessing class; it seems
like it should work here but going through the examples and modifying is
taking some time.

For the future, either OSes and compilers need to get smarter about
multi-core so we can just use Threads, or we need tools that use cores -
hopefully multiprocessing will do that.

And then how about file structure?

I have seen at least one app organized so:

App/

Models/

Views/

Controllers/

which makes imports interesting, at least.

-Ray Schumacher

···

At 09:50 AM 7/21/2009 -0700, you wrote:

Perhaps because only the FSM can comprehend 10,000 lines of FORTRAN 77
:slight_smile:

···

At 11:23 AM 7/21/2009 -0700, you wrote:

Werner F. Bruhin wrote:
>> I am trying to do something like MVC now, and also
>> trying to create "ravioli code",
> It is pasta too, but I think you meant "Spaghetti" :wink:

Compared to Spaghetti, Ravioli is a good thing:
http://en.wikipedia.org/wiki/Ravioli_code:

Thank you, Dominique, for this thorough explanation, very helpful.
In my case (and in Werner’s) I am using Boa Constructor to create and

manage the GUI elements, and Boa does the event binding right in the
GUI code, so in that case it might be more work to create a separate
interactor class to do the binding. (This is mentioned in this thread by

RayS).

I think part of my confusion is that I am giving my Model everything
to do, not just database queries but any processing, and perhaps that
ought to be done in a Controller/what you are calling a Presenter.

Also a good point about the timers able to be anywhere.

Che

···

On Tue, Jul 21, 2009 at 5:37 AM, Dominique MyDomDom@gmail.com wrote:

Hi Che and Werner,

Although I am only an amateur and consequently an eternel newbie,

I’ll try explain what I understood from the MVP, which is quite

similar to MVC.

Python and wxPython Gurus, please correct me if I am misunderstanding

something.

In the MVP, you have several components: the model, the view and the

interactor, and the Presenter.

According to http://wiki.wxpython.org/ModelViewPresenter, "the

difference between MVC and MVP is that in MVP the presenter does not

handles the GUI events directly as the controller does in MVC but

through delegation via the interactor".

Thus, in the MVC, the bindings are done in the Controller while they

are kept seperated in the interactor in the MVP.

The model:

  • manages all data coming in / from the database,

  • defines methods to get data from the db (queries,…)

The View:

  • is a gui with a frame, panels, buttons, fields,…

  • is modified through:

    • the presenter which has methods calling, for instance,

      view.TextCtrl_1.ChangeValue()

    • the interactor which binds all widgets of the view to methods

that

    send back to methods defined in the presenter

The Interactor:

  • binds all the View widgets to methods

  • these methods call Presenter’s methods that update the view

The Presenter:

  • takes care of the logic of the application

  • has methods that :

    • update the View (send data to the View)

    • get data from the View through the Interactor

    • get data from the model

    • send data to the model

Everything goes through the Presenter

For instance, if a button is pressed:

  • the button is defined in the View

  • the button is bound in the Interactor, with a method that calls

    a Presenter’s method such as:

      def OnButtonPressed(self, evt):
    
          # This is in the Interactor
    
          self.presenter.UpdateView()
    
  • the Presenter’s method is called:

      def UpdateView(self):
    
          # This is in the Presenter
    
          self.view.SetTextCtrl_1(value= self.view.GetTextCtrl_2())
    
          self.model.UpdateDb()
    

    which:

    • updates the View (the value of the TC_1 is set to the value of

the TC_2)

- calls the updateDb method of the Model class, which updates the

db, doing

    this or that

As a consequence, the Presenter manages the logic of the app, since

when the user acts on the gui elements, it goes to the interactor and

then to the Presenter where the action in response to the user actions

is defined:

gui action → interactor (bindings) → Calls Presenter’s method →

action managed by the Presenter (on the View, database,…).

To my understanding, the Presenter (or Controller) makes it easier to

modify the gui without putting the mess in the app.

You just modify your gui and the bindings in the interactor.

Since the logic of the app is in the presenter, you keep it clean.

To me, if the app becomes a little bit complex, it is certainly

worthwhile to use the MVP or MVC.

On Jul 20, 11:25 pm, C M cmpyt...@gmail.com wrote:

In the MVC pattern, what’s the point of the Controller?

I found this way of thinking about MVC:

Input → Processing → Output

Controller → Model → View

…but why shouldn’t one thing take care of input and output,

since both are handled by GUI code (e.g. .SetValue and

.GetValue). To add to my confusion, in the wxPython

doc about MVC, often the two words are combined into

one, as in View/Controller or ViewController.

One source I found said that for desktop GUI apps,

the distinction between View and Controller is not

important, but that it is important for web apps.

A good answer about the point of the Controller

would take the form: "Unless you have a Controller,

you will not be able to do x." (or it will harder to do x).

I am trying to do something like MVC now, and also

trying to create “ravioli code”, and wondering if it is

worth my time to create a separate Controller. To

start, I am just trying to separate any GUI aspect

(display and user selection) from logic like database

queries and other processes. I will have some

wxTimers involved, too, and realize these can only

run in the main loop, so I’ll need to think about how

that factors in here as well.

Regarding your wxTimer point, I don’t really understand.

The app is defined in the init method of the View:

wx.InitAllImageHandlers()# if necessary

self.app = wx.App(0)

Then, the mainloop is launched FROM the presenter (Controller) calling

for instance view.Start(), method defined in the View:

self.Show()

self.app.MainLoop()

Consequently, once the app is launched, you can put your timers

wherever you want:

  • in the presenter,

  • in the view,

and interacts with them without problems in the interactor.

If you need a small very simple app, let us know.

I hope this can help you.

Dominique

That is helpful, Karsten. It makes the point about separation of jobs
well in that in this description the GUI and Model really are only aware
of how to do their jobs and nothing else about the app.

Thanks,
Che

···

On Tue, Jul 21, 2009 at 8:43 AM, Karsten Hilbert Karsten.Hilbert@gmx.net wrote:

On Mon, Jul 20, 2009 at 05:25:21PM -0400, C M wrote:

I am trying to do something like MVC now, and also

trying to create “ravioli code”, and wondering if it is

worth my time to create a separate Controller. To

start, I am just trying to separate any GUI aspect

(display and user selection) from logic like database

queries and other processes.

That is about the state of affairs in GNUmed as well - it

works quite nicely for us.

However, consider the following situation:

The Model contains a class of, say, Vaccination.

The View has a dialog which can display fields for adding a

new or editing an existing vaccination.

Our code (a listctrl on-activate handler, to be precise)

hands a vaccination instance to the edit-vaccination dialog

which displays it.

When the user clicks “Save” two things are supposed to

happen:

  1. validation of the fields - whether they are valid-for-saving

  2. the actual saving

Now, the Vaccination class knows how to save its values to

the database. However, we certainly don’t want to fill the

vaccination instance with possibly invalid data from the

dialog just to discover on saving that now we’ve got a

vaccination instance with invalid data contained in it (in

the model, not in the database).

The validation can potentially take part in several places:

  • inside the Vaccination class upon setting a value

  • inside the dialog before letting the Save handler do its thing

  • in some unbound method before passing the dialog data

    to the vaccination object

Then the question is - who takes action ?

The dialog could upon invoking the Save handler validate the

data and pass it to the vaccination instance.

But why would business logic (validation) be

done by a dialog ?

The dialog could tell the vaccination instance that it

was told to save the data.

Now the vaccination object would have to know how to

get the data from that particular dialog into the model

class. There could be several different dialogs …

This is AFAIU the job of the controller - it provides the

glue between Model and View: The View tells the controller

that it wants to be saved. The controller knows

  1. how to transfer data from the dialog to the vaccination class

     which the vaccination class shouldn't need to know
    
  2. how to effect validation of the data

  3. how to tell the vaccination class to save itself

     which the dialog shouldn't need to know
    

I am not sure whether this helps any but there’s

my 2 Cents anyway :slight_smile:

Karsten

A small additional twist I run into is that most IDEs (Boa in my case,
and I recall, Glade) mix the View and Interactor/Controller by binding
events within the same files as the View. I think it’s a fairly small
penalty for extreme convenience of GUI design, and I effect a compromise
by making the IDE-generated event handler stubs in the View files call
dedicated methods in a “FrameEvents” file. Not strictly MVC,
but the idea is that I don’t do any hand coding of the GUI files and the
IDE can’t mess with my event methods.

Although in Boa, if you delete the event in its Inspector it really only
deletes the binding, not the method. And, the Controller is instantiated
in the main app.py under def main():

This is a key point for me, because I use Boa Constructor, too. So is your Controller then created with Boa as a wx.App (which it calls class BoaApp)?

Is the point of the FrameEvents file just to truly keep the GUI code only GUI? After using Boa for awhile I have tended to think of GUI and events as going together. Do you find it is worth it to separate them?

My current design issue is to do something like pub/sub across processes.
My Model is shared memory, and the Controllers work in separate processes

  • I’m leaning toward a mailbox approach using shared memory data stores,
    but still testing.

Beyond the part about pubsub, I don’t know what this means.

Thanks,
Che

I think it's a fairly small penalty for extreme convenience of GUI design, and I effect a compromise by making the IDE-generated event handler stubs in the View files call dedicated methods in a "FrameEvents" file. Not strictly MVC, but the idea is that I don't do any hand coding of the GUI files and the IDE can't mess with my event methods.
Although in Boa, if you delete the event in its Inspector it really only deletes the binding, not the method. And, the Controller is instantiated in the main app.py under def main():

This is a key point for me, because I use Boa Constructor, too. So is your Controller then created with Boa as a wx.App (which it calls class BoaApp)?

you can, as in the wiki example,

def main():
     Controller(app)
     application = BoaApp(0)
     application.MainLoop()

which requires pubsub or similar to communicate between Controller/Model.

I (for now) import the event/controller as in:
#MDIParentFrame1.py
import MDIParentFrame1Events
     def OnMenuFileItemsopenconfMenu(self, event):
         MDIParentFrame1Events.OnMenuFileItemsopenconfMenu(self, event)

#MDIParentFrame1Events.py
def OnMenuFileItemsopenconfMenu(self, event):
     #do stuff

Is the point of the FrameEvents file just to truly keep the GUI code only GUI? After using Boa for awhile I have tended to think of GUI and events as going together. Do you find it is worth it to separate them?

Only because I have 60+ events in the GUI parent frame and it is almost 1000 lines just with stubs, and no accidental mods to the GUI code.
They do really go together as you say.

My current design issue is to do something like pub/sub across processes. My Model is shared memory, and the Controllers work in separate processes - I'm leaning toward a mailbox approach using shared memory data stores, but still testing.

Beyond the part about pubsub, I don't know what this means.

DSP programmers use "mailboxes", and older C++ MFC programs used message methods and idle main loops.
The Model must be shared memory to be accessible between processes (hopefully via the new multiprocessing Class) which do FFTS etc. The mailbox is then just another named shared memory block/structure of ints/char. This seems to be what multiprocessing does behind the scenes.

-Ray

···

At 03:09 PM 7/21/2009 -0400, you wrote:

> However, consider the following situation:
>
> The Model contains a class of, say, Vaccination.
>
> The View has a dialog which can display fields for adding a
> new or editing an existing vaccination.
>
> Our code (a listctrl on-activate handler, to be precise)
> hands a vaccination instance to the edit-vaccination dialog
> which displays it.

In a pure MV(C/P) application what should happen here is
this:

- the listctrl tells the controller that the user wants to
  edit a certain vaccination

- the controller retrieves the vaccination instance

- the controller instantiates a vacination editor

- the controller hands the vaccination instance to the editor

Thus we've got a decoupling between GUI and model code as
well as between GUI elements. That makes it less messy to
change either of the vaccination list ctrl or the
vaccination editor.

What GNUmed currently does is this:

Within the file which holds both the vaccination list
control as well as the vaccination editor there is a
module-global function

  def edit_vaccination(vaccination=None):

which pretty much acts like a controller above except that
callers pass in the vaccination instance. This way the list
ctrl doesn't have to know about the editor. It just calls
edit_vaccination() and passes in the vaccination instance
that was selected in it.

> When the user clicks "Save" two things are supposed to
> happen:
>
> 1) validation of the fields - whether they are valid-for-saving

> - inside the Vaccination class upon setting a value
> - inside the dialog before letting the Save handler do its thing
> - in some unbound method before passing the dialog data
> to the vaccination object

Doing it on setting a value makes the change of several
values at once non-atomic, however. While the vaccination
will stay medically valid it will not reflect Truth anymore ...

Thus we want a dedicated method validating values *before*
setting any of them in the model class. That extra validator
can then live in either of the mentioned places. Currently
we've got it in the view which is non-ideal as it can then
not be re-used - but we found it to be not easily reusable.

And since saving any of our model classes to the database is
nothing but calling instance.save() we do that in the SAVE
handler as well ...

Personally, I can see the point of the controller and what
it should do. However, in the application I have worked with
it would have amounted to apparently little gain over the
abovementioned half-assed controller approach that we
haven't bothered to more cleanly factor it out.

Karsten

···

On Tue, Jul 21, 2009 at 03:03:58PM -0400, C M wrote:
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

I also think some validation needs to be done in the dialog/View, as it would be like adding Boa itself to the app to allow the introspection and validation of parent widget entries. Some validation depends on the state of the app or what was entered in other dialogs so it will need to pull info from a Model to get that.

For my personal Boa-user paradigm I guess I should be writing dialogX.py and dialogXcontrol.py. On "OK" button the dialog binding should call the dialog's Controller and so not self.Close() unless the Control returns a good indicator.

In the Web world, form parsers validate specific forms and re-show the form if there is an error, then do SQL.
As far as modularity, I'd also like to think I can replace control.py with a .pyd or .dll and have it work.
And, if you wanted to replace the dialog with a slightly different one (or command line args), the validation code would need to change too.

-Ray

···

At 11:09 PM 7/21/2009 +0200, you wrote:

Thus we want a dedicated method validating values *before*
setting any of them in the model class. That extra validator
can then live in either of the mentioned places. Currently
we've got it in the view which is non-ideal as it can then
not be re-used - but we found it to be not easily reusable.

Robin Dunn wrote:

Werner F. Bruhin wrote:

I am trying to do something like MVC now, and also
trying to create "ravioli code",
      

It is pasta too, but I think you meant "Spaghetti" :wink:
    
Compared to Spaghetti, Ravioli is a good thing: http://en.wikipedia.org/wiki/Ravioli_code:

"""
Ravioli code is a type of computer program structure, characterized by a number of small and (ideally) loosely-coupled software components. The term is in comparison with spaghetti code, comparing program structure to pasta; with ravioli (small pasta pouches containing cheese, meat, or vegetables) being analogous to objects (which ideally are encapsulated modules consisting of both code and data).
"""

I stay corrected, never heard that term before in this context.

Che, sorry for thinking you didn't know better :slight_smile:
Werner

C M wrote:

In the MVC pattern, what's the point of the Controller?

As you've seen by now, there are many variations of the pattern and ways to implement it. For working with wxPython, I interpret it this way:

The Model is solely concerned with the collection/composition of data used in the UI (which may be a form, subform, etc.). It makes all the data available to View methods and notifies the View(s) of any changes.

The View is primarily oriented toward display. When notified by the Model, it will do what's necessary to make/keep the display consistent with the (possibly changed) model data. The only other thing it does is provide the Controller with access to the displayed values (not necessarily in 1-1 fashion; it could be an abstraction).

The Controller may be a class, or just a collection of methods bound to UI events. A controller method may read from parts of the View, but never modify it. Its sole function is to decide, based on the nature of the event and state of the display, what method of the Model to call.

For validation: simple "syntactic" validation can and should be done as locally as possible, for example, numeric input in a text box. Any validation involving more than one data element should be done by the model. This implies that the View should be prepared to handle an "invalid" notification and translate it into an appropriate change to the display to alert and inform the user.

Here's a nice simple MVC setup for a radio button set:
- Model: a "value holder" that can hold any value, possibly constrained by type. Notifies the registered radio button widgets when the value changes, passing the new value.
- View: a radio button widget with a "MyValue" instance variable. When notified with a value, sets its state to On iff MyValue == the notification value.
- Controller: called when a button is clicked. Reads its MyValue and sends it to the Model to replace the held value.

A radio button group is simply a set of Views that are registered with the same Model. Also, no special code is needed to ensure consistency among the buttons, or to handle the case where the Model's value doesn't match any of the buttons' MyValues.

An exercise: generalize this to a set of, say, 10 buttons, of which up to 3 may be "on" at the same time. (E.g., an election for a Board of Directors where the voter can choose a subset of the candidates.) It's pretty simple. (But what happens when 3 buttons are on, and the user clicks another one?)

···

--
Don Dwiggins
Advanced Publishing Technology