473,379 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 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 5955
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: pysim | last post by:
Hi, I have a couple of general requests for pointers to python examples and design advice. I'm looking for examples of MVC-based GUI controls done in python (model-view-controller). Also,...
6
by: | last post by:
Hi all, Like to know if any one have implementated any enterprise .Net Solution using MVC Model? Is there any doc (whitepaper) or write up on this? Thanks. regard
6
by: avlee | last post by:
Hello I am planning in my company to move to php.MVC framework - to create several big web applications. What are your opinions of this framework ? Is it the best MVC framework for PHP ? What...
4
by: scott.w.white | last post by:
Looking at different MVC frameworks from many langauges from PHP to Python, I've concluded that the explosion of MVC frameworks is mainly due to undisciplined & unexperienced programmers. Nobody...
1
by: Sean Quinn | last post by:
Hi all, This may have a really simple answer to it, but I can't seem to figure it out no matter how much I think about the problem. I'm working with an MVC framework (presently the most recent...
3
by: -pb- | last post by:
We are developing a Windows client using .NET 2.0 and C#. We have decided to use MVC based architechture due to its obvious advantages. This application shows data from database in a Grid. We want...
2
by: Richard Coltrane | last post by:
Hello, Can anyone point me in the direction of some good introductory resources to MVC and Web. I have looked up several resources on MVC in general (none of which have any code samples) but I...
1
by: shapper | last post by:
Hi, I was checking the new ASP.NET MVC Preview 4 features and I found the following: var dataTokens = new RouteValueDictionary(); dataTokens.Add("namespaces", new HashSet<string>( new string...
13
by: Author | last post by:
I happened to see this asp.net MVC tutorial at http://www.asp.net/learn/mvc/tutorial-01-cs.aspx Indeed as the author says it is gonna look like classic ASP at the very beginning of this...
0
Airslash
by: Airslash | last post by:
Hello, this is my second article that I'm writing for bytes.com, and this time I wish to share some knowledge on how to test a custom ActionFilterAttribute for a controller in the ASP.NET MVC 2...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.