2,99 €
This title is one of the "Essentials" IT Books published by TechNet Publications Limited.
This Book is a very helpful practical guide for beginners in the topic , which can be used as a learning material for students pursuing their studies in undergraduate and graduate levels in universities and colleges and those who want to learn the topic via a short and complete resource.
We hope you find this book useful in shaping your future career.
This book will be available soon...
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Veröffentlichungsjahr: 2016
Chapter 1 Conceptual Overview
MVC is Microsoft’s latest offering for designing and building web applications, and it is also an architectural design pattern. As a design pattern, MVC and its variant patterns have been around for some time but the framework is still relatively new, giving it the advantage of having learned from earlier frameworks. If you are familiar with other web development frameworks, then you can trace the history of key features and see how common problems are avoided. However, this book is not intended to be a history lesson. For the present, be aware that MVC incorporates lessons learned from many systems and is not in itself "a thing".
The Design Pattern
To better understand the framework, we need to understand the design pattern on which it is based. MVC and its variant patterns (MVP, Passive View, etc.) separate a web app into three distinct areas: the model, the view, and the controller. The model is the data that the app uses, the view is the user interface (UI), and the controller houses most of the app’s logic which ties the three pieces together.
Figure 1: Components in the MVC pattern
The difference between the MVC, MVP, Passive View, and other design patterns is a matter of how much responsibility you give the various components. These are subtle differences that we can safely ignore for now.
To effectively use MVC, adhere to the following best practices:
The model should be a simple object with read and write properties to support a single view.The view should focus on standards-based markup.Logic in the view should be limited to user interaction and not include business logic.Controllers should not know anything about how the data in the model is manipulated in the view.Controllers should not know anything about how the data is persisted beyond the model.Following these guiding principles will help ensure that we stay out of trouble.
A Brief Discussion on Web Forms
If you are already familiar with developing web apps with Web Forms, MVC will be a paradigm shift for you. To fully appreciate the power and appeal of the framework, let’s review some of the limitations of Web Forms and point out some of the advantages of MVC.
Many of these limitations become less relevant with each new release of Web Forms and can be mitigated further with best practices. But the following remain the common problems plaguing many Web Forms apps:
View stateBloated HTMLPage life cycleAwkward URLsTestabilityNote: You will find it much easier to integrate client-side functionality to a controller than to a Web Form’s code-behind. Actions in controllers make natural endpoints for AJAX calls. We can easily build up the URL for the endpoint. We do not have to struggle to identify and track the IDs of HTML components. View models, along with the model binder (which we will soon explore), make it easy to translate data back and forth.
MVC builds on the stateless nature of the web. In many Web Forms apps, app state is maintained in server-side code with view state required to remind the server of everything that has happened so far. Getting rid of view state eliminates a lot of what is passed back and forth between the server and browser. This alone makes pages load faster.
While view state accounts for most of the bloat in the markup, we are able reduce it further with a stronger focus on standards-compliant markup. With Web Forms’ focus on server controls, developers often had little control over the markup that was ultimately sent to the browser. The situation has improved with each release of the framework, but developers still relinquish substantial control of the generated markup. With MVC, developers still may not have complete control, but the functionality they are now provided is more focused on producing clean, standards-compliant markup. With Web Forms, the developer has a large volume of work to perform to get clean markup. With MVC, the developer has to work hard to avoid producing clean markup—a transition that turns the workflow on its head.
Note: Standards-compliant markup makes it easier to integrate with client-side libraries such as jQuery, Knockout, and AngularJS.
The page life cycle has been abandoned altogether in MVC. Each request is truly stateless. State is more easily maintained in the page than on the server. Each page request has an associated action; the logic executed during the request can be traced from this one method. This simplifies troubleshooting and maintenance. You do not have to follow through the events of the page life cycle to track down what logic is being run.
For many, the most important innovation is the improved testability. It is difficult to build automated unit tests against a Web Form, but it is relatively straightforward to build test cases against the logic in the controller. A Web Form is riddled with complex UI elements such as text boxes, as well as Session, HTTPContext, and much more. In MVC, controllers deal with view models and similar objects that can be mocked with relative ease to simplify testing.
Tip: You cannot access the Document Object Model (DOM) from the controller, but you could access anything else exposed in the Request object. Don't be tempted. Let the framework handle manipulating query strings, forms collections, etc. You should focus only on manipulating the model.
More Than Just a Pattern
The MVC framework is more than just a pattern. MVC apps implement the MVC design pattern because Microsoft has brought more to the table than simply helping developers maintain good practices. The framework does a lot of the heavy lifting for us, making it easier to follow good coding standards and build robust web apps.
Throughout this book, we will explore:
View enginesModel bindersDisplay and editor templatesHTML helpersFiltersView engines make it easier to produce the HTML we need. Razor, the default view engine in ASP.NET MVC, has a streamlined syntax injection model that allows developers to use a template-driven approach, which makes it easier to create a consistent look and feel for your site. Model binders handle taking data the user entered in the view and repopulating the model. Not only do they handle automatically pulling data in from the view, they will also run any validations associated with the model, which will free you up to work on more pressing matters such as business logic. We will explore all things validation-related in Chapter 4.
Editor templates take the separation of concerns a step further. As we will soon see, editor templates allow us to simply state that we want an editor for a particular property without getting bogged down specifying the form that the editor should take.
HTML helpers are a great compromise between a drag-and-drop designer approach and the tedium of writing all of the HTML by hand. Anyone who has trusted a drag-and-drop approach to get everything right has been burned, but having to write all of the markup by hand is tedious and potentially error-prone. With HTML helpers, we get simple methods that we can call to handle much of the heavy lifting, without sacrificing any of the control. We can also easily write our own helpers to cover any scenarios missed by the built-in helpers.
Filters are attributes that we can add to actions or the controller as a whole. Filters have logic associated with them. All we need to do to get this logic or new behavior is to add the appropriate filter to an action.
Don’t worry, you can build effective MVC apps long before you understand these concepts. But, as you learn more about these concepts, you will discover how the framework can do more of our work for us.
Summary
MVC is both a design pattern and a framework for building web apps following this pattern. The framework builds on what has worked well with other frameworks and manages to avoid some common pitfalls that have plagued others.
Chapter 2 MVC Says Hello World
When you create a new MVC web app in Visual Studio, you will have a functional web app out of the box. Visual Studio will create the directory structure that the framework expects and will provide default content for key files based on templates that we will explore in Chapter 6. This will not be a useful app, but it does have all of the pieces in place to run. Let’s get started.
The screenshots in this book are taken from Visual Studio 2013. I am also using the 2012-compatible templates because I believe this gives you a better breakdown of your template options. Other versions of Visual Studio may look a bit different.
Our First Application
In Visual Studio, create a new ASP.NET MVC 4 Web Application and give it a meaningful name.
Figure 2: Creating the initial app
You will then be prompted for a few other details about this new app.
We will be using the Razor view engine, so keep that option selected. We will more thoroughly discuss view engines later.
Choosing a template is a bit tougher and depends on what you are trying to do. In most cases, your choice will boil down to deciding between an Internet app and an intranet app.
If you select an empty template, you can still easily build an Internet, intranet, mobile, or any other type of app. Adding a web API is easy, as is building a single-page app. The only difference is how much work is already done for you and how that work was done. For this book, we will use the Internet Application template. This means that it will include an AccountController and supporting views to handle authentication as well as password resets and changes. If you start with an intranet app, the resulting app will not include the AccountController but would, instead, rely on Windows authentication to handle authentication and authorization.
Figure 3: Selecting the Internet Application template
The templates provided are just that: they give you a starting point to build on.
Note: Because testability is a big deal in MVC, you are prompted to create a unit test project as well. If you set this up, you can use a unit testing tool to test all of the business logic in your controller. This is a good practice to follow but outside the scope of this book.
Once Visual Studio is finished creating our initial project, it will look similar to the following screenshot.
Figure 4: Solution Explorer for new MVC project
At this point, we have a functional MVC web app. Press F5 to run it. You should see something similar to the following in your web browser.