BTI420 notes – Tue Feb 3

Checkpoint. Database-hosted persistent store. Quiz. KPI Survey.

.

Quiz today

Near the end of the class/session, we’ll have a quiz.

The quiz will be ten (10) minutes in length.

You will answer two or three questions, on a single sheet of paper. The questions will cover topics, concepts, definitions, descriptions, and so on. Think about a question-and-answer session in a job interview. Those are the kinds of questions you can expect.

.

Checkpoint – don’t fall behind

Do you understand the concept and use of view model classes? If not, ask!

Do you understand the concept and use of AutoMapper? If not, ask!

Please do not hesitate to ask or visit your professor for help.

.

Topic wrap-up – view model class for an HTML Form

This is your opportunity to complete your understanding of this topic.

.

Using a database server to host the persistent store

Today you will begin using a database server to host a persistent store.

Will it change how you work with objects and collections of objects? No.

Does it change the way we query and manage collections? No.

.

Persistent store components

From the perspective of our web app, there are a small number of components that enable the use of a persistent store. They will be explained below and demonstrated in class.

  1. Design model classes, which describe your entities and associations
  2. Entity Framework, which provides the ‘gateway’ components
  3. SQL Server, which persists your data

.

Add the Entity Framework package to your project

We will use the Microsoft Entity Framework technology in the next several weeks.

Entity Framework enables C# programmers to work with objects, and persist them in a relational database management system.

Another definition:

Entity framework is an object design, management, and persistence framework.

From our perspective, we continue to use objects and collections of objects as though they were in memory. Entity Framework takes care of the persistence part.

.

Getting started

Create a new project. (No authentication!)

In Solution Explorer, right-click your project object.

Select Manage NuGet Packages…

Online, search for ‘entity’, and select the “Entity Framework” result; install.

(Also, while you’re there, install AutoMapper.)

Build/compile.

.

Add a ‘connection string’

Add the following to your Web.config. Locate it just above the <appSettings> element.

<connectionStrings>
 <add
 name="DataContext"
 connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\StoreName.mdf;Initial Catalog=StoreName;Integrated Security=True"
 providerName="System.Data.SqlClient" />
 </connectionStrings>

.

Change “StoreName” to something that will match better to the purpose of your project.

.

Add design model classes

In the Models folder, add a source code file named DesignModelClasses.cs.

Add a single entity class. A later example will have more than one entity class.

Continue the convention that we’ve been using, where each class has an ‘int Id’ property.

Build/compile.

.

Write a ‘data context’ class

At the bottom of the DesignModelClasses.cs source code file (but before the closing namespace brace), add a new class to interface with the data store. (It will perform facade-like services.)

Its class name will be DataContext. This class inherits from System.Data.Entity.DbContext. 

By convention, its default constructor has this signature:


public DataContext() : base("name=DataContext") { }

.

Add DbSet<TEntity> collection properties for each entity class that you wrote above. (There should be only one.)

Build/compile.

.

Optional (but recommended) – write a ‘store initializer’ class

At the bottom of the DesignModelClasses.cs source code file (but before the closing namespace brace), add a new class to interface with the data store. (It will perform facade-like services.)

Its class name will be StoreInitializer. Notice its superclass:


public class StoreInitializer : CreateDatabaseIfNotExists<DataContext>

.

There are two other ‘database initializers’:
DropCreateDatabaseAlways<TContext>
DropCreateDatabaseIfModelChanges<TContext>

Then, implement (override) its “Seed()” method:


protected override void Seed(DataContext context) { }

.

Replace the implementation with your own code that creates objects.

  1. Create two or three new objects
  2. Add each to the appropriate collection in the context object
  3. When finished, ask the context to save your changes

.

Update the MvcApplication class Application_Start() method (in Global.asax.cs) to set the store initializer when the app starts:


System.Data.Entity.Database.SetInitializer(new Models.StoreInitializer());

.

This statement sets (configures, declares) the database initializer class. That class is instantiated and executed when the given DbContext type is used to access a database for the first time.

Build/compile.

.

Write view model classes

Do this in the Controllers folder.

Create a source code file for each design model entity (or logically-related group of design model entities).

Start with two  view model classes – an ‘add new’ class, and a ‘base’ class.

(In the near future, you will add others, which exactly match use cases.)

Important:

After you add each view model class, pause for a moment, and determine whether you need to create an AutoMapper mapping definition. If yes, add it to the Application_Start() method of the MvcApplication class.

.

Write a ‘manager’ class

Do this in the Controllers folder.

It will have a reference to the <projectName>.Models namespace.

As a result, we can use the DataContext class to work with the persistent store. Add a private variable for the data context object:


private DataContext ds = new DataContext();

.

This manager class manages/coordinates/mediates/marshals data operations.

It reads from the persistent store, and returns view model objects.

Or, it accepts view model objects, and writes to the persistent store.

Add members (property, method) to handle ‘get all’ and ‘add new’ use cases.

Build/compile.

.

Add a controller

Do this in the Controllers folder.

Use the second-from-top scaffold template, with read-write actions.

(Do NOT select the one with ‘Entity Framework’ in the name!)

Add a private variable for the manager object:


private Manager m = new Manager();

.

Build/compile.

.

Add a view

From the Index() method

Use the “List” template

Select “<entityName>Base” as the model class.

.

Hands-on

In the hands-on session this week, you will have an opportunity to get started with these topics, by starting Lab 4.

Then, you can continue your work on Lab 4 on your own.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

  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: