1 00:00:00,520 --> 00:00:01,730 So far in this course, 2 00:00:01,730 --> 00:00:05,400 we've been using the database context directly in our controllers. 3 00:00:05,400 --> 00:00:09,170 This can lead to code duplications not only across controllers but 4 00:00:09,170 --> 00:00:11,580 also across applications, 5 00:00:11,580 --> 00:00:16,300 violating the design principle called DRY, Don't Repeat Yourself. 6 00:00:16,300 --> 00:00:20,150 The comic book library management console app used a repository But 7 00:00:20,150 --> 00:00:23,530 its overall design, as we saw earlier in this course, 8 00:00:23,530 --> 00:00:25,900 wasn't going to work well with our web app. 9 00:00:25,900 --> 00:00:31,190 Given that, we decided to hold off on using a repository, at least initially. 10 00:00:31,190 --> 00:00:34,060 Now that we've fully implemented two of our web apps for 11 00:00:34,060 --> 00:00:37,660 controllers, it's time to revisit the repository design pattern. 12 00:00:38,930 --> 00:00:42,850 We can think of the repository as a central location for storing and 13 00:00:42,850 --> 00:00:44,470 managing entities. 14 00:00:44,470 --> 00:00:49,220 Whenever we need to retrieve entities from the data base or add, update or 15 00:00:49,220 --> 00:00:52,960 delete an entity we call a method on the repository class 16 00:00:52,960 --> 00:00:56,060 instead of using the data base context directly. 17 00:00:56,060 --> 00:00:59,820 With this design, only the repository class has knowledge 18 00:00:59,820 --> 00:01:02,930 of the context class when working with entities. 19 00:01:02,930 --> 00:01:08,000 This approach makes it simple for the rest of the app to retrieve and persist data. 20 00:01:08,000 --> 00:01:11,490 Let's add a repository to our web app project. 21 00:01:11,490 --> 00:01:14,940 Since we aren't planning on using the existing repository class 22 00:01:14,940 --> 00:01:18,450 in our web app project Let's do a little house cleaning and 23 00:01:18,450 --> 00:01:22,190 move the existing repository back to the console app project. 24 00:01:22,190 --> 00:01:28,120 Add a data folder to the console app project and 25 00:01:28,120 --> 00:01:32,880 drag and drop the Repository.cs file from the shared class library projects data 26 00:01:32,880 --> 00:01:36,000 folder to the console app projects data folder. 27 00:01:36,000 --> 00:01:38,960 Then delete the file in the shared class library project. 28 00:01:40,280 --> 00:01:42,990 Now we have some name spaces to fix. 29 00:01:42,990 --> 00:01:47,840 Change the repository classes name space from comic book shared data 30 00:01:47,840 --> 00:01:50,800 to comic book library manager data. 31 00:01:50,800 --> 00:01:55,440 And add a using directive for the comic book shared data name space. 32 00:01:55,440 --> 00:02:00,770 And finally, in the program.cs file, change the ComicBookShared.Data name space 33 00:02:00,770 --> 00:02:05,965 to ComicBookLibrary.Data. 34 00:02:09,680 --> 00:02:12,530 Now, let's add our new repository class. 35 00:02:12,530 --> 00:02:17,410 We could add a repository class within a data folder in the web app project. 36 00:02:17,410 --> 00:02:20,050 That matched what we did in the console lab project. 37 00:02:20,050 --> 00:02:21,410 But I am going to go ahead and 38 00:02:21,410 --> 00:02:26,420 add our new repository class in the shared class library project's data folder. 39 00:02:26,420 --> 00:02:30,510 Putting our repository in the shared class library will give us the option to 40 00:02:30,510 --> 00:02:34,100 update the console lab project to use the new repository. 41 00:02:34,100 --> 00:02:38,220 Or we might add another project to the solution at some point in the future. 42 00:02:38,220 --> 00:02:43,030 Maybe a public facing website or a web based API project. 43 00:02:43,030 --> 00:02:46,020 Either of which might be able to use the new repository. 44 00:02:46,020 --> 00:02:49,160 In order to make this class accessible to the web app project, 45 00:02:49,160 --> 00:02:51,720 we need to add a public access modifier. 46 00:02:51,720 --> 00:02:56,030 Unlike the console apps repository, which instantiates its own instances 47 00:02:56,030 --> 00:03:00,950 of the database context We'll add a single constructor to our new repository, which 48 00:03:00,950 --> 00:03:05,820 will require the consumer of this class to provide an instance of the context. 49 00:03:05,820 --> 00:03:14,890 First, the private field, private Context _context = null 50 00:03:14,890 --> 00:03:21,953 Then the constructor public Repository(Context context). 51 00:03:24,824 --> 00:03:28,050 _context=context. 52 00:03:28,050 --> 00:03:31,670 Doing this will allow our web apps controllers to continue to control 53 00:03:31,670 --> 00:03:33,510 the context's lifetime. 54 00:03:33,510 --> 00:03:36,140 Let's turn our attention to our base controller. 55 00:03:36,140 --> 00:03:40,260 Now that our controllers will use the repository for all data retrieval and 56 00:03:40,260 --> 00:03:41,370 persistance. 57 00:03:41,370 --> 00:03:44,690 We can convert the context property back to a private field. 58 00:03:46,380 --> 00:03:51,800 Private context underscore context equals null. 59 00:03:51,800 --> 00:03:55,930 Then change the context property to be a repository property. 60 00:03:55,930 --> 00:03:59,820 Change the type to repository and 61 00:03:59,820 --> 00:04:04,450 the name to repository, and update the default constructor. 62 00:04:06,250 --> 00:04:12,190 Context is now underscore context, and initialize the repository property. 63 00:04:16,730 --> 00:04:19,120 Passing in the context private field. 64 00:04:19,120 --> 00:04:23,240 Don't forget to change the context property reference in the dispose method 65 00:04:23,240 --> 00:04:24,580 to use the context field. 66 00:04:27,420 --> 00:04:32,360 Press control Shift B to build Okay, 67 00:04:32,360 --> 00:04:36,830 after making these changes, we've introduced a whole slew of build errors. 68 00:04:36,830 --> 00:04:39,260 To resolve these errors, we'll need to move 69 00:04:39,260 --> 00:04:44,090 all of our database context queries and commands to our new repository class. 70 00:04:44,090 --> 00:04:48,650 Let's start with updating the comic books controller's index action method. 71 00:04:48,650 --> 00:04:53,330 Copy the link word to the clipboard and switch back to the repository class. 72 00:04:53,330 --> 00:04:54,930 In the repository class, 73 00:04:54,930 --> 00:04:59,990 add a getComicBooks method that returns an I list of comic book entities. 74 00:04:59,990 --> 00:05:05,630 Public IList comic book at a using directive for 75 00:05:05,630 --> 00:05:11,840 the comic book shared model's name space, then the method name, getComicBooks. 76 00:05:13,190 --> 00:05:18,190 Now, paste the query from the clipboard into this method, add the return keyword, 77 00:05:19,330 --> 00:05:22,860 and change context to underscore context. 78 00:05:25,970 --> 00:05:30,530 Don't forget to add a using directive for the system data entity namespace so 79 00:05:30,530 --> 00:05:34,370 we can use the include method to eagerly load related entity. 80 00:05:36,940 --> 00:05:40,800 Every method that we add to the repository class will return a collection of 81 00:05:40,800 --> 00:05:45,230 entities, a single entity, a value type like a Boolean value, or 82 00:05:45,230 --> 00:05:47,800 void if a return value isn't needed. 83 00:05:47,800 --> 00:05:51,450 Notice that we're calling ToList to force execution of the query. 84 00:05:51,450 --> 00:05:56,000 Forcing execution of the query allows us to return a list collection type, 85 00:05:56,000 --> 00:05:58,170 like I list of t. 86 00:05:58,170 --> 00:06:02,330 it makes it clear that the database query will happen here in this method. 87 00:06:02,330 --> 00:06:05,230 I find this approach to be cleaner and 88 00:06:05,230 --> 00:06:09,000 simpler to returning an I-queriable object from the repository. 89 00:06:12,590 --> 00:06:17,120 Back in the controller, update the index action method to use the repository class 90 00:06:17,120 --> 00:06:18,910 method that we just added. 91 00:06:18,910 --> 00:06:22,933 Repository Get ComicBooks. 92 00:06:22,933 --> 00:06:26,600 One error down 24 left to fix. 93 00:06:26,600 --> 00:06:28,460 This is our next exercise. 94 00:06:28,460 --> 00:06:29,060 Go ahead and 95 00:06:29,060 --> 00:06:33,280 fix the remaining errors by adding new methods to the repository class. 96 00:06:33,280 --> 00:06:35,340 If you get stuck don't worry. 97 00:06:35,340 --> 00:06:38,700 In the next video I'll look at how I resolved each error. 98 00:06:38,700 --> 00:06:40,100 Good luck and we'll see you in a bit.