1 00:00:00,690 --> 00:00:04,170 Let's take a look at my solution at completing the artist section. 2 00:00:04,170 --> 00:00:08,900 For consistency, I decided to create an entity type focus repository for 3 00:00:08,900 --> 00:00:10,840 the artist entity type. 4 00:00:10,840 --> 00:00:16,220 I started by adding an artist repository class to the class library's data folder. 5 00:00:18,440 --> 00:00:23,010 I inherited from the base repository class and specified the artist entity type for 6 00:00:23,010 --> 00:00:25,220 the entity type generic parameter. 7 00:00:25,220 --> 00:00:28,740 Then I added a constructor, and implemented the get and 8 00:00:28,740 --> 00:00:31,260 getList abstract methods. 9 00:00:31,260 --> 00:00:32,400 In the get method, 10 00:00:32,400 --> 00:00:37,600 I called the db set AsQueryable method to get a reference to an iQueryable object. 11 00:00:37,600 --> 00:00:41,600 In order to equally load the comic book's navigation collection properly, 12 00:00:41,600 --> 00:00:44,690 if the includeRelatedEntities parameter is set to true. 13 00:00:44,690 --> 00:00:49,280 It can be difficult to see at first, but I'm actually eagerly loading 14 00:00:49,280 --> 00:00:54,100 four navigation properties with these two calls to the include method. 15 00:00:54,100 --> 00:00:58,840 The ComicBooks collection, which is a collection of comic book artist entities. 16 00:00:58,840 --> 00:01:04,841 The comic book artist entities ComicBook in Role properties and 17 00:01:04,841 --> 00:01:08,190 the ComicBook.Series property. 18 00:01:08,190 --> 00:01:11,170 Remember as we discussed in an earlier video, 19 00:01:11,170 --> 00:01:14,758 every property in the expression's property path will be included. 20 00:01:14,758 --> 00:01:20,410 In the getList method, I call the ToList method on the Context.Artists DBSet 21 00:01:20,410 --> 00:01:23,650 property, ordering the list by the artist name property. 22 00:01:25,200 --> 00:01:30,310 And I finished the repository by an artist has name method that uses the any 23 00:01:30,310 --> 00:01:35,290 operator to check if there is an artist with a different Id but the same name. 24 00:01:35,290 --> 00:01:39,270 Next, I turn my attention to the artist controller class. 25 00:01:39,270 --> 00:01:43,850 I started by updating the controller to inherit from our BaseController class. 26 00:01:43,850 --> 00:01:47,320 Then I added the private field for the ArtistsRepository and 27 00:01:47,320 --> 00:01:50,220 added a constructor to initialize the private field. 28 00:01:51,505 --> 00:01:56,145 In the index action method, I replaced the instantiation of the list collection 29 00:01:56,145 --> 00:01:59,305 with a call to the repository's getList method. 30 00:01:59,305 --> 00:02:04,173 In the detail action method, I replaced the instantiation of the artist entity 31 00:02:04,173 --> 00:02:07,311 class with a call to the repository's get method. 32 00:02:07,311 --> 00:02:09,395 In the add post action method, 33 00:02:09,395 --> 00:02:14,640 I added a call to the repository's add method to add the artist to the database. 34 00:02:14,640 --> 00:02:16,524 In the edit Get action method, 35 00:02:16,524 --> 00:02:19,863 I added another call to the repository's Get method. 36 00:02:21,146 --> 00:02:25,780 This time, setting the include related entities parameter to false. 37 00:02:25,780 --> 00:02:27,420 And then the edit post action method, 38 00:02:27,420 --> 00:02:30,850 I added a call to the repository's update method. 39 00:02:30,850 --> 00:02:34,880 To persist to the database any changes made to the artist entity. 40 00:02:34,880 --> 00:02:39,430 In the delete action method I added another call to the repositories get 41 00:02:39,430 --> 00:02:42,880 method and then the delete post action method. 42 00:02:42,880 --> 00:02:46,200 I added a call to the repository delete method 43 00:02:46,200 --> 00:02:49,030 to delete the artist entity from the database. 44 00:02:49,030 --> 00:02:52,130 And lastly, I updated the validate artist method 45 00:02:52,130 --> 00:02:57,050 with a call to the repository's artist has named method that completed 46 00:02:57,050 --> 00:03:01,100 all of the necessary changes to implement the artist section of the web app. 47 00:03:01,100 --> 00:03:05,107 I then moved on, to do a bit of clean up work in the repository class. 48 00:03:10,022 --> 00:03:14,567 The GetArtists method had the same implementation as the Indy type focus 49 00:03:14,567 --> 00:03:17,130 repositories getList method. 50 00:03:17,130 --> 00:03:19,137 So I remove that method. 51 00:03:22,059 --> 00:03:27,054 Removing the get artist method from the repository class introduced errors in 52 00:03:27,054 --> 00:03:32,298 the comic book artist add view model and comic books add view model classes. 53 00:03:32,298 --> 00:03:36,480 As the Init methods in those classes contained a call to that method. 54 00:03:36,480 --> 00:03:40,690 To resolve the error in the ComicBookArtistsAddViewModel class, 55 00:03:40,690 --> 00:03:43,810 I added another parameter for the artistRepository. 56 00:03:43,810 --> 00:03:48,062 And we place the call to the get artist method with a call to the artist 57 00:03:48,062 --> 00:03:50,086 repositories getList method. 58 00:03:54,286 --> 00:03:58,708 Then, I made the same change to the comic book's add view model class. 59 00:04:09,085 --> 00:04:11,530 Change the signature of the Init method. 60 00:04:11,530 --> 00:04:14,570 In the ComicBooksAddViewModel class, cause an error. 61 00:04:15,860 --> 00:04:21,230 ComicBooksAddViewModelInit, no suitable method found to override. 62 00:04:21,230 --> 00:04:24,700 I resolved this error by adding an artistRepository parameter 63 00:04:24,700 --> 00:04:26,940 to the base class's Init method, so 64 00:04:26,940 --> 00:04:31,760 its signature matched the signature of the Init method in the descendant class. 65 00:04:31,760 --> 00:04:35,060 And I update the call to the base class's Init method by 66 00:04:35,060 --> 00:04:37,360 passing in the artistRepository parameter. 67 00:04:38,970 --> 00:04:43,110 Changing the signature of the view model's Init method broke every call to those 68 00:04:43,110 --> 00:04:48,560 methods in the ComicBooksController and ComicBookArtistController classes. 69 00:04:48,560 --> 00:04:50,460 A total of six errors. 70 00:04:50,460 --> 00:04:54,910 To resolve those errors, I needed an instance of the artist's repository class 71 00:04:54,910 --> 00:04:57,006 that I could pass to each Init method call. 72 00:04:57,006 --> 00:05:01,806 In the coming [controller class, I added a private field for the artist 73 00:05:01,806 --> 00:05:06,700 repository and update the constructor to initialize the private field. 74 00:05:12,622 --> 00:05:15,619 Then I update each call to the Init method, 75 00:05:15,619 --> 00:05:19,130 the pass in _artistsRepository private field. 76 00:05:26,094 --> 00:05:30,387 Then I repeated that process in the ComicBookArtistController Class. 77 00:06:04,888 --> 00:06:09,450 Once I had completed all of my changes, I ran and tested the app 78 00:06:17,449 --> 00:06:21,550 Congrats on finishing the Comic Book Library Manager web app. 79 00:06:21,550 --> 00:06:24,190 Great job seeing this project all the way through to the end. 80 00:06:25,350 --> 00:06:28,320 I ended up completing two versions of the web app. 81 00:06:28,320 --> 00:06:32,230 One that utilizes repositories, and another that utilizes query and 82 00:06:32,230 --> 00:06:33,700 command classes. 83 00:06:33,700 --> 00:06:37,660 See the teacher's notes for links to download my completed project files. 84 00:06:37,660 --> 00:06:41,640 Now is a great time to reinforce what you've learned in this course by working 85 00:06:41,640 --> 00:06:42,830 on a project. 86 00:06:42,830 --> 00:06:46,340 For instance, you could update the comic book gallery website 87 00:06:46,340 --> 00:06:49,820 from an earlier Treehouse course on ASP.NET MVC 88 00:06:49,820 --> 00:06:55,350 to use the EF related code in the shared class library project from this course. 89 00:06:55,350 --> 00:06:58,910 Or you could modify the Comic Book Library Manager web app 90 00:06:58,910 --> 00:07:01,540 to work just about any dataset. 91 00:07:01,540 --> 00:07:06,090 Music, movies, books, the possibilities are endless. 92 00:07:06,090 --> 00:07:08,670 Pick something that interests you and get started today. 93 00:07:09,780 --> 00:07:13,130 Now that you're familiar how to use EF with ASP.net MVC, 94 00:07:13,130 --> 00:07:17,428 you can leverage your knowledge to create powerful data driven web apps. 95 00:07:17,428 --> 00:07:20,980 But your ASP.NET journey doesn't stop here. 96 00:07:20,980 --> 00:07:26,000 In future Treehouse content we'll show you how to authenticate users, restrict access 97 00:07:26,000 --> 00:07:31,700 based upon a user's identity and roles, how to create web-based APIs, and more. 98 00:07:31,700 --> 00:07:35,150 To learn more about EF and ASP.NET see the teacher's notes for 99 00:07:35,150 --> 00:07:38,040 a list of other Treehouse resources. 100 00:07:38,040 --> 00:07:40,780 Have fun creating your next project and we'll see you next time.