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

Search for a consistent set of rules for Models and Views in C#


We frequently keep running into the same set of inconsistent practices
around models and views and would like some other opinions.

1) There is no such thing as a "Controller" in C#. Developers keep
creating them, but in the purist sense they can't exist in C#. The View
and Controller are always one.

2) Exposing the internal structure of the Model to pass its contents to
something else (often another model) is bad. Let's say the model's
internal data structure is an ArrayList (for now). Developers often
will add a property to get this array list out. I argue that you should
add an interface to the Model to get the "Elements" of that ArrayList.
This way if you extend the model you just extend that interface and you
maintain appropriate access to the model rather than some
representation of its internals. It also avoids the whole issue of
returning a reference to the internal versus copying it. Passing a
reference to the ArrayList doesn't scale well as the model develops.

3) Who owns the model? Does the View Own the Model? If so when the View
is destroyed and something wants access to the data in the Model it's
kinda gone. If the Object that created the View is also responsible for
creating the Model this seems to break the encapsulation.

4) Can the Model ever directly reference a View? This gets messy. If we
create a Dialog (that we decide is simple enough that requires no
model) is it legitimate for the Model to call the Dialog. But then we
run into window ownership issues, since the model has no "Window"
and the Dialog wants a parent window. I think it's pretty clear that
the model should not directly reference any of it's "own" views
and only do that through subscribed events.

5) The Busy loop. This is how it usually goes. We add an update event
and any time the model changes we fire it. Then things start to get
complicated and we introduce a BegingUpdate/EndUpdate set of methods
(these control if update events should be fired or not). Then a bunch
of code evolves some using BeginUpdate/EndUpdate and some not. An valid
argument one developer keeps saying is, don't have the UpdateEvent
and then we don't need te BeginUpdate/EndUpdate and the let users of
the model explicitly call FireEvents().

6) Visual Studio Dialog Designer. This thing just seems to get in the
way. It discourages Model/View design and you have to go very out of
your way (and it's way) to have a Model/View based window. There are
many threads that discuss the issue is also the .Net WinForm library is
just poor plain poorly designed.

Nov 17 '05 #1
2 1137
Right...because as we all know, the ONLY way to design a UI is with the MVC.
Thats why FMC was such a raving hit!!!

whats your point?

"mswlogo" wrote:

We frequently keep running into the same set of inconsistent practices
around models and views and would like some other opinions.

1) There is no such thing as a "Controller" in C#. Developers keep
creating them, but in the purist sense they can't exist in C#. The View
and Controller are always one.

2) Exposing the internal structure of the Model to pass its contents to
something else (often another model) is bad. Let's say the model's
internal data structure is an ArrayList (for now). Developers often
will add a property to get this array list out. I argue that you should
add an interface to the Model to get the "Elements" of that ArrayList.
This way if you extend the model you just extend that interface and you
maintain appropriate access to the model rather than some
representation of its internals. It also avoids the whole issue of
returning a reference to the internal versus copying it. Passing a
reference to the ArrayList doesn't scale well as the model develops.

3) Who owns the model? Does the View Own the Model? If so when the View
is destroyed and something wants access to the data in the Model it's
kinda gone. If the Object that created the View is also responsible for
creating the Model this seems to break the encapsulation.

4) Can the Model ever directly reference a View? This gets messy. If we
create a Dialog (that we decide is simple enough that requires no
model) is it legitimate for the Model to call the Dialog. But then we
run into window ownership issues, since the model has no "Window"
and the Dialog wants a parent window. I think it's pretty clear that
the model should not directly reference any of it's "own" views
and only do that through subscribed events.

5) The Busy loop. This is how it usually goes. We add an update event
and any time the model changes we fire it. Then things start to get
complicated and we introduce a BegingUpdate/EndUpdate set of methods
(these control if update events should be fired or not). Then a bunch
of code evolves some using BeginUpdate/EndUpdate and some not. An valid
argument one developer keeps saying is, don't have the UpdateEvent
and then we don't need te BeginUpdate/EndUpdate and the let users of
the model explicitly call FireEvents().

6) Visual Studio Dialog Designer. This thing just seems to get in the
way. It discourages Model/View design and you have to go very out of
your way (and it's way) to have a Model/View based window. There are
many threads that discuss the issue is also the .Net WinForm library is
just poor plain poorly designed.

Nov 17 '05 #2
"mswlogo" <ms*****@hotmail.com> a écrit dans le message de news:
11**********************@g43g2000cwa.googlegroups. com...
1) There is no such thing as a "Controller" in C#. Developers keep
creating them, but in the purist sense they can't exist in C#. The View
and Controller are always one.
We have designed our own MVP (Model View Presenter) framework in Delphi and
are porting it to C# with no problems at all.

The Presenter (Controller) is definitely a viable separate entity and we are
even thinking of making it a RAD component.
2) Exposing the internal structure of the Model to pass its contents to
something else (often another model) is bad. Let's say the model's
internal data structure is an ArrayList (for now). Developers often
will add a property to get this array list out. I argue that you should
add an interface to the Model to get the "Elements" of that ArrayList.
This way if you extend the model you just extend that interface and you
maintain appropriate access to the model rather than some
representation of its internals. It also avoids the whole issue of
returning a reference to the internal versus copying it. Passing a
reference to the ArrayList doesn't scale well as the model develops.
IMO the Model should contain a "Value" which is the real business object.
The Value contains any properties and methods that pertain purely to
behaviour the business object alone. But if there is behaviour that affects
objects related to the Value type, then that behaviour should be a part of
the Model class.
3) Who owns the model? Does the View Own the Model? If so when the View
is destroyed and something wants access to the data in the Model it's
kinda gone. If the Object that created the View is also responsible for
creating the Model this seems to break the encapsulation.
The Presenter (Controller) can own the Model but it is not necessary that
any one class does so. It is the job of the Presenter to either create or
link to a View.

Your application code will usually create an instance of a business class
and then pass that to the constructor of the Presenter; in essence this can
be said to be "presenting the business object".

The Presenter then creates an appropriate Model and passes the BO to its
constructor so that it then becomes the Value of the Model. Nothing takes
ownership of the BO, it is "owned" by the app code that created the
Presenter.

The Presenter also is responsible for creating a View if a whole object is
being edited and a form is required; but it only links the View to the
Value/Model in the case of an edit displaying a property of of an object.
4) Can the Model ever directly reference a View? This gets messy. If we
create a Dialog (that we decide is simple enough that requires no
model) is it legitimate for the Model to call the Dialog. But then we
run into window ownership issues, since the model has no "Window"
and the Dialog wants a parent window. I think it's pretty clear that
the model should not directly reference any of it's "own" views
and only do that through subscribed events.
The Model/Value should never know anything about the UI that is used to
display/interact with it. As I have said the Presenter is the class that
owns both the Model and the View in the case of a form, but only the Model
in the case of an edit control.

You should be using the Observer pattern to link the View to the Model. I
use Interactors to react to the events of the View and translate those
"gestures" into Commands on the Model.
5) The Busy loop. This is how it usually goes. We add an update event
and any time the model changes we fire it. Then things start to get
complicated and we introduce a BegingUpdate/EndUpdate set of methods
(these control if update events should be fired or not). Then a bunch
of code evolves some using BeginUpdate/EndUpdate and some not. An valid
argument one developer keeps saying is, don't have the UpdateEvent
and then we don't need te BeginUpdate/EndUpdate and the let users of
the model explicitly call FireEvents().
The Model and/or the Value should be the Subject in the Observer pattern and
the View should be attached to this Subject. In MVP, the Interactors that
respond to events from the View can be enabled or disabled to avoid any
circular reactions to updates from the Model triggering further reactions in
the Model.

Begin/EndUpdate are useful if several changes in data should be reflected in
the UI to avoid flickering.
6) Visual Studio Dialog Designer. This thing just seems to get in the
way. It discourages Model/View design and you have to go very out of
your way (and it's way) to have a Model/View based window. There are
many threads that discuss the issue is also the .Net WinForm library is
just poor plain poorly designed.


We use the form/dialog designers in Delphi and C# to layout forms and set
the names of controls to equate to the names of the properties that they are
to display. The Presenter links up the edits to the properties when it
creates one sub-presenter for each property.

We have not created a designer for use in the IDE but it is something in the
pipeline and looks to fairly easy to implement. AFAICS, the designer should
represent/edit the Presenter and should take the form of a design surface
that creates non-visual classes.

There are some articles about MVP using Delphi on my website :
www.carterconsulting.org.uk

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #3

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

Similar topics

1
by: Fardude | last post by:
I developed a search stored proc that searches all or some of Procs, Views, Triggers and functions. Would anyone be interested to see it posted here? Do you have any suggestions about other...
0
by: todd | last post by:
here is a search tool SP I wrote. How many times have you wanted to search all your Stored procs or views (in a database) for a keyword but couldn't!? Well now you can! THis can makes life a...
3
by: Saradhi | last post by:
Hi all, Can any one give details about plug-in models in .NET? We have an application which will consists of a different documents, which will be displayed in 2 different tree views based on...
8
by: btober | last post by:
I'm finding that column defaults are not being assigned to nulls when I do an insert by way of a an ON INSERT rule on a view. For example, the following script \set ON_ERROR_STOP ON \c...
0
by: Mujdat Pakkan | last post by:
We have an interesting case where we want to use Postgres both as a database and a front end to a proprietary database. For the latter, we wrote functions that access the proprietary database. Then...
7
by: Nick | last post by:
Hi, I am wondering what people do to achieve a consistent look and feel for their websites? Does anybody here do anything similar to the Composite View design pattern, where a view renderer...
0
by: Ulrich Meis | last post by:
Hi! Situation two schemas: webviews: Contains a set of views with insert rules on them, accessed via a GUI. devconf: "Business logic", all the tables, functions, triggers that I want to...
3
by: linq936 | last post by:
Hi, I always have an impression that for the following semantics: #include <something e.g. #include <stdio.h> Compiler searches it from built-in header search path, on UNIX, it could be...
6
by: paankhate | last post by:
Hi, I have a task at hand to reduce the time taken for search query to execute. The query fetches records which will have to sorted by degrees away from the logged in user. I have a function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.