471,319 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

MVC-Pattern in my C# GUI !?!

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
Jan 2 '06 #1
10 5880
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

Jan 2 '06 #2
"Marcel Hug" <ma******************@ATch.abb.com> a écrit dans le message de
news: eB**************@TK2MSFTNGP14.phx.gbl...

| 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
Jan 2 '06 #3
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/mo...controller.asp for an introduction.

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Jan 2 '06 #4
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
"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
| "Marcel Hug" <ma******************@ATch.abb.com> a écrit dans le message
de
| news: eB**************@TK2MSFTNGP14.phx.gbl...
|
|| 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
|
|
Jan 4 '06 #5
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> a écrit dans
le message de news: eL**************@TK2MSFTNGP11.phx.gbl...

| 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
Jan 4 '06 #6
| 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
"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:uy**************@TK2MSFTNGP10.phx.gbl...
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> a écrit
dans
| le message de news: eL**************@TK2MSFTNGP11.phx.gbl...
|
|| 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
|
|
Jan 4 '06 #7
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> a écrit dans
le message de news: Oh**************@TK2MSFTNGP09.phx.gbl...

| 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
Jan 5 '06 #8
| 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
"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:u7**************@TK2MSFTNGP14.phx.gbl...
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> a écrit
dans
| le message de news: Oh**************@TK2MSFTNGP09.phx.gbl...
|
|| 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
|
|
Jan 5 '06 #9
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> a écrit dans
le message de news: uA*************@tk2msftngp13.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 5 '06 #10

Finally a useful reply.

Thank you Jeorg

On Mon, 02 Jan 2006 11:18:55 -0800, Joerg Jooss
<ne********@joergjooss.de> wrote:
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/mo...controller.asp for an introduction.

Cheers,


Jan 5 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by pysim | last post: by
6 posts views Thread by | last post: by
6 posts views Thread by avlee | last post: by
4 posts views Thread by scott.w.white | last post: by
1 post views Thread by Sean Quinn | last post: by
3 posts views Thread by -pb- | last post: by
2 posts views Thread by Richard Coltrane | last post: by
1 post views Thread by shapper | last post: by
13 posts views Thread by Author | last post: by
reply views Thread by rosydwin | 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.