473,324 Members | 2,313 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,324 software developers and data experts.

MVC pattern doubts

Hi,

If I am correct the MVC basics are:

The primary goal is to decouple the model from the views and controller.

- Model: Holds data and business logic.
- View: represents data and queries the model.
- Controller: Responds to user input, sends change notifications to the
model and to the views.

When the user clicks to open an edit view the controller instantiates
it. If the user changes the view's data, then the controller will pick
the data and notify it to the model. Then the controller will notify all
it's registered views that the model has changed and the views will
query the model.

¿Why don't the view notify the model directly since it holds a reference
to it?
¿What happens if the edit view needs to validate some user input data
before closing, for example that the code the user has just entered is
not repeated in the database? ¿wouldn't be more convenient if the view
queries directly the model? After some "googling" my impression is that
this is not recomended, but ¿how should it be done then? ¿is it
necessary the trip from the view to the controller and from the
controller to the model, even though the view holds a reference to the
model directly?
What I have in mind to do in my c# application is, using delegates and
events:

- The controller creates and registers the views and then wires itself
to the user interface notification events that the views raise. The
model also wires the views to the model's Changed event.

- The views hold a reference to the model and can query it or call its
"store this entity" method directly. If there is for example a check
needed to be done throug the model, for example if a value that the user
has entered is repeated, it can also do it directly. When they recive
the "ModelChanged" event they update themselves querying the model.

- The model is decoupled from de view and the controller, therefore it
can be tested separately. It has some standard public functions to be
queried and to store entities. It also raises a 'ModelChanged' evend
¿Is this correct? ¿I am misunderstanding something?

sorry for the long post and thank you very much in advance,
Santi
Nov 16 '05 #1
4 1876
Ben
Before making a lot of effort... Take a look at Microsoft.ApplicationBlock.UIProcess.
It's a very nice base for creating a MVC-like application.

Gr,
Cybenny
Hello Santi,
Hi,

If I am correct the MVC basics are:

The primary goal is to decouple the model from the views and
controller.

- Model: Holds data and business logic.
- View: represents data and queries the model.
- Controller: Responds to user input, sends change notifications to
the
model and to the views.
When the user clicks to open an edit view the controller instantiates
it. If the user changes the view's data, then the controller will pick
the data and notify it to the model. Then the controller will notify
all it's registered views that the model has changed and the views
will query the model.

zWhy don't the view notify the model directly since it holds a
reference
to it?
zWhat happens if the edit view needs to validate some user input data
before closing, for example that the code the user has just entered is
not repeated in the database? zwouldn't be more convenient if the view
queries directly the model? After some "googling" my impression is
that
this is not recomended, but zhow should it be done then? zis it
necessary the trip from the view to the controller and from the
controller to the model, even though the view holds a reference to the
model directly?
What I have in mind to do in my c# application is, using delegates and
events:

- The controller creates and registers the views and then wires itself
to the user interface notification events that the views raise. The
model also wires the views to the model's Changed event.

- The views hold a reference to the model and can query it or call its
"store this entity" method directly. If there is for example a check
needed to be done throug the model, for example if a value that the
user has entered is repeated, it can also do it directly. When they
recive the "ModelChanged" event they update themselves querying the
model.

- The model is decoupled from de view and the controller, therefore it
can be tested separately. It has some standard public functions to be
queried and to store entities. It also raises a 'ModelChanged' evend

zIs this correct? zI am misunderstanding something?

sorry for the long post and thank you very much in advance, Santi

Nov 16 '05 #2
Santi... Sometimes seeing actual working code is helpful. Here is some
actual
MVC code in a framework designed as MVC. In this case I am doing
everything through the controller so that the controller knows about the
views and the model. The views and model know nothing of each other.
For
instance textViewQuery is in the View and model is the Model.

- (IBAction)doExecuteScalar:(id)sender {
[textViewQuery setString:@""];
[textViewQuery setString:[model scalarQuery:[textFieldSQL
stringValue]]];}

// Controller v0.15

#import <Cocoa/Cocoa.h>
#import "Model.h"

@interface Controller : NSObject
{
IBOutlet id buttonExecuteToString;
IBOutlet id buttonExecuteTableView;
IBOutlet id buttonExecuteScalar;
IBOutlet id buttonExecuteNonQuery;
IBOutlet id model;
IBOutlet id buttonClear;
IBOutlet id textViewQuery;
IBOutlet id textFieldURL;
IBOutlet id textFieldUserID;
IBOutlet id textFieldPW;
IBOutlet id textFieldDatabaseName;
IBOutlet id textFieldSQL;
IBOutlet id textViewProperties;
IBOutlet id tableView;
IBOutlet id dataSource;
IBOutlet id textFieldSQL1;
IBOutlet id textFieldSQL2;
IBOutlet id textFieldSQL3;
IBOutlet id textFieldSQL4;
IBOutlet id textViewTransaction;
}
- (IBAction)doExecuteStringAction:(id)sender;
- (IBAction)doExecuteTableViewAction:(id)sender;
- (IBAction)doClearAction:(id)sender;
- (IBAction)doConnectAction:(id)sender;
- (IBAction)doDisconnectAction:(id)sender;
- (IBAction)doExecuteScalar:(id)sender;
- (IBAction)doExecuteNonQuery:(id)sender;
- (IBAction)doAutoCommitOn:(id)sender;
- (IBAction)doAutoCommitOff:(id)sender;
- (IBAction)doBeginTransaction:(id)sender;
- (IBAction)doExecuteTransaction:(id)sender;
@end

In C#, I tend to think in M-VC or ENGINE-INTERFACE.

Regards,
Jeff
- Model: Holds data and business logic.
- View: represents data and queries the model.
- Controller: Responds to user input, sends change notifications to the
model and to the views.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
Santi wrote:
Hi,

If I am correct the MVC basics are:

The primary goal is to decouple the model from the views and
controller.

- Model: Holds data and business logic.
Holds data yes. Business logic? Possible, but not mandatory. Depending
on the application architecture, the model could consist of mere value
objects (or data transfer objects or view beans...).
- View: represents data and queries the model.
The view doesn't query the model. It receives events from the model --
it's a "push", not a "pull".
- Controller: Responds to user input, sends change notifications to
the model and to the views.

When the user clicks to open an edit view the controller instantiates
it. If the user changes the view's data, then the controller will pick
the data and notify it to the model. Then the controller will notify
all it's registered views that the model has changed and the views
will query the model.
In clasic MVC, the model publishes changes to the views directly. The
controller isn't involved.
Why don't the view notify the model directly since it holds a
reference to it?
See above -- it's the other way around:

Controller --Update()--> Model
Model --OnStateChanged()--> View
What happens if the edit view needs to validate some user input data
before closing, for example that the code the user has just entered is
not repeated in the database?
That would be plain wrong. Views render content. They don't perform any
actions. Controllers perform input validation.
wouldn't be more convenient if the view
queries directly the model? After some "googling" my impression is
that this is not recomended, but how should it be done then? is it
necessary the trip from the view to the controller and from the
controller to the model, even though the view holds a reference to the
model directly?

What I have in mind to do in my c# application is, using delegates and
events:

- The controller creates and registers the views and then wires itself
to the user interface notification events that the views raise. The
model also wires the views to the model's Changed event.
I guess you mean the right thing, but the model doesn't know any views.
Views register for for the model's state change event.
- The views hold a reference to the model and can query it or call its
"store this entity" method directly.


No. See above.

[...]

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 16 '05 #4
Thank you Joerg, I understand now how it should be done, but I still
have a doubt about the relation between the views and the model.
What happens if the edit view needs to validate some user input data
before closing, for example that the code the user has just entered is
not repeated in the database?
That would be plain wrong. Views render content. They don't perform any
actions. Controllers perform input validation.

In a List/Detail application the Detail form is a view, right? What is
the advantage of the controller picking the input information from the
detail form and sending it to the model over the detail sending the data
directly to the model, assuming that every view holds a reference to the
model.
The view doesn't query the model. It receives events from the model --
it's a "push", not a "pull".


Is it wrong to hold the views a reference to the model? The model will
still not know about the views. I thought that the model sended a
OnStateChanged to the views and then the views in response queried the
model because they held a reference to it.
regards,
Santi
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just...
0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
0
by: abbas reji | last post by:
--0-599929911-1059996886=:4358 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Content-Id: Content-Disposition: inline ...
4
by: eric | last post by:
Greetings, I am looking for C++ implementations of a pattern similar to the following description. This pattern is a wrapper of sorts (in the general sense, not like a Decorator) for a single...
6
by: ritesh | last post by:
Hi, I have been reading some text on C and C++ (i.e advanced books). One of the books mentioned that C++ requires a runtime support whereas C does not - what the author was trying to say was...
1
by: NagaKiran | last post by:
Hi I want to post VBA related doubts. Where can I post my doubts in VBA? thanks bye
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
11
by: td0g03 | last post by:
Hello, I just have a few questions. The first one be how would you print a pattern. I could use the if else, but I remember my teacher talking about something like for(i=1;i<=size;i) ...
1
by: halekio | last post by:
Hi all, Please bear with me as I've only started programming in C# 2 weeks ago and this is my first contact with OOP. I ran into a situation where I needed to catch an event in an object that...
16
by: vizzz | last post by:
Hi there, i need to find an hex pattern like 0x650A1010 in a binary file. i can make a small algorithm that fetch all the file for the match, but this file is huge, and i'm scared about...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.