MVC-Pattern in my C# GUI !?!

M

Marcel Hug

Hi NG !
For learning work with patterns I would like to add the MVC-Pattern in my
little application.
On my form (View) I have a TabController with 2 tabs. In each tab I would
like to have a datagrid, which should be updated after some changes. So this
two datagrids must implement my observer-interface. My class supportControl
would be the Model, which extends from my observable class.
Thats my theoretical understanding...now my questions to the implementation
in C#:

1.) The 2 Datagrids are the objects, which must be updated. Do I have to
create two classes (for each datagris one), which extends from
System.Windows.Forms.DataGrid and implements my observer interface ?
2.) My DataGrid on the main form would initialized by the classes i created
under point 1.) ???

Sorry this questions, but I'm not sure, if thats the right way of
implementation MVC !



Thanks and regards

Marcel Hug
 
D

Dries

Marcel,
For learning work with patterns I would like to add the MVC-Pattern in my
little application.
I have tried to implement the MVC-pattern as well, but I did not
completely succeed.
On my form (View) I have a TabController with 2 tabs. In each tab I would
like to have a datagrid, which should be updated after some changes. So this
two datagrids must implement my observer-interface. My class supportControl
would be the Model, which extends from my observable class.
Thats my theoretical understanding...now my questions to the implementation
in C#:
I did it as follows:
1/ I create my forms and I add some public properties and events. The
properties are used for setting and getting the data for the controls
(textboxes, datagrids, button-captions). The events are used for
handeling button-clicks, data-changes, etc.
1/ I create another class which I call the controller-class. This class
is the connection between the forms (my GUI) and the underlying code.
This class has methods like ShowUsers(), which gets a datatable with the
user-data from the data-layers and then creates a new instance of the
Users-form, sets the data to the form-properties and shows the form.
This class is also catching the events from the User-form.

So, an example, I have called the ShowUsers() methode on my controller
class. The Users-form has been displayed by this method. Now I can
change the data in the textboxes and other controls. When I click a
button (e.g. a Save button), the controller class catches the event. It
is then the controller class that handles the rest. The controller class
will get the adapted data from the form, save it to the database and
then return the changed data to the form.

You can also use eventargs to pass data from the form to the
controller-class.

I hope you understand what I'm doing. This is the way I implement my
kind of MVC-pattern and I must say that it works very well!

greetz,
Dries
 
J

Joanna Carter [TeamB]

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

| For learning work with patterns I would like to add the MVC-Pattern in my
| little application.

May I advise that you can't "add" MVC in an application, you really need to
design the application to use the MVC framework; but that may just be a
difference in how we say the same thing :)

| On my form (View) I have a TabController with 2 tabs. In each tab I would
| like to have a datagrid, which should be updated after some changes. So
this
| two datagrids must implement my observer-interface. My class
supportControl
| would be the Model, which extends from my observable class.

You should not have anything other than visual controls on your forms, the
Controllers should be separate classes, instantiated in code outside of the
form.

You will also need to provide a set of wrapper classes that will allow you
to detect when you set the properties of your business classes, so that the
Controller can handle things like an Changing, or Changed event fired from
the "property".

As to the visual components on the form, if you want to avoid deriving from
the existing controls to implement the Observer pattern, these also need to
have wrapper classes that implement the Observer pattern and carry out the
Update method against the enclosed UI control.

You could also just use the data-aware nature of the existing controls to
replace teh Observer pattern, but write a Controller that catches the
Validating event of the controls so that you can enforce business rules when
leaving an edit control.

IMO, MVC is fairly well implemented, although not obviously, in the new .NET
data-aware controls.

I have written a series of articles on MVP (a sort of super MVC) on my
website www.carterconsulting.org.uk

Joanna
 
J

Joerg Jooss

Hello Marcel,
Hi NG !
For learning work with patterns I would like to add the MVC-Pattern in
my
little application.
On my form (View) I have a TabController with 2 tabs. In each tab I
would
like to have a datagrid, which should be updated after some changes.
So this
two datagrids must implement my observer-interface. My class
supportControl
would be the Model, which extends from my observable class.

Actually, you need neither an observer interface (C# is not Java -- use events),
nor should your model need to extend from any particular class. Your controller
should update the model, the model should expose events to signal state changes,
and your views should subscribe to those model events they care about. See
http://www.codeproject.com/csharp/model_view_controller.asp for an introduction.

Cheers,
 
J

Jay B. Harlow [MVP - Outlook]

Joanna,
|| For learning work with patterns I would like to add the MVC-Pattern in my
|| little application.
|
| May I advise that you can't "add" MVC in an application, you really need
to
| design the application to use the MVC framework; but that may just be a
| difference in how we say the same thing :)
I agree.

However! Using Refactoring & Refactoring To Patterns, surely one can *add*
the MVC pattern to an existing app that currently doesn't use the MVC
pattern?

http://www.refactoring.com/
http://www.industriallogic.com/xp/refactoring/

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| "Marcel Hug" <[email protected]> a écrit dans le message
de
| news: (e-mail address removed)...
|
|| For learning work with patterns I would like to add the MVC-Pattern in my
|| little application.
|
| May I advise that you can't "add" MVC in an application, you really need
to
| design the application to use the MVC framework; but that may just be a
| difference in how we say the same thing :)
|
|| On my form (View) I have a TabController with 2 tabs. In each tab I would
|| like to have a datagrid, which should be updated after some changes. So
| this
|| two datagrids must implement my observer-interface. My class
| supportControl
|| would be the Model, which extends from my observable class.
|
| You should not have anything other than visual controls on your forms, the
| Controllers should be separate classes, instantiated in code outside of
the
| form.
|
| You will also need to provide a set of wrapper classes that will allow you
| to detect when you set the properties of your business classes, so that
the
| Controller can handle things like an Changing, or Changed event fired from
| the "property".
|
| As to the visual components on the form, if you want to avoid deriving
from
| the existing controls to implement the Observer pattern, these also need
to
| have wrapper classes that implement the Observer pattern and carry out the
| Update method against the enclosed UI control.
|
| You could also just use the data-aware nature of the existing controls to
| replace teh Observer pattern, but write a Controller that catches the
| Validating event of the controls so that you can enforce business rules
when
| leaving an edit control.
|
| IMO, MVC is fairly well implemented, although not obviously, in the new
..NET
| data-aware controls.
|
| I have written a series of articles on MVP (a sort of super MVC) on my
| website www.carterconsulting.org.uk
|
| Joanna
|
| --
| Joanna Carter [TeamB]
| Consultant Software Engineer
|
|
 
J

Joanna Carter [TeamB]

"Jay B. Harlow [MVP - Outlook]" <[email protected]> a écrit dans
le message de [email protected]...

| However! Using Refactoring & Refactoring To Patterns, surely one can *add*
| the MVC pattern to an existing app that currently doesn't use the MVC
| pattern?

Certainly, but not by adding the extra functionality to a form class. IME,
refactoring a data-aware, RAD, "code on the form" application is usually
best achieved by a complete redesign :)

Joanna
 
J

Jay B. Harlow [MVP - Outlook]

| best achieved by a complete redesign :)
Which is the entire point behind Refactoring: "Improving the Design of
Existing Code". ;-)

Of course I am not trying to infer that it would be easy to add.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| "Jay B. Harlow [MVP - Outlook]" <[email protected]> a écrit
dans
| le message de [email protected]...
|
|| However! Using Refactoring & Refactoring To Patterns, surely one can
*add*
|| the MVC pattern to an existing app that currently doesn't use the MVC
|| pattern?
|
| Certainly, but not by adding the extra functionality to a form class. IME,
| refactoring a data-aware, RAD, "code on the form" application is usually
| best achieved by a complete redesign :)
|
| Joanna
|
| --
| Joanna Carter [TeamB]
| Consultant Software Engineer
|
|
 
J

Joanna Carter [TeamB]

"Jay B. Harlow [MVP - Outlook]" <[email protected]> a écrit dans
le message de news: (e-mail address removed)...

| Which is the entire point behind Refactoring: "Improving the Design of
| Existing Code". ;-)
|
| Of course I am not trying to infer that it would be easy to add.

Heheh, if you think MVC is difficult, you wait until you do MVP, Model View
Presenter that is, not the MS type :)

I have now implemented MVP in both Delphi and C#; C# 2.0 has made it sooo
much easier. The object linkable data-aware controls have also enabled me to
throw out all sorts of code that used to be necessary in Delphi.

Once you get to know how MVC or MVP work, it really does save so much
hassle; most of the forms in a good MVP system have no extra code apart from
that generated by the designer for placing the components on the form. MVP,
along with a good Object Persistence Framework have gotten us to the stage
where the only code we now write is business classes and logic; storage and
UI are already done and dusted.

Joanna
 
J

Jay B. Harlow [MVP - Outlook]

| Heheh, if you think MVC is difficult, you wait until you do MVP, Model
View
| Presenter that is, not the MS type :)
Yea, I saw MVP on your site, I was going to add it to my short list of
things to investigate.

| I have now implemented MVP in both Delphi and C#; C# 2.0 has made it sooo
| much easier. The object linkable data-aware controls have also enabled me
to
| throw out all sorts of code that used to be necessary in Delphi.
Is the C# version of MVP available yet?

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| "Jay B. Harlow [MVP - Outlook]" <[email protected]> a écrit
dans
| le message de news: (e-mail address removed)...
|
|| Which is the entire point behind Refactoring: "Improving the Design of
|| Existing Code". ;-)
||
|| Of course I am not trying to infer that it would be easy to add.
|
| Heheh, if you think MVC is difficult, you wait until you do MVP, Model
View
| Presenter that is, not the MS type :)
|
| I have now implemented MVP in both Delphi and C#; C# 2.0 has made it sooo
| much easier. The object linkable data-aware controls have also enabled me
to
| throw out all sorts of code that used to be necessary in Delphi.
|
| Once you get to know how MVC or MVP work, it really does save so much
| hassle; most of the forms in a good MVP system have no extra code apart
from
| that generated by the designer for placing the components on the form.
MVP,
| along with a good Object Persistence Framework have gotten us to the stage
| where the only code we now write is business classes and logic; storage
and
| UI are already done and dusted.
|
| Joanna
|
| --
| Joanna Carter [TeamB]
| Consultant Software Engineer
|
|
 
J

Joanna Carter [TeamB]

"Jay B. Harlow [MVP - Outlook]" <[email protected]> a écrit dans
le message de news: (e-mail address removed)...

| Is the C# version of MVP available yet?

Due to copyright reasons with the companies that have paid for the
development of the code base, I can only really teach the design principles
on a consultancy basis - which I am more than willing to do :)

Joanna
 
D

Dino Buljubasic

Finally a useful reply.

Thank you Jeorg

Hello Marcel,


Actually, you need neither an observer interface (C# is not Java -- use events),
nor should your model need to extend from any particular class. Your controller
should update the model, the model should expose events to signal state changes,
and your views should subscribe to those model events they care about. See
http://www.codeproject.com/csharp/model_view_controller.asp for an introduction.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top