Developing a Web Application with TDD and ASP.NET MVC, Part 3: The MVC Structure

by Sekhat 21. December 2009 12:46

In my previous post we setup our solution. We got the main ASP.NET MVC project created as well as our testing project and we left it in a state to begin coding.

 

In this post however, I’m going to go over what MVC is and the directory structure of the MVC web project.

 

So what is MVC?

MVC is simply a design pattern, the three letters stands for Model View Controller. The MVC pattern tries to address the need to keep concerns separated, as the name suggests, into three pieces, the Model, the View and the Controllers.

 

The Model

The Model is simply your Domain, Business objects, Application Services (the classes that do stuff to your domain, but doesn’t have a place in a single domain object) and it also encompasses your Data Access Layer.

 

The Controllers

The Controllers are classes that contain actions, these actions are synonymous with actions of your website, for example, Your Account controller might have a login action that takes a username and password which would then log someone into the website. The Controllers sit between the view and model, asking questions and issuing orders to the model, and converting information from the model into information that view can use.

 

The Views

The Views are the code and HTML to turn the data passed from the controller into the HTML the browser gets, these are generally dumb and should contain no business logic within them.

 

The ASP.NET MVC Project Structure

imageAs you can see, the ASP.NET MVC project structure contains some similarities with standard ASP.NET Web Application structure. We have the Properties section, which holds the typical AssemblyInfo.cs. We have the App_Data folder which would hold your SQL Express data files. We also have a few others.

 

Content

This folder should hold the pure content parts of your site, in typical usage this would your style sheets and images.

 

Controllers

This folder should hold all your controllers.

 

Models

This folder holds the classes that make up your model. However this only really works if your model is small, most people will separate the model into it’s own assembly, as I will once I begin work on it.

 

Scripts

This folder should hold your client side scripts, such as Javascript scripts and libraries. You’ll find it comes pre-populated with Microsoft Ajax and JQuery.

 

Views

This folder will hold your views, normally sub-foldered into folders with the name of the controller that the views apply to. There is also often a Shared folder that would contain View specific items that are shared between everything, for example, controls and master pages.

 

The views folder also has it’s own Web.config, this is supposed to be there, as it configures some things (mainly validation related things) that should only be applied to your Views.

 

Files in Root

You’ll also notice we have a Default.aspx file in our root structure. This palms off any requests that make it to this page to the MVC handler, this shouldn’t need to be changed.

 

Global.asax contains (by default) the code to register the routes in our application. (More on routes in a later post)

 

And there is also a Web.config which is the main configuration file for the application and would be where you place any application configuration into.

 

Short and sweet, the end to another article

So there you have it, an explanation on MVC and the ASP.NET MVC structure. In the next article, we will get to write our first line of code. I promise.

Comments

12/21/2009 3:07:47 PM #

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        Another Coding Catastrophe | Developing a Web Application with TDD and ASP.NET MVC, Part 3: The MVC Structure
        [anothercodingcatastrophe.co.uk]
        on Topsy.com

topsy.com | Reply

12/22/2009 4:37:36 PM #

pingback

Pingback from 44crosbyrow.com

Another Coding Catastrophe | Developing a Web Application with TDD and ASP.NET MVC, Part 3: The MVC Structure

44crosbyrow.com | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading