Solution: Finishing the Comic Book Artists Controller4:36 with James Churchill
Let's review my solution for finishing the comic book artists controller.
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/v2.8 -b solution-finishing-the-comic-book-artists-controller
Welcome back, how did you do with finishing the ComicBookArtistsController? 0:00 Don't worry if you weren't able to finish every task in this exercise. 0:04 No matter how far you were able to get, just the act of trying alone 0:08 will improve your ability to retain what you've learned so far in this course. 0:13 Let's take a look at my solution. 0:17 I start it with updating the ComicBookArtistsController 0:19 with a private field for the database context, and 0:22 instantiated an instance in the default constructor. 0:25 I also over-rode the base classes dispose method, so 0:31 I can dispose of the context instance when the controller is disposed. 0:34 This code is just like the code that we added to the comic books controller. 0:38 So by adding it to this controller, I've repeated myself by duplicating that code. 0:42 We'll see in the next section how we can avoid doing this. 0:48 Now that I had a database context available, 0:51 turned my attention to completing the to-dos in the action methods. 0:54 In the add get action method, I added a query to retrieve the requested comic book 0:58 using the include method to eagerly load the series navigation property. 1:03 I also passed the context into the view model's init method call. 1:07 Doing that immediately broke the build. 1:12 So I went ahead and took a bit of a detour and 1:14 updated the viewMethod.Init method with the parameter for the database context. 1:17 And I added the queries to retrieve the available artist and roles. 1:27 Both ordered by the respective entity name properties. 1:33 Before I could update the other action methods, I needed to take another detour. 1:37 And add a ComicBooKArtists DbSet property to the context class. 1:42 Adding this property makes it possible to query directly for a ComicBookArtists. 1:46 As well as add, update, and delete a CominBookArtists. 1:51 After making that change, I updated the add post action method 1:55 to instantiate a ComicBookArtists entity instance. 1:59 Setting the ComicBookId, ArtistId, and 2:03 RoleId property values to their respective viewModel property values. 2:06 Then I added that entity instance to the ComicBookArtists DBSet property, and 2:11 called the SaveChanges method. 2:16 That takes care of the happy path. 2:18 If the model state is invalid and 2:21 we have validation messages to display back to the user, 2:22 we need to set the viewModel's ComicBook property and call its Init method. 2:25 To set the ComicBook property, I copied the query from the get at action method, 2:33 and I past the database context into the Init method call. 2:38 Which had already been updated earlier with a parameter for the context. 2:43 In the delete get action method I added a query to retrieve the ComicBookArtists. 2:47 This query eagerly loads multiple navigation properties including, 2:52 Artist, Role, ComicBook, and the ComicBook.Series navigation property. 2:57 Noticed I didn't need to add separate include method calls 3:03 with the ComicBook and ComicBook.Series properties. 3:06 As everything in the property path will be automatically included. 3:10 Then in the delete post action method, 3:13 I added the code to delete the ComicBookArtists using the same technique 3:16 that we use in the ComicBook's controller. 3:20 Lastly, I updated the validate ComicBookArtists method with a query 3:22 that uses the any operator to check if there is an existing ComicBookArtists with 3:26 the provided ComicBookId, ArtistId, and RoleId values. 3:31 You may have done things slightly differently from how I did, 3:37 and that's okay. 3:40 Often there's more than one way to implement a feature or solve a problem. 3:42 I'd encourage you to share your solution with me, and 3:46 others on the Treehouse community. 3:49 Let's review what we covered in this section. 3:51 We added a database connection string to our app's configuration file, 3:53 in order to configure the server and database that EF should use. 3:57 And we saw how to configure the database initializer using our apps configuration 4:01 file, which prevented us from making an unnecessary number of calls 4:06 to the database set initializer method. 4:10 We also saw how to manage the context's lifetime 4:13 by aligning it with the controllers lifetime. 4:17 And we implemented the necessary reads, creates, updates, and 4:19 deletes for CcomicBooks. 4:24 In the next section, 4:26 we'll continue to work on the ComicBook Library Manager web app by 4:27 looking at ways we can improve the design and organization of our data access code. 4:31 See you then. 4:35
You need to sign up for Treehouse in order to download course files.Sign up