Creating MVC with Observer-Pattern

  • Thread starter Thread starter Marcel Hug
  • Start date Start date
M

Marcel Hug

Hi NG !
I have already written a task about MVC and I tried to get the best
informations together.
I would like to implement the MVC pattern and it work on the way I did it.

At first i know the MVC-ipmlementation from the JAVA by using the
observer-pattern.

I used an interface IObservable (AddObserver, RemoveObserver,...). My Model
implemented this interface.
I created a second interface IObserver (Update(Iobservable)).
I have to datagrids, which have to show the content of correpsoning datasets
in my model, which would be updated by user interactions.

So i created 2 classes (one for each grid), which extends from DataGrid and
implements IObserver.
In my dump mainForm i designed my to datagrids.
In the constructor i used the following code:

IObservable myModel = new SupportMaintainer();

myModel.AddObserver((IObserver) this.dataGridFailures);

myModel.AddObserver((IObserver) this.dataGridMessages);


Now my questions:

1.) In order to run the program i have initialized my designed datagrid with
my two classes (which extends) from datagrid.
It works good, but I have done this initializing in the InitialieComponent()
Methode, which should not be edited in the editor (summery-remark). Is it a
"illegal" way to do this, like i did ? Why should this code not be edited ?.

Would it be better to create UserControls for my datagrid classes
(Observers) ?

2.) Is there a possability to use my classes in the form without creating
usercontrol. I guess not by using the designer !?!

Thanks and regards

Marcel
 
"Marcel Hug" <[email protected]> a écrit dans le message de
news: (e-mail address removed)...

| I have already written a task about MVC and I tried to get the best
| informations together.
| I would like to implement the MVC pattern and it work on the way I did it.

Marcel, you seem to be intent on adding the Model and Controller to your
form class.

This is not how MVC is intended to work; the whole idea of MVC and its
successor MVP, is to separate UI functionality from business model
functionality via a Controller. That Controller should not be a part of the
View (form), it is meant to hold a reference to both the Model and the View;
the way you are doing it, the View has a reference to the Model and
Controller.

| Now my questions:
|
| 1.) In order to run the program i have initialized my designed datagrid
with
| my two classes (which extends) from datagrid.
| It works good, but I have done this initializing in the
InitialieComponent()
| Methode, which should not be edited in the editor (summery-remark). Is it
a
| "illegal" way to do this, like i did ? Why should this code not be edited
?.

It is not "illegal" to alter the InitializeComponent() method, but it is not
wise so to do as the IDE manages this code and could scrub any alterations
you make. You should amend the constructor, just after the call to
InitializeComponent().

Better still, if you followed the correct way of implementing MVC, you
wouldn't need to write any code at all in the form class.

| Would it be better to create UserControls for my datagrid classes
| (Observers) ?
|
| 2.) Is there a possability to use my classes in the form without creating
| usercontrol. I guess not by using the designer !?!

If you have derived from DataGrid in order to implement the Observer
pattern, then you don't need any other classes on the form. Create a module
for the Model and another for the Controller, do all the work in those.
There is absolutely no point at all in implementing MVC within a form class,
it just doesn't make sense.

Joerg also suggested that you don't need to implement MVC as the data-aware
nature of .NET controls removes a lot of that requirement. Hµowever, if you
insist on using MVC for a learning experience, then you should not use the
"classic" implementation using the "classic" Observer pattern, instead you
need to use a modified pattern that takes advantage of things like multicast
delegates, etc that .NET provides.

Take the example of the ISubject or IObservable interface; all it does is
maintain a list of IObservers and broadcast a call to an Update method. This
can be replaced by the following interfaces :

public interface ISubject
{
event EventHandler Notify;
}

public interface IObserver
{
void Update(object sender, EventArgs args);
}

Joanna
 
Back
Top