Breaking Apart Our Repository9:22 with James Churchill
Instead of a single repository class we can have multiple repository classes, each focused on a single entity type. Let's start by splitting out all of the repository methods that deal with comic books into its own repository class.
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 breaking-apart-our-repository
CTRL+SHIFT+B- Build Solution
Okay, we now have a repository, that's great, but 0:00 let's take another look at our design. 0:04 As we work on our project implementing this series and 0:07 artist controllers, we'll continue to add methods to our repository class. 0:10 Overtime, our repository can become bloated. 0:14 Also, our repository is currently concerned about persisting data for 0:18 more than one entity type, comic books and comic book artists. 0:22 We might think of our design is violating the single responsibility principle, 0:26 which states, that every class should have responsibility 0:30 over a single part of the functionality provided by your application. 0:34 Luckily for us, the solution to this problem is easy. 0:38 Instead of a single repository class, we can have multiple repository classes, 0:41 each focused on a single entity type. 0:47 Let's split out all of the repository methods that deal with comic books, 0:50 into its own repository class. 0:53 To start, let's add a class named ComicBooksRepository to the shared class 0:56 libraries date folder. 1:01 By default, classes use the internal access modifier, if one isn't specified. 1:02 That restricts access to this class to code within this project or assembly. 1:09 Adding a public access modifier, 1:14 will make this class accessible to code within the web app project. 1:15 Now, let's set up our database context instance. 1:19 At a private field of type Context named _context. 1:23 And a constructor that accepts a context instance, and 1:28 uses that the set the private field. 1:31 Remember, this is the same pattern that we used for the repository class, 1:38 which allows the controller to manage the lifetime of the context. 1:41 Now, we can select and cut the ComicBook related methods to the clipboard. 1:46 GetComicBooks, GetComicBook, AddComicBook, UpdateComicBook, 1:51 DeleteComicBook and ComicBookSeriesHasIssueNumber. 1:56 And paste those methods from the clipboard into the ComicBooksRepository class. 2:00 Let's also move over to ComicBookArtistRoleCombination method. 2:09 Looks like we've got some using directives to add. 2:27 For the namespaces, ComicBookShared.Models and 2:36 System.Data.Entity. 2:43 Now that we have a repository that's focused on comic books, 2:46 we can simplify our method names. 2:50 Consumers of this class will already know that they're working with comic books. 2:53 So we can remove any references to comic book from the method names. 2:57 For instance, we can change GetComicBooks to GetList, 3:01 And GetComicBook to simply Get. 3:10 And we can remove ComicBook from the CRUD method names. 3:13 For now, I'll leave the validation method names as they are. 3:32 Currently, we're instantiating an instance of the repository 3:36 in the BaseController class. 3:40 Doing this in the controller base class, 3:42 made the repository available to all of our controllers. 3:44 When you have a single repository class, this approach makes a lot of sense. 3:47 But when you have multiple focus repository classes, it's probably best to 3:52 allow each controller to instantiate the repositories that they need. 3:56 Given that, let's update the ComicBooksController to instantiate 4:01 an instance of our new ComicBooksRepository. 4:04 Add a private field for the repository. 4:07 And add a default constructor to instantiate an instance of the repository. 4:21 We need an instance of our database context to 4:34 pass into the ComicBooksRepository constructor. 4:37 In the BaseController class, the context is currently a private field. 4:40 We'll need to switch that over to be in a protected property, so 4:44 that it's accessible to the descendant controller classes. 4:48 And update any references to the private field to use the new property. 4:51 Back at the ComicBooksController, update the call to the ComicBooksRepository 5:02 constructor to pass in the base controller's context property. 5:06 Now we can update all of the bad repository references and method calls. 5:10 Repository.GetComicBooks becomes, 5:14 _comicBooksRepository, GetList. 5:19 Repository.GetComicBook becomes _comicBooksRepository get. 5:26 Repository.AddComicBook becomes _comicBooksRepository.Add. 5:39 Another call to Repository.GetComicBook to update. 5:45 Repository.UpdateComicBook becomes _comicBooksRepository.Update, 5:53 and another call to Repository.GetComicBook to update. 5:59 Repository.DeleteComicBook becomes _comicBooksRespository.Delete, 6:04 Repository.ComicBookSeriesHasIssueNumber becomes 6:11 _comicBookRepository.ComicBookSeriesHasIs- sueNumber. 6:16 And that fixes all of the build errors in the ComicBooksController. 6:21 Let's build the project by pressing Ctrl+Shift+B. 6:25 And the ComicBookArtistController has some build errors that we need to fix. 6:32 This controller will also need an instance on the ComicBook repository. 6:37 You might be tempted at this point to just move the ComicBooksRepository instance 6:41 into the BaseController. 6:46 But let's hold off on doing that. 6:47 I suspect that the series and 6:50 artist controllers won't need an instance of that repository. 6:51 So let's stick with our plan of letting each controller instantiate 6:55 the repositories that they need. 6:58 First, the private field. 7:01 Then, the default constructor. 7:11 And update the bad method calls 7:27 Repository.GetComicBook becomes 7:32 _comicBooksRepository.Get. 7:37 Once more, And 7:45 Repository.ComicBookHasArtistRoleCombina- tion 7:51 becomes _comicBook.Repository.ComicBookHasArtistR- 7:58 oleCombination. 8:07 And now the project is successfully building again. 8:11 In repository class, we have six methods left. 8:27 GetSeriesList, GetArtists, GetRoles, GetComicBookArtist, 8:33 AddComicBookArtist, and DeleteComicBookArtist. 8:39 Let's keep series, artist, and 8:44 role related methods in this class, at least for now. 8:46 Later on, when we have more than one method for 8:50 each of those entity types, we'll add a repository for those types. 8:52 But let's go ahead and 8:56 move the ComicBookArtist-related methods to a new repository class. 8:57 In fact, let's make that your next exercise. 9:02 Add a new repository class, move the GetComicBookArtist, 9:06 AddComicBookArtist, and DeleteComicBookArtist methods to it. 9:10 And update the ComicBookArtist controller as needed, 9:14 to get the project building again. 9:17 See you after the break. 9:20
You need to sign up for Treehouse in order to download course files.Sign up