1.Introducing MVC

The Model-View-Controller pattern is certainly nothing new. It has been around for decades. There have been many variations of it and many articles, book chapters, and blog posts written about it. The original impetus for the pattern was to help intelligently structure an application to handle low-level user input, back when UI controls were quite primitive. As the developer-friendliness and richness of UI controls has improved over time, MVC has taken on broader meaning.

These days MVC generally refers to the separation of concerns wherein you have a Model to represent the logic and data of the problem domain, a View to display the Model on-screen, and a Controller to handle user interaction events and inform the Model when processing needs to occur. The Controller is interested in user interaction; such as keyboard, mouse, or stylus input; for an entire form or user control…not just one button or dropdown.

 

 

Now that I have regurgitated a generic explanation of MVC, let us see what it really means in practice. Previously we saw how loosely coupling application logic to the UI has many desirable advantages. What we did not review was how to implement that loose coupling. As you have probably guessed by now, the MVC pattern is a fantastic way to do it.

The essence of a loosely coupled system boils down to one (bizarre) term that I coined: referencelessness. A loosely coupled system does not have unnecessary object references directly connecting its major functional components. Where there are direct references between parts of the system, they should often be expressed as interfaces or abstract base classes, to allow for dependency injection and object mocking (more on that later). The only places in the system where it makes sense to have a direct reference between two components is where the Controller references the Model, and possibly between the Controller and View in certain situations. As we examine how the demo application uses MVC, the reasoning behind these statements will become apparent.

2.My Demo

 In my demo application, I write one application to make it clear. Assuming we have a table, as it is shown in the following.

Student Information Table

 

(Field)

(Type)

StuIdPrimary Key

Int

StuNo

Char(8)

StuName

Varchar(16)

Sex

Char(1)

Brithday

Datetime

Address

Varchar(64)

Telphone

Varchar(16)

Remark

Varchar(128)

 

We want to operate this Student Information Table with one application based MVC pattern. It can add a record, modify a record, delete a record, and show all the record.   

I write the Model Layer like this. It’s very easy, isn’t it? :-)

 

Code

 

And then I write the Controller Layer like this.

 

Code

 

 

Then I write some code to test this Controller Layer, of course, I use the Nunit Test to do this.

 

Code

 

 

At last, I finish the View Layer. It’s UI like this.

 

 

And the form of adding and modifying students’ information is as this.

posted on 2008-10-09 13:44  ehyea  阅读(251)  评论(0编辑  收藏  举报