Reviewing the Comic Book Artists Repository3:36 with James Churchill
Let's review my solution for moving the comic book artist related repository methods to their 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.5 -b reviewing-the-comic-book-artists-repository
CTRL+O- Collapse to Definitions
How did it go? 0:00 Were you able to move the comic book artists related repository methods 0:01 to their own repository class? 0:05 Let's walk through my solution. 0:07 I started by adding the class named ComicBookArtistsRepository 0:09 to the class library's data folder. 0:13 To which I added a private field for the context instance and 0:15 a constructor that accepts a context instance, and 0:19 initializes the private field to that instance. 0:22 I then moved the GetComicBookArtist, AddComicBookArtist, and 0:25 DeleteComicBookArtist methods from the Repository class to 0:29 the ComicBookAristRepository class. 0:33 And added the using directives for the comic book shared models and 0:38 system data entity name spaces. 0:42 After I move the methods to the new class, 0:49 I renamed the GetComicBookArtist method to Get. 0:52 The AddComicBookArtist method to Add, and 1:03 the DeleteComicBookArtist method to Delete. 1:06 That keeps the entity type focused repositories 1:12 following the same method naming convention. 1:15 Then I moved on to the ComicBookArtistsController class and 1:18 added a private field from the new repository and 1:21 instantiated an instance of it in the constructor. 1:24 Then I finished up by fixing the build errors in the controller. 1:40 Repository.AddComicBookArtist became _comicBookArtistsRepository.Add. 1:46 Repository.GetComicBookArtist became _comicBookArtistRepository.Get. 1:58 Repository.DeleteComicBookArtist became _comicBookArtistsRepository.Delete. 2:10 Once the project was successfully building again, I ran the web app and 2:18 tested each page. 2:22 Remember, it's okay if you did things slightly differently than I did. 2:31 There's often more than one way to do something. 2:35 Let's take a closer look at our two entity type focused repositories. 2:38 I'll open them and place them in side by side tabs. 2:42 They both contain private fields for their context instances and initialized 2:48 those fields using a constructor that accepts a context instance. 2:52 They also both contain a Get method, 3:00 though the signature of those methods differ slightly. 3:02 The CRUD related methods are also very similar, 3:06 basically just varying by the entity type they're operating against. 3:08 So our repository classes look very similar to each other. 3:16 Which might make you wonder if there's a way that we can reduce some of this 3:20 very close to the same code. 3:24 While still retaining our entity type focused repositories. 3:25 Well, it turns out there is something that we can do. 3:29 We'll see exactly what in the next video. 3:33
You need to sign up for Treehouse in order to download course files.Sign up