471,337 Members | 1,115 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Creating MVC with Observer-Pattern

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
Jan 4 '06 #1
1 6291
"Marcel Hug" <ma******************@ATch.abb.com> a écrit dans le message de
news: Oi*************@tk2msftngp13.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer

Jan 4 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Pawan | last post: by
15 posts views Thread by Carlos Lozano | last post: by
2 posts views Thread by LIN | last post: by
9 posts views Thread by =?Utf-8?B?YmJn?= | last post: by
2 posts views Thread by Anders Dahnielson | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.