Preparing Our Plan5:09 with James Churchill
Let's start with reviewing the comic books controller—specifically the `Index`, `Detail`, and `Add` action methods—and put together our plan for updating the action methods to use EF to retrieve data from the database.
To follow along committing your changes to this course, you'll need to fork the dotnet-comic-book-library-manager repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd dotnet-comic-book-library-manager git checkout tags/v1.5 -b preparing-our-plan
CTRL+O- Collapse to Definitions
CTRL+M- Toggle Outlining Expansion
[MUSIC] 0:00 Welcome back. 0:04 In this section we will be updating our web app to retrieve and 0:06 process Comic book data from and to the database. 0:09 As we do that, we'll look at how to configure the context 0:12 database connection string, set the database initializer, 0:15 manage the lifetime of the database context, and more. 0:19 Are you ready to start updating our web app to use EF? 0:22 Let's get started. 0:25 The Comic Book Library Manager Web App has three top level sections. 0:27 Comic Books, Series, and Artists. 0:31 Any of these sections could be used as a starting point for 0:35 adding EF to our web app. 0:38 But let's start with the comic books section, 0:39 that will give us a good jumping off point into our web app. 0:42 It's the heart of our app, and it has enough meat on the bones, so to speak, 0:45 to give us a realistic example. 0:50 We'll start with updating the ComicBooksController, 0:55 specifically the index. 0:58 Detail and Add action methods. 1:02 Each place in the controller class that we need to update 1:09 is marked with a TODO code comment. 1:12 The Index action method needs a query that returns a list of comic books. 1:14 The detailAction method needs a query that returns a single comic book. 1:19 And in the addAction method, the viewModelsInit method 1:28 Needs to query a list of the artists and roles. 1:35 And in the base class, a list of series. 1:43 When we move the EF related classes out of the console lib project, the class library 1:47 project, one of the classes that we moved was a repository class. 1:52 Let's review the contents of that class. 1:57 Press Ctrl+m, Ctrl+o to collapse the class down to its definitions. 1:59 That'll make it easier to see all of the methods in the class at a glance. 2:05 Placing your cursor on a collapsed section and 2:09 pressing Ctrl+m, Ctrl+m will expand that section. 2:12 Pressing Ctrl+m, Ctrl+m a second time will collapse that section again. 2:15 The GetContext() method is responsible for creating an instance of the context class. 2:21 Other methods in this class, 2:26 as we'll see in the next method, call this method to get an instance of the context. 2:28 Notice that the Database.Log property is being set to a simple Lambda expression 2:32 that uses the Debug.WriteLine method to log messages. 2:37 The GetComicBooks method returns a list of comic books. 2:41 And it eagerly loads the related series entity by 2:45 including the series navigation property. 2:48 The GetComicBook method returns a single comic book with the series and 2:55 artists related entities eagerly loaded. 3:00 And a bit further down, here are methods that 3:04 return lists of Series, Artists and Roles. 3:09 Each of this lists is needed by the view model for the AddComicBook page. 3:14 So to recap, the repository class has a method to complete every one 3:19 of the TODOs in the index, detail and add action methods. 3:23 That's great. 3:29 What's not so great is that each method in the repository class 3:30 instantiates it's own instance of the context class. 3:34 Why is this a problem? 3:38 Let's look at an example. 3:40 The addAction method will need to call the GetSeries, GetArtists, 3:47 and GetRoles methods on the repository class in order to get the series, 3:53 artist, and roles data. 3:58 If we expand those methods, we can see that each method makes a call 4:02 to the GetContext() Method, wrapped in a using () statement. 4:06 So three instances of the context class will get instantiated. 4:10 Ideally, each of these queries would use the same context class, especially when 4:14 they're being called from within the same invocation of the add action method. 4:19 In the next section, we'll create an improved version of our repository 4:24 that'll work better with our MVC web app. 4:28 For now, we'll just copy the link queries that we need from the repository class 4:31 to our controller action methods. 4:36 But before we do that, there is some prep work that we need to do. 4:38 Two things are needed in order to ensure that EF is 4:42 properly configured for a web app. 4:45 Configure the server and 4:47 database that EF should use and configure the database initializer. 4:48 We'll start with configuring the server and database. 4:54 There are a number of ways to accomplish this. 4:57 Do you recall the method that allows you to specify the server and 4:59 database via configuration? 5:03 In the next video we'll start with making that change to our web app. 5:06
You need to sign up for Treehouse in order to download course files.Sign up