BTI420 notes – Wed Jan 8

Welcome to our first session together in BTI420.

Your professors are:

  • Dr. Elliott Coleshill, section A
  • Peter McIntyre, section B

.

How to use these notes

Every class/session will have notes posted on this web site.

All notes will be linked from the index page: petermcintyre.com/bti420/notes

Study this page’s URI to learn the naming convention for a specific notes page.

Your professors plan to post the notes at least two days before the class/session begins. Before you come into a class, we expect that you have read/skimmed/studied/contemplated the topics covered in the notes.

The format and style of the notes pages will vary. At times, they will be terse, with headings and keywords that are intended to guide the student through the topics. At other times, they will be lengthy, with narrative that explains and supports the topics. Expect a full range of formats and styles between these extremes.

Class/sessions are important. This is not a distance education (online) course. The notes do not attempt to capture everything that must be communicated in the course of learning a topic.

.

Topic coverage today

Presented in point form:

Segue from BTI320 topics

BTI420 course overview

Introducing the development environment:

  • Visual Studio 2013
  • Web browsers
  • HTTP inspector (e.g. web-sniffer.net, Fiddler app)

Web app development overview and context

Introduction to ASP.NET MVC

Get started with the C# language and the .NET Framework

Writing code, writing classes

Modelling data entities with classes

The role of a view model class

Getting started with string-number conversions

Introduction to debugging

.

Web app development overview and context

In the last decade or so, two web app dev frameworks have dominated:

Other than these two, you’ll see some Ruby on Rails and Java solutions (e.g. Spring) out there. What about the future? Who knows… the whole landscape may get upset by something, maybe Node.js.

The Model-View-Controller (MVC) software design pattern is now the de facto development approach for web apps.

Each framework – ASP.NET and PHP – offer the ability to use the MVC pattern.

.

Introduction to ASP.NET MVC

ASP.NET is a programming and runtime environment for web apps that run on the Microsoft Web Platform.

ASP.NET MVC is a framework for building web apps that conform to the model-view-controller (MVC) design pattern.

The two main things that new devs need to know about ASP.NET MVC:

  1. The front controller pattern, in that a URL does not reference a file system resource
  2. The app dev process relies on programming conventions

If you have trouble learning both of these, you will not be successful in this course.

How does the front controller pattern work?

  1. When you request a resource in an ASP.NET MVC app, the ASP.NET runtime receives the request
  2. The URL is inspected, because it will determine what happens next
  3. The request is routed to a specific method in a specific “controller” class
  4. The method generates some data, and passes it to a “view” for rendering

Regarding programming conventions, the HomeController is the web app’s entry point. The URI to methods in HomeController use the /home/ path.

.

What is an ASP.NET application

The following was adapted from this MSDN reference document:

An ASP.NET application is the collection of all source code files and resources (e.g. images) within a specific directory, and its subdirectories, on a single web server.

The first time a browser/client requests any URL in an ASP.NET application, the web server creates an instance of an HttpApplicationState class. A reference to this instance is available within the app via the ApplicationInstance property. The lifetime of the instance is twenty (20) minutes. The lifetime is reset with each request to a URL in the app.

The Global.asax.cs source code file defines the “MvcApplication” class, a subclass of System.Web.HttpApplication. You can add code to this class, to (for example) initialize software components and data for the app, and to customize behaviour when servicing a request. While in this class:

  • The ApplicationInstance property refers to the app’s code and execution environment
  • The Application property refers to the state of the app, including its data

You will typically use the Application property most often. In other source code files, you must get a reference to the application, using System.Web.HttpContext.Application. (The System.Web namespace is always added in a using directive, so we can simply use “HttpContext.Application”.)

.

What is an application domain (aka problem domain)?

We will begin using the phrase “application domain” (often shortened to “app domain”) in this course. An app domain is the area of knowledge that is implemented in a software application.

In an ASP.NET MVC application, an app domain includes some standard software components, such as models, views, and controllers. In addition, it will include software that implements business logic and rules, workflow, and persistence management.

Wikipedia has useful articles on problem domaindomain model, and conceptual model.

.

MVC – what is a model?

A model holds and manages your app’s data.

We write classes that model the data.

Data that’s persisted in a store is modelled by app domain model classes.

Data that is delivered to the user, or gathered from the user, is modelled by view model classes.

.

MVC – what is a view?

A view is a source code file that contains user interface code.

The user interface target is a web browser, so a view contains HTML markup and code expressions that place data into the markup.

.

MVC – what is a controller?

A controller is a class that services requests from users.

An incoming request from a user is routed to a specific method in the controller.

The method generates some data (from the model), and passes the data to the view for rendering.

.

Get started with the C# language and the .NET Framework

Please read these brief documents:

Introduction to the .NET Framework

Introduction to C#

Learning C# – The top ten list

.

Writing code, writing classes

You will write many classes in a web app.

The class is the most fundamental container, or building block, for your code.

You have already seen that a controller is a class.

You will also use classes to define data objects (i.e. classes that are things), as well as objects with behaviour (i.e. classes that do things), or both data and behaviour.

Code for a class is placed within a namespace. A class will include members. Common members include:

  • Private fields, not visible outside the class
  • Constructors, for creating a new instance of the class
  • Properties, for holding publicly-available data
  • Methods, public or private, for behaviour

And, there are a few other kinds of members that can be added.

Do not nest a class inside another class.

.

Modelling data entities with classes

Modelling data entities with classes is a simple task, as it is in other languages and frameworks.

A model class needs properties, maybe one or more methods, and maybe one or more constructors.

.

The role of a view model class

We need to talk about view model classes. This is an important topic.

A view model class takes on the exact shape of the data you wish to work with in a view (as defined in the model-view-controller architecture).

A view model class can define the data that is delivered to a user.

Or, it can define the data that is gathered from a user.

.

What are view model classes? Why have them?

As noted above, a view model class takes on the exact shape of the data you wish to work with in a view.

The properties in a view model class are based on the properties in a design model class.

Our goal is to prevent design model classes from being visible and accessible in controllers and views. Instead, in controllers and views, we will use view model classes. This implies that we must map data between design model classes, and view model classes.

This will help to implement the “separation of concerns” principle.

.

How to create view model classes

View model classes, for this BTI420 course, should be written in the Controllers folder.

We suggest that you create a separate source code file to hold the view model classes needed for each controller. For example, assume that you have a “Suppliers and Products” domain model. Therefore, you should create a source code file named “Supplier_vm.cs”, and another named “Product_vm.cs”.

What classes do you write? The answer is totally and completely dependent upon your app’s use cases. However, there are some common and obvious use case patterns. Follow these suggestions as you write view model classes for your first few apps.

As just stated above, assume a “Suppliers and Products” domain model. A supplier can have zero or more products, and a product is always linked to one supplier.

Notice the use of the entity’s name in the classes. Notice also the inheritance chain.

List – a class that can be used in the user interface as a simple lookup list (e.g. drop-down list)

  • public class ProductList
  • For all kinds of users
  • Include the Id property, and one or two descriptive properties

Add – a class that can be used by public users

  • public class ProductAdd
  • For all kinds of users
  • To be used for add-new tasks
  • Therefore, include properties that must be present in an add-new task
  • Do NOT include the Id property

Base – for get-some and get-one tasks

  • public class ProductBase : ProductAdd
  • For public or trusted users
  • Adds the Id property to the ‘…Add’ class (and any other useful/required properties)

Edit – for update-item tasks

  • public class ProductEdit : ProductBase
  • For partially-trusted users
  • Includes all (most) properties that you allow to be edited

Full – to deliver all (most) properties of an object

  • public class ProductFull : ProductEdit
  • For trusted users
  • Includes all (most) properties that you allow to be viewed or edited

You do not have to write all these classes.

Conversely, you may need more classes to handle some use cases. Don’t be afraid of that.

.

Getting started with string-number conversions

The String class is often used. It is a reference type, and is immutable. However, the editor, compiler, and runtime make it easy to work with strings. Declaring a string is simple, and does not require the “new” keyword (unlike the declarations for other reference types):

string myName = “Peter”;

And, although a string is immutable after it has been created, the compiler and runtime enable you to mutate (change) an existing string’s value by using a natural and comfortable syntax. Using the myName instance from above, let’s change it:

myName = “Prof. Peter McIntyre”;

Numbers in the .NET Framework are typically value types. Many numeric types are available, but we typically use int and double in C#.

.

Conversions

Why do you care about converting between strings and numbers?

Well, think about it: What data type is HTML, which appears in a browser? Text (string).

Therefore, if you are “round-tripping” numbers to and from a web page, you must convert them to and from strings.

You can “convert” a number to a string with the number’s “ToString()” method:

string foo = 123.ToString();

Some number types have ToString() overloads, which permit you to specify a preset (common) or custom number format string.

If a string contains or holds a numeric value, there is a System.Convert class, which has methods that help. For example:

// The “Convert” class is in the local scope, because “using System;” is always present at the top of the source code file
int bar = Convert.ToInt32(“123”);

This is an unsafe conversion, because it’s possible that the string cannot be converted. If that’s the situation, this statement will cause an exception (a runtime error). A better approach is to use the Int32.TryParse() method:

// Create a temporary variable
int foo;
// Attempt to convert…
bool isNumber = Int32.TryParse(“123”, out foo);

The return result of TryParse() is a bool.

If the conversion is successful:

  1. The return result is “true”, and
  2. The variable “foo” holds the converted string-to-number

If it’s not successful, the return result is false, and foo is zero.

There is a Double.TryParse() method that you can use for double values.

Back to strings for a moment… How can you determine whether a string variable is null or empty? Use this test:

// Assume that “stringVariable” may or may not hold some content…
bool isProblem = string.IsNullOrEmpty(stringVariable);

.

.

.

.

.

.

.

.

.

.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: