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