15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1260 ❘ ChaPTer 42 Asp.net dynAmic dAtA And mvc<br />

What is mVC<br />

MVC is a way of programming that involves breaking your code into three distinct parts: models, views,<br />

<strong>and</strong> controllers. This is not a web-specific idea, <strong>and</strong> can be applied to any system that involves user<br />

interaction. The definition of these terms is as follows:<br />

➤<br />

➤<br />

➤<br />

Model — A model refers to code that represents the data or state of an application. Note that this<br />

doesn’t include data storage, which if included is the job of, for example, a database. However, it does<br />

encapsulate all manipulation of data, including business logic such as validation rules. It also includes<br />

the code necessary to interact with the underlying data store, if one exists.<br />

View — A view is the user interface that exposes the model to the user <strong>and</strong> allows the user to perform<br />

operations on the model. There is not necessarily a one-to-one relationship between views <strong>and</strong> models;<br />

a given model may be visualized through multiple views. Any changes to models should be reflected<br />

through changes in any associated views.<br />

Controller — Controllers are responsible for mediating between models <strong>and</strong> views. When the user<br />

performs an action on a view, the controller responds to the action <strong>and</strong>, if necessary, forwards the<br />

action on to the model by interacting with it directly.<br />

The relationship between these three parts are shown in Figure 42-8.<br />

The controller has access to both the view <strong>and</strong> the model, as it must<br />

mediate between them. The view must render information from <strong>and</strong><br />

reflect changes in the model, so it also requires access to the model.<br />

There are numerous advantages to programming in this way. First,<br />

<strong>and</strong> most obviously, it provides a clean separation of functionality<br />

figure 42-8<br />

between distinct units of an application. These may even be developed<br />

independently, assuming that well-defined contracts exist between the parts. This means that MVC<br />

applications are perfect in situations where a large team of developers works on a project simultaneously.<br />

Another advantage that follows on from this is that unit testing becomes much easier, as each part can be<br />

unit tested independently when required. Applications with user interfaces are notoriously difficult to test,<br />

as it is often difficult or impossible to design tests that precisely mimic what a user will do, <strong>and</strong> automation<br />

of a user interface can be a major challenge. Since MVC defines exactly what actions a user may perform via<br />

controllers, you can be sure that if those controllers are tested adequately you won’t run into any surprises.<br />

After all, the view can only interact with the controller in the same way as any other client code can interact<br />

with the controller, including test code.<br />

The main disadvantage of MVC is that there is an increase in complexity <strong>and</strong>, potentially, more code to be<br />

written. For small-scale applications, this may not be desirable, <strong>and</strong> it certainly will increase development<br />

time. It may also make it more difficult to make use of all the features provided by the overarching<br />

technology being used for the application. For example, when this technology is used with ASP.<strong>NET</strong>,<br />

as you will see, event-based user interaction <strong>and</strong> view state are not usable.<br />

What is asP.neT mVC<br />

Model<br />

ASP.<strong>NET</strong> MVC applies the principles of MVC to the ASP.<strong>NET</strong> development environment. This means<br />

that ASP.<strong>NET</strong> pages <strong>and</strong> controls are used as views, .<strong>NET</strong> classes are used for controllers, <strong>and</strong> a data<br />

framework such as LINQ to SQL or the ADO.<strong>NET</strong> Entity Framework is used for the model.<br />

You could, if you wanted, stop reading right now <strong>and</strong> start writing MVC web sites using ASP.<strong>NET</strong>.<br />

However, what is particularly exciting with .<strong>NET</strong> 4 is that a full implementation of the MVC pattern is<br />

included for you, with plenty of supporting classes that h<strong>and</strong>le a lot of the plumbing (to connect things) <strong>and</strong><br />

scaffolding (to reduce repetitive or boilerplate code) for you. This is the ASP.<strong>NET</strong> MVC 2 Framework.<br />

The best way to see what is on offer here is to dive straight in <strong>and</strong> build an application (or inspect the<br />

version in the downloadable code for this chapter).<br />

View<br />

Controller<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!