Reviewing the Completed Repository6:16 with James Churchill
Let's walk through how I resolved each error and completed the repository.
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/v3.3 -b reviewing-the-completed-repository
You can view the completed Repository class on GitHub at:
How did it go? 0:00 Were you able to fix all the remaining build errors? 0:01 Let's walk through how I resolved each error. 0:04 I started with the ComicBooksController and worked my way through each error. 0:06 In the detail action method, I moved the query to retrieve 0:11 a single comicBook to a Repository method named GetComicBook and 0:14 updated the action method to call that method. 0:18 In the add get action method, I updated a call to the viewModels Init method 0:21 by passing in the repository class instance. 0:26 Then I updated the viewModels Init method 0:29 to accept an instance of the Repository class instead of the context class. 0:31 That change cascaded down into the ComicBooksBaseViewModel classes 0:36 Init method. 0:41 I then move the series, artist and roll list queries into their own repository 0:42 methods, and updated the ViewModel Init methods to call those methods. 0:47 Here's the call to repositories GetSeriesList method. 0:52 I'll press alt F12 to peak the definition for the method. 0:57 And in the ComicBook's add ViewModel class, 1:03 the call to the repository's GetArtists method, and 1:07 its definition, and the repository's GetRoles method call and its definition. 1:12 Back in the controllers add PostAction method, I move the code to 1:26 add a comicBook entity to a repository method named, AddComicBook. 1:30 And I updated the call to the view methods Init method by passing the repository 1:39 class instance. 1:44 The query in the EditGetAction method looked 1:45 very similar to the query that I moved to the GetComicBooks repository method. 1:48 The difference being that it didn't eagerly load the related entities. 1:53 Instead of adding another version of the GetComicBook method to the repository 1:57 I updated the existing method to allow the color to specify 2:02 if the related entities should be loaded or not. 2:05 To do that I added a second parameter of type bool named includeRelatedEntities. 2:09 And defaulted the value to true, so 2:15 by default we'll eagerly load the related entities. 2:18 Then I called the AsQueryable method on the DBset property 2:21 to get an iQueryable object which allowed me to incrementally build up the query. 2:25 You can see here that I only call the include method 2:31 if the include related entities parameter is set to true 2:34 when calling this method from the action method. 2:37 I explicitly referenced the includeRelatedEntities parameter so 2:40 anyone reading this code will easily be able to tell 2:44 that we're opting out of eagerly loading the relatedEntities. 2:47 I also updated the call to the viewModel.Init method 2:51 by passing in the Repository class instance. 2:54 For the EditPostAction method, I simply moved the code to update the comicBook 2:57 entity to a repository method named UpdateComicBook. 3:02 And, I updated the call to the Init method 3:07 by passing in the repository class instance. 3:10 The query in the Delete Action method to get a comicBook was also 3:13 very similar to the query for our repository's GetComicBook method. 3:16 The only difference is that it equally loaded just the series navigation property 3:21 and not the artist collection. 3:26 Instead of trying to cover this variation, 3:28 I just decided to call our existing GetComicBook repository method. 3:30 While this will add some additional overhead, when executing the query and 3:35 loading the entities into the context. 3:38 I don't expect users to DeleteComicBooks very often. 3:41 So the performance penalty shouldn't be a concern. 3:44 The Delete Post Action method is another simple example of moving the code 3:47 to delete a comicBook to its own repository method, DeleteComicBook(). 3:51 For the validate comicBook method, I move the validation query to a Repository 3:59 method named ComicBookSeriesHasIssueNumber. 4:04 Moving on to the ComicBookArtistController. 4:14 In the Add Get action method, 4:20 it just needed to replace the query with a call to the repository comicBook method. 4:22 And like I did with the ComicBooksController, I updated the call 4:27 to the ViewModels Init method by passing in the repository class instance. 4:30 Then I updated the view models in a method to accept an instance 4:37 of the repository class instead of the context class. 4:40 And updated the method to call the existing repository methods to get 4:44 the artists and role lists. 4:48 In the Add Post Action method, I move the code 4:58 to add a comicBookArtist to a Repository method named AddComicBookArtist. 5:01 And if the model state is not valid I added a call to get the comicBook from 5:12 the repository. 5:16 And I updated the Init method call to pass send repository class instances I 5:17 moved the query and 5:22 the DeleteGetAction method to a repository method named GetComicBookArtist. 5:23 And in the delete post action method. 5:35 I moved the code to delete a ComicBookArtist to a repository method 5:37 named DeleteComicBookArtist And 5:41 lastly I move the validation query in the validate ComicBookArtist method. 5:48 To a repository method named ComicBook has artist role combination. 5:53 How does my repository class compare to yours? 6:00 As I mentioned in an earlier video, you may have done things slightly differently 6:03 than how I did, and that's okay. 6:06 There's almost always more than one way to implement a feature. 6:09 I'd encourage you to share your solution with me and 6:12 others on the Treehouse Community. 6:14
You need to sign up for Treehouse in order to download course files.Sign up