Home > BTI420 ASP.NET dev, Windows > Data persistence choices for ASP.NET MVC web apps

Data persistence choices for ASP.NET MVC web apps

January 27, 2013 Leave a comment Go to comments

This document describes the choices a new ASP.NET MVC programmer has for data persistence.

This document was most recently updated in January 2013.

The intended audience is students in Seneca College’s Software Development degree program.

This is the third in a series of five posts that enable students to get started with ASP.NET MVC web development. The five posts are:

  1. Get ready to create ASP.NET web apps
  2. Creating your first simple ASP.NET MVC web app
  3. Data persistence choices for ASP.NET MVC web apps
  4. Adding HTML Forms to ASP.NET MVC web apps
  5. Designing an ASP.NET MVC web app (version 1)

.

Introduction and context

The web protocol, HTTP, is stateless.

As a result, the programmer must add the ability to maintain state in a web app.

What choices are available for ASP.NET MVC web apps?

  1. TempData
  2. Session State
  3. Application State
  4. Data store

.

In addition, an HTTP cookie can play a role in maintaing state. It is often used as an identifier to retrieve a package of state-related data that’s often persisted in a data store.

.

TempData

TempData is a temporary storage area in the memory space of a web app. It is organized as a dictionary that can hold one or more items. Each item has a string key identifier, and its value can be an object of any type. The backing store for TempData is the Session object.

A typical use for TempData is to pass data from one action method to another action method, via a redirect. After the desired object is read from TempData, the ASP.NET runtime disposes of the object in the TempData dictionary.

You must conclude that TempData provides very temporary storage. You must be careful when you use it, and should confine its use to the typical use case described above.

Assume that you wished to store a string, and a collection, in TempData.

In the “source” action method, add code that is similar to the following:

TempData[“result”] = “Hello, world!”;
TempData[“selections”] = userSelections;
// assuming that userSelections is a List<T>

Now, assume that you wished to fetch a string, and a collection, from TempData. You must cast or convert the object reference to the desired type.

In the “target” action method, add code that is similar to the following:

result = TempData[“result”] as string;
// assuming that result is a string variable
userSelections = TempData[“selections”] as List<string>;
// assuming that userSelections is a List<string>

When reading from TempData, you must check whether the value read is null.

This MSDN Library document is the TempData reference. Posts from Rachel Appel (including a comment from Henrik Stenbaek) and Greg Shackles provided additional insight.

.

Session State

Session State is a storage area in the memory space of a web app. It is organized as a dictionary-like collection that can hold one or more items. Each item has a string key identifier, and its value can be an object of any type.

When a browser user/client accesses a resource in your web app for the first time, the ASP.NET runtime creates a “session”, which is simply a logical grouping of requests from the same browser window/tab during a limited time window.

As suggested above, the lifetime of an object in Session State is limited. By default, it’s twenty minutes. (Therefore, if more than twenty minutes elapses between requests, the old session object expires, and a new one created.)

The syntax for reading from, and writing to, Session State is the same as the syntax used in the TempData discussion above. “Session” is the programmatic property name.

When reading from Session State, you must check whether the value read is null.

This MSDN Library document is an overview of Session State.

.

Application state

Application State is a storage area in the memory space of a web app. It is organized as a dictionary that can hold one or more items. Each item has a string key identifier, and its value can be an object of any type.

Objects in Application State are available to all users of the web app. Application State is a useful place to store small amounts of often-used data that may be shared among the app’s users.

The lifetime of an object in Application State is limited. By default, it’s twenty minutes. (Therefore, if more than twenty minutes elapses from the last request to the app, the app is gracefully shut down by the web server.)

The syntax for reading from, and writing to, Application State is the same as the syntax used in the Session State discussion above. In a controller, “HttpContext.Current.Application” is the programmatic property name.

When reading from Application State, you must check whether the value read is null.

This MSDN Library document is an overview of Application State.

This document is an introduction to state management. This document has recommendations for state management. Please note that these documents were written for ASP.NET Web Forms programmers. Therefore, they include information that does not apply to ASP.NET MVC web apps.

.

Data store

A data store is a data repository on a persistent medium. There are several commonly-used data stores that are used by web app programmers, including:

  • RDBMS – relational database management system
  • File system on the web server
  • Data store services from “cloud” providers
  • Web services

.

Very soon, you will begin using a database as a persistent store.

.

Summary, and what’s next

In this post, you learned about the choices available for data persistence.

We also covered the typical use cases that each choice was suited for. This knowledge is needed to make progress and create web apps that have more functionality.

In the next post, you will add an HTML form to an ASP.NET MVC web app.

.

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: