BTI420 notes – Wed Mar 12

Security (tentative). Updating your design model, and using Code First Migrations. Working with Internet media types.

.

Security topic continued

(coverage of this topic may or may not happen today)

.

Design model updates, and Code First Migrations

During the lifetime of the programming assignment, you must make some changes to the design model. New properties will be added, maybe some data annotations, and possibly new entity classes.

When you make a change, and then run your app (and view a page in a browser), you will soon see an error message. It told you that the database design has changed, and recommends that you use Code First Migrations.

That’s what you’ll learn now.

Migrations enable you to KEEP YOUR EXISTING DATA when you make minor changes to your design model classes.

It’s a developer technology, but it obviously helps the users, because you can make many changes to your design model classes, and your web app keeps running, with its existing data.

Before you enable and configure Migrations, you must remove the statement in the WebApiApplication class (in Global.asax.cs) that starts/initializes the data store initializer. Why? Because we now want to keep your existing data, and have Migrations manage that process. So, go ahead and remove the statement that starts/initializes the data store initializer.

.

Configuring Code First Migrations

Configuring Migrations requires two simple tasks:

  1. Enable migrations
  2. After a change (or set of changes), add a ‘migration’ definition, and update the data store.

The first task is to ‘enable’ Migrations. In Visual Studio 2013, open the TOOLS menu. Choose ‘Library Package Manager’, then ‘Package Manager Console’. That will open a command-line window/panel.

Type the following command. It’s a once-per-project command. After you do this, you will not have to do it again for the current project:

enable-migrations

This command configures the base or start state of your the app domain data model, and its persistent store implementation.

If you have existing data, and have not made a change to your app domain data model, then you can skip the second task below. Read the package manager console messages carefully – it will tell you the status.

Then, whenever you have made a change to your app domain model classes, you do the second task, by executing the following two commands in the Package Manager Console:

add-migration DescriptiveNameForTheMigrationDefinition 

update-database

In a new (or almost new) project, the descriptive name for your first migration definition can be “Initial” or something similar.

If you anticipate performing many changes, then you can do the add-migration and update-database in a granular way. Or, you could just do all your changes, and run these commands once.

.

Resources and references 

Introduction and overview – Code First Migrations

Migrations commands – EF Migrations Command Reference

.

Working with internet media types

Internet media types includes images, audio, and video. Let’s introduce this topic by working with images (photos, drawings, .jpg, .png, etc.).

You will store images in the persistent data store (i.e. the database).

The data type of an image is a byte array (i.e. byte[]).

On the web, we must know the Internet media type of the image. Therefore, when designing your design model class, you must configure two properties – one for the image bytes (named Content), and one for the Internet media type string (named ContentType).

If you need to handle a large number of images, whether or not they’re associated with other objects in the data model, then create a class that’s dedicated to image storage. That’s the way we will do it in this semester’s programming assignment. 

.

Image upload, introduction

Your app will have to provide a way for the browser/user to “upload” an image. That will be explained below, and requires 1) a controller method, and 2) a manager method.

The HTML <input> element has a type=file attribute. Use that to create a file upload control on your view/page. The uploaded image bytes will be available in your controller method as a byte array. It will also have a string for its Internet media type. Those values can be directly assigned to the properties of a new object.

As a result, a manager class method that handles the creation of a new object doesn’t have to do anything special.

.

Image delivery, introduction

A typical web app design strategy is to create a controller that’s dedicated to image delivery, typically named “ImageController”.

The controller will have a method that delivers an image. You can use the Index() method, but add an int? parameter for the object identifier.

What should the method body do?

First, convert the parameter, from a nullable int, to a regular int:

int lookup = id.GetValueOrDefault();

(If id was not provided, then the method will return zero. This is better than an exception.)

Next, attempt to find/fetch the matching object: Call the manager method (and pass it the id value).

If the result is null (i.e. it did not find a matching object), then:

return HttpNotFound();

Otherwise, return a FileContentResult object. How? Easy. Assume that you have an object “img”, that was successfully returned from the manager class method:

return File(img.Content, img.ContentType);

That’s it. Deceptively simple. The server will prepare an HTTP response as follows:

  • It will create a Content-Type header, and set its value to the img.ContentType value
  • It will then set the response message body to the value of img.Content

.

Using this controller 

So… any time you need an image, you can specify a URL like this:

http://example.com/image/123

For example, if you create an HTML img element, then set the value of its src attribute to the appropriate image.

.

.

.

.

.

.

.

.

.

.

.

.

.

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: