1 00:00:00,300 --> 00:00:03,626 Now, that we have our generic base repository class, 2 00:00:03,626 --> 00:00:08,920 let's update our comic books and comic book artists repositories to use it. 3 00:00:08,920 --> 00:00:12,590 First, let's update the comic books repository class. 4 00:00:12,590 --> 00:00:15,815 To start inherit from the base repository class, 5 00:00:15,815 --> 00:00:20,620 specifying comic book for the T entity generic type parameter. 6 00:00:20,620 --> 00:00:27,670 Colon, BaseRepository and we get build errors. 7 00:00:28,720 --> 00:00:33,540 The error in the class is telling us that we didn't implement the abstract get and 8 00:00:33,540 --> 00:00:35,120 getList methods. 9 00:00:35,120 --> 00:00:36,920 We'll get to that in just a bit. 10 00:00:36,920 --> 00:00:40,190 For now, let's move on to the error with the constructor. 11 00:00:40,190 --> 00:00:43,300 There is no argument given that corresponds to the required 12 00:00:43,300 --> 00:00:47,838 formal parameter context of base repository of type ComicBook, 13 00:00:47,838 --> 00:00:51,380 base repository context. 14 00:00:51,380 --> 00:00:56,510 The constructive for the base class require us the pass in a context instance. 15 00:00:56,510 --> 00:01:01,620 To do that we can use the base keyword in order to call the base class constructor 16 00:01:01,620 --> 00:01:07,520 and pass in the context parameter that is being passed to the ComicBook Repository 17 00:01:07,520 --> 00:01:13,110 constructor :base(context), then pass in the context parameter. 18 00:01:13,110 --> 00:01:15,600 We no longer need the context private field, 19 00:01:15,600 --> 00:01:18,750 as we'll use the context property in the base class. 20 00:01:18,750 --> 00:01:21,600 The green squiggle under the getlist method name 21 00:01:21,600 --> 00:01:26,070 is telling us that our method is hiding the inherited method of the same name. 22 00:01:26,070 --> 00:01:27,970 This error is easy to fix. 23 00:01:27,970 --> 00:01:31,720 We just need to add the override keyword to make our intentions clear to 24 00:01:31,720 --> 00:01:33,030 the compiler. 25 00:01:33,030 --> 00:01:38,130 public, override, and do the same for the Get method. 26 00:01:40,950 --> 00:01:43,200 Public, override. 27 00:01:48,510 --> 00:01:53,960 We also need to remove the add, update, and delete methods. 28 00:01:53,960 --> 00:01:56,920 That might seem counter-intuitive but remember, 29 00:01:56,920 --> 00:02:01,710 the base repository class is providing the implementations for these methods. 30 00:02:01,710 --> 00:02:05,870 To resolve the remaining errors, we need to replace all references to 31 00:02:05,870 --> 00:02:10,270 the _context private field with the base class's Context property. 32 00:02:17,315 --> 00:02:21,679 Now, let's finish up by updating the ComicBookArtistsRepository class. 33 00:02:25,360 --> 00:02:28,068 Inherit from the BaseRepository class, 34 00:02:28,068 --> 00:02:32,479 specify ComicBookArtist for the T entity generic type parameter, 35 00:02:32,479 --> 00:02:36,440 update the constructor to call the base class constructor. 36 00:02:40,450 --> 00:02:43,140 And remove the context private field. 37 00:02:45,940 --> 00:02:51,410 The signature for the get method currently doesn't match the base classes get method. 38 00:02:51,410 --> 00:02:55,560 If we put our cursor on the class name and press control period or 39 00:02:55,560 --> 00:02:59,520 dot, we can select to implement the abstract class. 40 00:02:59,520 --> 00:03:03,570 Which will add overrides for the missing methods, then we can cut and 41 00:03:03,570 --> 00:03:07,860 paste the old get method implementation down into the new get method. 42 00:03:08,910 --> 00:03:12,570 And remove the old get add and delete methods. 43 00:03:14,570 --> 00:03:18,700 The new get method supports the ability for the caller to specify whether or 44 00:03:18,700 --> 00:03:21,820 not the related entities are included or not. 45 00:03:21,820 --> 00:03:24,420 So, we need to update our method implementation 46 00:03:24,420 --> 00:03:26,690 to respect that parameter value. 47 00:03:26,690 --> 00:03:31,583 To start, let's call the AsQueryable method on the ComicBookArtists DB 48 00:03:31,583 --> 00:03:34,754 set property in order to get a IQueryable object. 49 00:03:34,754 --> 00:03:38,957 Var comicBookArtist equals 50 00:03:38,957 --> 00:03:45,990 Context.ComicBookArtists.AsQueryable. 51 00:03:45,990 --> 00:03:49,780 Then, let's add a conditional statement that checks if the include related 52 00:03:49,780 --> 00:03:51,520 entities parameter is set to true. 53 00:03:52,530 --> 00:03:57,423 If include related entities, and within the if statement code block we can 54 00:03:57,423 --> 00:04:02,268 add the calls to the include method to eagerly load the related entities. 55 00:04:02,268 --> 00:04:09,130 ComicBookArtists equals comicBookArtists. 56 00:04:09,130 --> 00:04:12,880 Then, cut and paste the include method calls to there. 57 00:04:16,940 --> 00:04:21,070 Then finish up with returning the result of a query to retrieve the comicBookArtist 58 00:04:21,070 --> 00:04:23,390 with the passed in ID parameter value. 59 00:04:23,390 --> 00:04:26,920 Looks like we can just modify this to suit our purposes. 60 00:04:26,920 --> 00:04:32,226 Return comicBookArtists where cba goes to cba.Id == id and 61 00:04:32,226 --> 00:04:35,090 then calling SingleOrDefault. 62 00:04:39,750 --> 00:04:43,466 The original version of the repository didn't include a method 63 00:04:43,466 --> 00:04:45,980 to get a collection of comic book artists. 64 00:04:45,980 --> 00:04:50,880 But the base class requires us to override its abstract getList method. 65 00:04:50,880 --> 00:04:55,291 Since our web app doesn't need a method to get a list of comic book artists, 66 00:04:55,291 --> 00:04:59,213 we can just leave the step-out method that throws a new instance of 67 00:04:59,213 --> 00:05:02,382 the NotImplementedException class and that's it. 68 00:05:07,740 --> 00:05:09,810 Now, let's run and test our web app. 69 00:05:13,620 --> 00:05:15,539 Here is our list of Comic Books, 70 00:05:15,539 --> 00:05:23,314 let's add a new Comic Book Bone and then issue number 5. 71 00:05:27,940 --> 00:05:31,200 Jeff Smith for the artist and script for the role. 72 00:05:33,200 --> 00:05:36,980 Here's the detail for that new issue, let's add another artist. 73 00:05:40,650 --> 00:05:43,860 Jeff Smith again, this time pencils for the role. 74 00:05:46,990 --> 00:05:50,570 Here's that new artist in his role, let's delete that artist 75 00:05:54,700 --> 00:05:56,090 And delete the comic book. 76 00:05:59,500 --> 00:06:01,760 Looks like everything's still works as expected.