E
elvis_the_king
Hi all,
I have quite some design experience on other platforms and especially
with web applications. Now I'm about to design a .NET application and
am a little stuck as I am trying to apply my "habits" of separating
model, view and controller. Either that is the totally wrong approach
or I am not familiar enough with the platform to make it .
By now, I've already written some model classes and test cases. In that
simple case, the "controller" is nothing more than the logic that
creates the right instances of models, connects them and calls some
business methods on them, validating the results.
Now as it comes to adding a GUI and responding to user input, I was
hoping that there is something like the "blueprints" with J2EE, that
is, some patterns as design recommendations on how to sort things out;
or maybe you can share your thoughts and experience with me?
I'll try to describe the problems I've run into so far; that should
make it easier to reply as the question will be less abstract
For every model, I will have one or more views to display the models.
These views are subclasses of System.Windows.Forms.Form.
Basically - how to separate controller and view? It seems to me that
it's always the Form class that reacts on user input; but maybe that is
just because the IDE/designer suggests that? I assume it is possible to
move that code to some "controller" class (probably somehow using the
properties of the form controls?) - but is that a good idea? Shouldn't
I just keep it as suggested?
Next, is it right to create a new instance of a certain view class (the
form) for every model I want to display? Or should I avoid creating too
many instances of the form class - that would mean I should instantiate
every form only once and use something like the flyweight pattern to
provide the view with the model it is supposed to display?
If several form instances are ok and I keep the controller logic in the
form class as well, one approach would be to simply pass the form its
model in the constructor like:
static void Main() {
Model m = new Model();
View v = new View(m); // View subclasses System.Windows.Forms.Form
Application.Run(v);
}
The view would then - maybe in an btnOK_Click handler - update the
model, maybe like
void button1_Click(object sender, System.EventArgs e) {
myModel.doSomeBusiness(textBox1.Text);
}
Is that a good approach? Clearly, what button1_Click does is the job of
a controller.
One more problem with the "pass model to form/view constructor"
approach is when instantiating new models: Sometimes I need to display
a form to collect some user input; that user input is necessary for
creating the new model instance. Later on, the same view class is
supposed to provide the means of manipulating the model again.
I feel it's not a good idea let the form be constructed without a model
and add another method like "createAModelFromInput()" that would build
the "new" model for me.
Thus, another way would be to have something like (informal notation)
class Controller {
newModelUseCase {
v = new View()
v.display()
// extract "pure data" from view here
m = new model(data extracted)
}
updateModelUseCase {
m = someModelIAlreadyGot;
v = new View()
// extract all data from model here
// populate v with the data
v.display()
}
}
Now that is clearly too much for a controller - the whole "extract data
from model and populate view" thing is the view's responsibility.
As a side note, with my specific application requirements I would
benefit if the whole create new model / update model could be generic.
I know that there will be future extensions to my application that will
bring new models with appropriate views, so it would be elegant to keep
that part of code arbitrary enough to work with such extensions.
As to updating the view upon model state changes, that's probably no
problem with a standard observer pattern approach where the view might
(pherhaps in its constructor) subscribe for model notifications. The
only thing I'm a little afraid here is that in the future, threading
problems might arise (what if the model changes in a non-GUI thread and
does the notification from there?).
I'm happy you read until here Again, point me to some recommended
approaches/patterns/best practices if there are some - I found
nothing that satisfies me .
Please reply to the group - I will follow things here.
Thanks in advance,
Matthias
I have quite some design experience on other platforms and especially
with web applications. Now I'm about to design a .NET application and
am a little stuck as I am trying to apply my "habits" of separating
model, view and controller. Either that is the totally wrong approach
or I am not familiar enough with the platform to make it .
By now, I've already written some model classes and test cases. In that
simple case, the "controller" is nothing more than the logic that
creates the right instances of models, connects them and calls some
business methods on them, validating the results.
Now as it comes to adding a GUI and responding to user input, I was
hoping that there is something like the "blueprints" with J2EE, that
is, some patterns as design recommendations on how to sort things out;
or maybe you can share your thoughts and experience with me?
I'll try to describe the problems I've run into so far; that should
make it easier to reply as the question will be less abstract
For every model, I will have one or more views to display the models.
These views are subclasses of System.Windows.Forms.Form.
Basically - how to separate controller and view? It seems to me that
it's always the Form class that reacts on user input; but maybe that is
just because the IDE/designer suggests that? I assume it is possible to
move that code to some "controller" class (probably somehow using the
properties of the form controls?) - but is that a good idea? Shouldn't
I just keep it as suggested?
Next, is it right to create a new instance of a certain view class (the
form) for every model I want to display? Or should I avoid creating too
many instances of the form class - that would mean I should instantiate
every form only once and use something like the flyweight pattern to
provide the view with the model it is supposed to display?
If several form instances are ok and I keep the controller logic in the
form class as well, one approach would be to simply pass the form its
model in the constructor like:
static void Main() {
Model m = new Model();
View v = new View(m); // View subclasses System.Windows.Forms.Form
Application.Run(v);
}
The view would then - maybe in an btnOK_Click handler - update the
model, maybe like
void button1_Click(object sender, System.EventArgs e) {
myModel.doSomeBusiness(textBox1.Text);
}
Is that a good approach? Clearly, what button1_Click does is the job of
a controller.
One more problem with the "pass model to form/view constructor"
approach is when instantiating new models: Sometimes I need to display
a form to collect some user input; that user input is necessary for
creating the new model instance. Later on, the same view class is
supposed to provide the means of manipulating the model again.
I feel it's not a good idea let the form be constructed without a model
and add another method like "createAModelFromInput()" that would build
the "new" model for me.
Thus, another way would be to have something like (informal notation)
class Controller {
newModelUseCase {
v = new View()
v.display()
// extract "pure data" from view here
m = new model(data extracted)
}
updateModelUseCase {
m = someModelIAlreadyGot;
v = new View()
// extract all data from model here
// populate v with the data
v.display()
}
}
Now that is clearly too much for a controller - the whole "extract data
from model and populate view" thing is the view's responsibility.
As a side note, with my specific application requirements I would
benefit if the whole create new model / update model could be generic.
I know that there will be future extensions to my application that will
bring new models with appropriate views, so it would be elegant to keep
that part of code arbitrary enough to work with such extensions.
As to updating the view upon model state changes, that's probably no
problem with a standard observer pattern approach where the view might
(pherhaps in its constructor) subscribe for model notifications. The
only thing I'm a little afraid here is that in the future, threading
problems might arise (what if the model changes in a non-GUI thread and
does the notification from there?).
I'm happy you read until here Again, point me to some recommended
approaches/patterns/best practices if there are some - I found
nothing that satisfies me .
Please reply to the group - I will follow things here.
Thanks in advance,
Matthias