1 00:00:00,720 --> 00:00:04,690 Okay, we now have a repository, that's great, but 2 00:00:04,690 --> 00:00:05,990 let's take another look at our design. 3 00:00:07,210 --> 00:00:10,070 As we work on our project implementing this series and 4 00:00:10,070 --> 00:00:14,520 artist controllers, we'll continue to add methods to our repository class. 5 00:00:14,520 --> 00:00:18,026 Overtime, our repository can become bloated. 6 00:00:18,026 --> 00:00:22,140 Also, our repository is currently concerned about persisting data for 7 00:00:22,140 --> 00:00:26,580 more than one entity type, comic books and comic book artists. 8 00:00:26,580 --> 00:00:30,950 We might think of our design is violating the single responsibility principle, 9 00:00:30,950 --> 00:00:34,220 which states, that every class should have responsibility 10 00:00:34,220 --> 00:00:37,390 over a single part of the functionality provided by your application. 11 00:00:38,390 --> 00:00:41,800 Luckily for us, the solution to this problem is easy. 12 00:00:41,800 --> 00:00:47,070 Instead of a single repository class, we can have multiple repository classes, 13 00:00:47,070 --> 00:00:49,380 each focused on a single entity type. 14 00:00:50,380 --> 00:00:53,895 Let's split out all of the repository methods that deal with comic books, 15 00:00:53,895 --> 00:00:55,670 into its own repository class. 16 00:00:56,970 --> 00:01:01,166 To start, let's add a class named ComicBooksRepository to the shared class 17 00:01:01,166 --> 00:01:02,530 libraries date folder. 18 00:01:02,530 --> 00:01:09,000 By default, classes use the internal access modifier, if one isn't specified. 19 00:01:09,000 --> 00:01:14,060 That restricts access to this class to code within this project or assembly. 20 00:01:14,060 --> 00:01:15,960 Adding a public access modifier, 21 00:01:15,960 --> 00:01:19,980 will make this class accessible to code within the web app project. 22 00:01:19,980 --> 00:01:23,230 Now, let's set up our database context instance. 23 00:01:23,230 --> 00:01:28,430 At a private field of type Context named _context. 24 00:01:28,430 --> 00:01:31,950 And a constructor that accepts a context instance, and 25 00:01:31,950 --> 00:01:33,750 uses that the set the private field. 26 00:01:38,037 --> 00:01:41,944 Remember, this is the same pattern that we used for the repository class, 27 00:01:41,944 --> 00:01:46,120 which allows the controller to manage the lifetime of the context. 28 00:01:46,120 --> 00:01:50,050 Now, we can select and cut the ComicBook related methods to the clipboard. 29 00:01:51,230 --> 00:01:56,047 GetComicBooks, GetComicBook, AddComicBook, UpdateComicBook, 30 00:01:56,047 --> 00:02:00,525 DeleteComicBook and ComicBookSeriesHasIssueNumber. 31 00:02:00,525 --> 00:02:08,060 And paste those methods from the clipboard into the ComicBooksRepository class. 32 00:02:09,820 --> 00:02:14,350 Let's also move over to ComicBookArtistRoleCombination method. 33 00:02:27,711 --> 00:02:30,580 Looks like we've got some using directives to add. 34 00:02:36,140 --> 00:02:43,053 For the namespaces, ComicBookShared.Models and 35 00:02:43,053 --> 00:02:46,514 System.Data.Entity. 36 00:02:46,514 --> 00:02:50,697 Now that we have a repository that's focused on comic books, 37 00:02:50,697 --> 00:02:53,540 we can simplify our method names. 38 00:02:53,540 --> 00:02:57,360 Consumers of this class will already know that they're working with comic books. 39 00:02:57,360 --> 00:03:01,650 So we can remove any references to comic book from the method names. 40 00:03:01,650 --> 00:03:05,790 For instance, we can change GetComicBooks to GetList, 41 00:03:10,028 --> 00:03:13,270 And GetComicBook to simply Get. 42 00:03:13,270 --> 00:03:16,600 And we can remove ComicBook from the CRUD method names. 43 00:03:32,420 --> 00:03:36,810 For now, I'll leave the validation method names as they are. 44 00:03:36,810 --> 00:03:40,180 Currently, we're instantiating an instance of the repository 45 00:03:40,180 --> 00:03:42,150 in the BaseController class. 46 00:03:42,150 --> 00:03:44,150 Doing this in the controller base class, 47 00:03:44,150 --> 00:03:47,630 made the repository available to all of our controllers. 48 00:03:47,630 --> 00:03:52,350 When you have a single repository class, this approach makes a lot of sense. 49 00:03:52,350 --> 00:03:56,910 But when you have multiple focus repository classes, it's probably best to 50 00:03:56,910 --> 00:04:01,130 allow each controller to instantiate the repositories that they need. 51 00:04:01,130 --> 00:04:04,360 Given that, let's update the ComicBooksController to instantiate 52 00:04:04,360 --> 00:04:07,920 an instance of our new ComicBooksRepository. 53 00:04:07,920 --> 00:04:09,830 Add a private field for the repository. 54 00:04:21,680 --> 00:04:26,240 And add a default constructor to instantiate an instance of the repository. 55 00:04:34,521 --> 00:04:37,108 We need an instance of our database context to 56 00:04:37,108 --> 00:04:40,130 pass into the ComicBooksRepository constructor. 57 00:04:40,130 --> 00:04:44,540 In the BaseController class, the context is currently a private field. 58 00:04:44,540 --> 00:04:48,020 We'll need to switch that over to be in a protected property, so 59 00:04:48,020 --> 00:04:51,650 that it's accessible to the descendant controller classes. 60 00:04:51,650 --> 00:04:55,250 And update any references to the private field to use the new property. 61 00:05:02,777 --> 00:05:06,940 Back at the ComicBooksController, update the call to the ComicBooksRepository 62 00:05:06,940 --> 00:05:10,700 constructor to pass in the base controller's context property. 63 00:05:10,700 --> 00:05:14,955 Now we can update all of the bad repository references and method calls. 64 00:05:14,955 --> 00:05:18,530 Repository.GetComicBooks becomes, 65 00:05:19,843 --> 00:05:26,890 _comicBooksRepository, GetList. 66 00:05:26,890 --> 00:05:32,422 Repository.GetComicBook becomes _comicBooksRepository get. 67 00:05:39,961 --> 00:05:45,890 Repository.AddComicBook becomes _comicBooksRepository.Add. 68 00:05:45,890 --> 00:05:48,840 Another call to Repository.GetComicBook to update. 69 00:05:53,032 --> 00:05:59,052 Repository.UpdateComicBook becomes _comicBooksRepository.Update, 70 00:05:59,052 --> 00:06:03,617 and another call to Repository.GetComicBook to update. 71 00:06:04,822 --> 00:06:11,432 Repository.DeleteComicBook becomes _comicBooksRespository.Delete, 72 00:06:11,432 --> 00:06:16,168 Repository.ComicBookSeriesHasIssueNumber becomes 73 00:06:16,168 --> 00:06:21,510 _comicBookRepository.ComicBookSeriesHasIs- sueNumber. 74 00:06:21,510 --> 00:06:25,550 And that fixes all of the build errors in the ComicBooksController. 75 00:06:25,550 --> 00:06:27,995 Let's build the project by pressing Ctrl+Shift+B. 76 00:06:32,553 --> 00:06:37,540 And the ComicBookArtistController has some build errors that we need to fix. 77 00:06:37,540 --> 00:06:41,990 This controller will also need an instance on the ComicBook repository. 78 00:06:41,990 --> 00:06:46,400 You might be tempted at this point to just move the ComicBooksRepository instance 79 00:06:46,400 --> 00:06:47,930 into the BaseController. 80 00:06:47,930 --> 00:06:50,100 But let's hold off on doing that. 81 00:06:50,100 --> 00:06:51,567 I suspect that the series and 82 00:06:51,567 --> 00:06:55,430 artist controllers won't need an instance of that repository. 83 00:06:55,430 --> 00:06:58,870 So let's stick with our plan of letting each controller instantiate 84 00:06:58,870 --> 00:07:01,040 the repositories that they need. 85 00:07:01,040 --> 00:07:02,440 First, the private field. 86 00:07:11,373 --> 00:07:13,100 Then, the default constructor. 87 00:07:27,690 --> 00:07:32,197 And update the bad method calls 88 00:07:32,197 --> 00:07:37,573 Repository.GetComicBook becomes 89 00:07:37,573 --> 00:07:42,606 _comicBooksRepository.Get. 90 00:07:45,570 --> 00:07:51,271 Once more, And 91 00:07:51,271 --> 00:07:58,721 Repository.ComicBookHasArtistRoleCombina- tion 92 00:07:58,721 --> 00:08:07,200 becomes _comicBook.Repository.ComicBookHasArtistR- 93 00:08:07,200 --> 00:08:11,888 oleCombination. 94 00:08:11,888 --> 00:08:14,690 And now the project is successfully building again. 95 00:08:27,450 --> 00:08:30,367 In repository class, we have six methods left. 96 00:08:33,125 --> 00:08:39,657 GetSeriesList, GetArtists, GetRoles, GetComicBookArtist, 97 00:08:39,657 --> 00:08:44,940 AddComicBookArtist, and DeleteComicBookArtist. 98 00:08:44,940 --> 00:08:46,900 Let's keep series, artist, and 99 00:08:46,900 --> 00:08:50,210 role related methods in this class, at least for now. 100 00:08:50,210 --> 00:08:52,550 Later on, when we have more than one method for 101 00:08:52,550 --> 00:08:56,580 each of those entity types, we'll add a repository for those types. 102 00:08:56,580 --> 00:08:57,430 But let's go ahead and 103 00:08:57,430 --> 00:09:01,400 move the ComicBookArtist-related methods to a new repository class. 104 00:09:02,510 --> 00:09:06,690 In fact, let's make that your next exercise. 105 00:09:06,690 --> 00:09:10,349 Add a new repository class, move the GetComicBookArtist, 106 00:09:10,349 --> 00:09:14,450 AddComicBookArtist, and DeleteComicBookArtist methods to it. 107 00:09:14,450 --> 00:09:17,569 And update the ComicBookArtist controller as needed, 108 00:09:17,569 --> 00:09:20,310 to get the project building again. 109 00:09:20,310 --> 00:09:21,270 See you after the break.