BTI420 notes – Wed Jan 29

Adding a persistent store to your project.

.

System design guidance

Introduce, hand out, and discuss the diagram

.

Persistent store introduction

Today you will begin using 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

In Solution Explorer, right-click your project object

Select Manage NuGet Packages…

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

Build/compile

.

Add a ‘connection string’

Web.config

<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>

.

Add design model classes

Models folder

Source code file name must be DesignModelClasses.cs

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

Build/compile

.

Write a ‘data context’ class

Models folder

This class inherits from System.Data.Entity.DbContext

By convention, its default constructor has this signature:
public DataContext() : base(“name=DataContext”) { }

Build/compile

.

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

Models folder

Initial ‘starter data’ for the persistent store; 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.)

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

Controllers folder

One source code file per design model entity (or logically-related group of design model entities)

Start with two – an ‘add new’ class, and a ‘base’ class

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

.

Write a ‘manager’ class

Controllers folder

Has a reference to the <projectName>.Models namespace

Therefore, can use the DataContext class to work with the persistent store

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

Build/compile

.

Add a controller

Controllers folder

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

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

Build/compile

.

Add a view

From the Index() method

Use the “List” template

Select “ManufacturerBase” 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.

.

.

.

.

.

.

.

.

.

.

.

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: