Sunday, January 27, 2013

Model

ASP.Net MVC Model is the third post in a series of tutorials which introduces the basics of ASP.Net MVC programming.


Previous Posts:
MVC Introduction
HTML Forms



The best way to learn is by doing, so I will run through how to create a class which will be used in the next couple of posts to create an MVC application that retrieves and stores data to a SQL database. If you have used Object Oriented programming techniques before, this will be straight forward.

You will need Visual Studio IDE for the examples. If you don't have Visual Studio, you can download the express version. It has some limitations but is free and is more than sufficient for learning. I will assume no Object Oriented programming experience and C# will be the language used. If you wish to use VB.Net, the developerFusion converter tool maybe useful for converting C# to VB.Net if you are not familiar with both. The code will be straight forward so it should convert without any issues.

Model Concept

The M in MVC stands for Model. The Model is a class just like a class in any other Object Oriented programming language. You can create a class for Person and create instances of person e.g. Michael Earley which can be used to populate forms in a web page (the View).

Object Oriented programming is kind of big for web design as for a long time scripting languages such as ASP and PHP were the main way to control information on the server side and had very limited Object Oriented capabilities and at best it was forced. ASP.Net Web Forms can be programmed using Object Oriented techniques but their are no rules or conventions for how this should be done which an be difficult for web designers and programmers who are new to ASP.Net.

Exercise

Start up Visual Studio (or Visual Studio Express for Web). From the File menu, select New Project.



Select ASP.Net MVC 3 Web Application as the template. When prompted, select Internet Application as the template. Ensure Razor is selected as the view engine.


A basic but functioning MVC project will be created. Before we do anything else, we will change the Default namespace. The reason for this will become apparent shortly. Right click on MvcTutorial1 in the Solution Explorer and select properties. Lets pretend we are creating an Intranet application for a company named Acme. Change the Default namespace to Acme.Intranet.



Again in the Solution Explorer, there will be a number of standard MVC project folders. Models, Views and Controllers are standard MVC folder names. If you expand the Models folder, you should see an AccountModel.cs class file which is created as part of the ASP.Net MVC 3 Web Application template.

Right click on the Models folder and select Add -> Class. Name the class Person.cs. All classes in C# have .cs as the file extension.


Your Person class should look like this:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Acme.Intranet.Models
{
    public class Person
    {
    }
}

The class includes some standard namespaces at the top. The namespace are like containers for similar classes. You could potentially have objects of the same name in separate namespaces e.g. Acme.Person and Horizon.Person but the same class name cannot exist in the same name space. This avoids naming conflicts with classes. A commonly used convention is to use Company.Product as the default namespace for applications. Remember, we named our Default namespace to Acme.Intranet. If we want to use Person i other classes, we can type Acme.Intranet.Person. Normally to avoid having to place Acme.Intranet in front of every declaration of the Person class, you can include a reference to the namespace at the top of the class e.g. using Acme.Intranet.

Our class is named Person. Pascal casing (first word capitalised) is the standard convention for naming classes in C#. Always use meaningful names for classes, don't abbreviate the name and never use plurals. Examples of good class names: Employee, EmployeeType, EmployeeWorksOn, Project.

The Person has no properties yet. Properties are accessible values of a class. Lets add a FirstName, LastName, Age and Height.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Acme.Intranet.Models
{
    public class Person
    {
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public int Age { get; set; }
          public float Height { get; set; }
    }
}

You will notice that each property starts with public. This is an access modifier and if it is set to public, it tells other objects that property is accessible. The other popular options are private and protected. Private allows this class only to access the property whereas protected allows only this class and any sub classes to access the property. For now we will use Public for properties.

The next attribute of a property is the type. FirstName is a string and Age is an integer (or int in C#). It is important to get the type right especially for numbers which can be classified by the number of decimal places or the size of the number. int is suitable for Age as it will be a whole number e.g. 29. I will use float for height as it allows me to store decimal places as I wan to store each persons height in metres e.g. 1.76m.

The property name should be descriptive and unique to the class. Pascal case should also be used for properties. Don't use any spaces in the property names e.g. First Name converts to FirstName.

{ get; set; } is a C# auto-implemented method for getting and setting the properties. The get and set functions can be extended but for now we will keep it simple as all we want to do is get and set the values.

Concepts covered

  1. Namespaces
  2. Object Oriented Programming
  3. Class
  4. Access Modifier
  5. Type
  6. Naming Conventions
  7. Auto-implemented Get and Set properties
Although our class is quite simple, it is sufficient for our first MVC model. The next post will introduce Views.