joi, 25 iunie 2009

Starting a simple ASP.NET Web App

Summary:

In this post we will discuss the tooling that is at our disposal and also some of the benefits or not so beneficial features it brings to the table.

 

Content

 

  • Introduction
  • Tooling
  • Benefits of using it

Introduction

ASP.NET MVC is Microsoft's way of getting on the MVC wagon. It offers a lot more control over the application you're building, no more WebControls ( personally I never and hope to never have to use it ), pure XHTML as W3C would have you doing anyways.

At this moment, and I suspect for years to come, the market of web-apps is under the heavy influence of dynamic languages like Ruby, PHP, Groovy, Python etc. Although scripting languages, a couple of strong frameworks have arisen, from them the most important of all is Rails, a MVC framework that set the bar for all the frameworks to come, so to speak. The other contestants that I find worth mentioning are Joomla (PHP), Grails ( Groovy ), Pylons and Django (Python).

We, the .NET guys, needed something like that for a while, of course there was MonoRails for years, but it didn't get enough publicity. Now there is the Microsoft's solution ASP.NET MVC, and also another project from the OSS ( Open Source Space ) called FubuMVC, wich is an opinionated front-controller style framework. I'll talk about FubuMVC in another post though.

 

Tooling

First there is Visual Studio, and then there is this add-on you have to install from http://www.asp.net/MVC/download/.

Next you have to go to File->New Project. And select from the Web category the Asp.NET MVC Web Application projec type.

 

Create-New-Project

Next you'll be displayed the following screen that let's you decide if you'll be using tests or not

Add-tests-project

For this part of the post it is not needed. Press ok and a sample project is created for you.

Let's have a look at the solution:

Screen solutionYou can see that the content is organized in

 

  • Content
  • Controllers
  • Models
  • Scripts
  • Views
There are three files left out, Default.aspx, Global.asax and Web.config. They are files that the ASP.NET engine expects to find in a web-project, kind'a like Java web-servers expect a certain layout of your project. It is a convention so we'll leave it at that.

In the Content directory you'll put your css files, images, documents and whatever else is considered to be content. The Scripts directory contains javascript files. This is the way they organize by default, you can roll your own, for instance you could have an Assets directory that has everything organized a certain way. There are no conventions so you're free to invent them.

The import part, the C in the MVC pattern, are the Controllers.They direct the flow and supply the models to the views, and that's enough of a responsability.

The nice part, the V in the MVC pattern, are the Views. They display data to the user and accept input through HTML Forms.

The core part, the M in the MVC pattern, are the Models. This layout is mostly inspired by the Domain Driven Design book, Eric Evans wrote a long time ago, but it changed the way many people, myself included, think about design.
I think that the Model part of the application is the most missunderstood part of the hole MVC pattern. The model is, in my vision, composed of the distinct parts: The Domain and The Services. The Domain is composed of entites and value objects, more info on the subject you can find at InfoQ Domain Driven Design Quickly. The Services are the business part of the model.
Don't worry I'll talk a bit more on the subject on the next part of this post.
Tools, tools and tools. If you left click a controller you'll see there's a button ready to generate the controller for you.
add-controller
Next let's create a simple EchoController.
add-controller-screen
Don select the add actions for.. since we'll not use it, just press Add. As you'll se the controller already has a Index method provided. That's a convention set in the Global.asax.cs file, by default the Index method is assumed when accesing a controller. Just to shed some light on the matter let's Debug (F5) the project. Press Ok when it asks you if you're to enable debugging on the Web.config file.

By default the Home/Index action is executed when a request is sent to the http://localhost:{development-port}/. That's because it was registered as the Default controller and action in the Global.asax.cs file. If you want to access the EchoController you'll have to write http://localhost:{development-port}/Echo. That will match the request to the EchoController and the Index action. If you do this you'll se a nice Yellow Screen Of Death.
Why? because we haven't added a view. The convention is that you have define in the Views folder, a Echo folder and add a Index.aspx page. Let's see to go about doing this. There are basically to options, but first stop the debugging ( since this is not a scripting language ).
Option No.1 By adding the folder Echo to the Views directory and Add-> View from the context menu, or Option No.2, right click inside the Index method in the EchoController class. And select Add View from the context menu. That is going to create everything for us,
and it let's us choose what type of view we want ( Details, Create, Edit, List, Empty), that's if we supply a model, but for the sake of simplicity we won't at this time


If you right click on the view, you'll have the option to go to the controller, similar, you can navigate from to the action to the view.
The rest of the tooling support is the standard editing and intellisense you've probably used with .NET previously. The Editor for the aspx pages could use alot of improvements, but that's for VS2010 I suppose.

Benefits of using it

For one you get a much richer experience since you have :

 

  • Friendly URLS
  • Alot more control over the generated XHTML and Javascript ( since you write it )
  • Easier testing of you workflow and services
  • Better separation of concerns
  • A powerfull architecture that allows you to even ditch the hole View-Engine and implement your own, or you could try Spark for a run.
In the next post I'll discuss how to set up a startup project on a simple theme that I've done with Spring MVC a couple of months back.

Niciun comentariu: