Solution: Finishing the "Series" Section6:13 with James Churchill
Let's review my solution for finishing the "Series" section of the web app.
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/v4.2 -b solution-finishing-the-series-section
Welcome back. 0:00 So, how did you do? 0:01 Were you able to complete the series section? 0:02 Let's take a look at my solution. 0:05 I decided to create an entity type focus repository for the series entity type. 0:07 So, the first thing that I did was add a series repository class 0:13 to the class library's data folder. 0:17 I inherited from the base repository class and specified the series entity type for 0:19 the entity type generic parameter. 0:24 Then, I added a constructor in order to get a reference to the data base context, 0:26 and implemented the get and git list abstract methods. 0:31 In the git method, 0:35 I called the DB set AsQueryable method to get a reference to an I queryable object. 0:36 in order to eagerly load the comic book's navigation collection property, 0:42 if the include related entities parameter is set to true. 0:46 And in the GetList method, I simply called the ToList method on the context of series 0:52 DBSet property, ordering the list by the series title property. 0:57 I also added a SeriesHasTitle method that uses the any operator 1:02 to check if there's a series with a different ID, but the same title. 1:06 Once I had the repository completed I turned my attention to the series 1:12 controller class. 1:16 I started by updating the controller to inherit from our BaseController class. 1:22 Remember, inheriting from our custom controller based class 1:27 gives the descending controller class access to our database context class. 1:31 Then, I added a private field for the series repository, and 1:36 added a constructor to initialize the private field. 1:40 In the index action method I replaced the instantiation with the list collection 1:43 with a call to the repositories get list method. 1:48 In the detail action method I replaced the instantiation of the series in the class. 1:51 With a call to the repository skip method. 1:56 In the add post action method, 1:59 I added a call to the repository's add method to add the series to the database. 2:01 In the edit get action method, I added another call to the repository's get 2:07 method, this time setting the includeRelatedEntities parameter to false. 2:12 Setting the included related entities parameter to false 2:18 wasn't absolutely necessary to do. 2:21 If you didn't you probably noticed that the page rendered just fine. 2:24 It's just a small optimization to not load the series as comic books 2:28 since the edit series page doesn't display them. 2:33 In the edit post action method, I added a call to the repository's update method. 2:36 To persist to the data base any changes made to the series entity. 2:41 In the delete get action method, 2:46 I added another call to the repository's get method. 2:47 And in the delete post action method I added a call to the repositories delete 2:51 method to delete the series entity from the data base. 2:56 And lastly, I updated the validate series method, 3:00 with a call to the repositories series has title method. 3:03 While that completed all the necessary changes to implement the series 3:10 section I decided to do a bit of cleanup work in the repository class. 3:15 The GetSeriesList method had the same implementation as the entity type focused 3:19 repositories GetList method, so I removed that method. 3:24 Removing the GetSeriesList method from the repository class 3:29 introduced an error in the coin books view model class as the Init 3:34 method contained a call to that method. 3:39 To resolve the error, I added another parameter for the seriesRepository. 3:42 And replaced the call to the GetSeriesList method with a call to 3:53 the seriesRepository.GetList method. 3:56 Changing the signature of the virtual Init method then 4:01 caused an error in the comic books adview model class. 4:05 I resolved that air by adding the seriesRepository parameter, so 4:18 its signature match the Init method in the base class. 4:22 And I updated the call to the base classes Init method 4:26 by passing in the seriesRepository parameter. 4:30 Changing the signature of the view models Init method 4:35 broke every call to that method in the ComicBooksController class. 4:38 A total of four errors. 4:42 To resolve those errors, I needed an instance of the series repository 4:44 class that I could pass to every Init method call. 4:48 So, I added a private field for the series repository, and updated the constructor 4:52 to initialize that private field to an instance of the SeriesRepository class. 4:57 Then I updated each call to the Init method to pass in 5:04 the _seriesRepository private field. 5:08 It's sometimes surprising how such a simple change like removing one 5:21 method from a repository can result in so many cascading changes, 5:26 once I had completed all of my changes, I ran and tested the app. 5:30 Don't worry if you weren't able to complete all of the tasks. 5:44 Attempting to complete a task, regardless of whether, or 5:47 not you were successful, is an important part of the learning process. 5:50 Failing at a task is not a measure of your abilities. 5:54 It's simply a signal that you've got something else to learn. 5:57 Also, you may have done things differently than how I did, and that's okay. 6:01 There's almost always more than one way to implement a feature, or 6:05 to solve a problem. 6:08 Next up finishing the artist section of the web app. 6:10
You need to sign up for Treehouse in order to download course files.Sign up