Using the Repository Pattern6:40 with James Churchill
Let's add a repository to our web app project.
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.2 -b using-the-repository-pattern
CTRL+SHIFT+B- Build Solution
So far in this course, 0:00 we've been using the database context directly in our controllers. 0:01 This can lead to code duplications not only across controllers but 0:05 also across applications, 0:09 violating the design principle called DRY, Don't Repeat Yourself. 0:11 The comic book library management console app used a repository But 0:16 its overall design, as we saw earlier in this course, 0:20 wasn't going to work well with our web app. 0:23 Given that, we decided to hold off on using a repository, at least initially. 0:25 Now that we've fully implemented two of our web apps for 0:31 controllers, it's time to revisit the repository design pattern. 0:34 We can think of the repository as a central location for storing and 0:38 managing entities. 0:42 Whenever we need to retrieve entities from the data base or add, update or 0:44 delete an entity we call a method on the repository class 0:49 instead of using the data base context directly. 0:52 With this design, only the repository class has knowledge 0:56 of the context class when working with entities. 0:59 This approach makes it simple for the rest of the app to retrieve and persist data. 1:02 Let's add a repository to our web app project. 1:08 Since we aren't planning on using the existing repository class 1:11 in our web app project Let's do a little house cleaning and 1:14 move the existing repository back to the console app project. 1:18 Add a data folder to the console app project and 1:22 drag and drop the Repository.cs file from the shared class library projects data 1:28 folder to the console app projects data folder. 1:32 Then delete the file in the shared class library project. 1:36 Now we have some name spaces to fix. 1:40 Change the repository classes name space from comic book shared data 1:42 to comic book library manager data. 1:47 And add a using directive for the comic book shared data name space. 1:50 And finally, in the program.cs file, change the ComicBookShared.Data name space 1:55 to ComicBookLibrary.Data. 2:00 Now, let's add our new repository class. 2:09 We could add a repository class within a data folder in the web app project. 2:12 That matched what we did in the console lab project. 2:17 But I am going to go ahead and 2:20 add our new repository class in the shared class library project's data folder. 2:21 Putting our repository in the shared class library will give us the option to 2:26 update the console lab project to use the new repository. 2:30 Or we might add another project to the solution at some point in the future. 2:34 Maybe a public facing website or a web based API project. 2:38 Either of which might be able to use the new repository. 2:43 In order to make this class accessible to the web app project, 2:46 we need to add a public access modifier. 2:49 Unlike the console apps repository, which instantiates its own instances 2:51 of the database context We'll add a single constructor to our new repository, which 2:56 will require the consumer of this class to provide an instance of the context. 3:00 First, the private field, private Context _context = null 3:05 Then the constructor public Repository(Context context). 3:14 _context=context. 3:24 Doing this will allow our web apps controllers to continue to control 3:28 the context's lifetime. 3:31 Let's turn our attention to our base controller. 3:33 Now that our controllers will use the repository for all data retrieval and 3:36 persistance. 3:40 We can convert the context property back to a private field. 3:41 Private context underscore context equals null. 3:46 Then change the context property to be a repository property. 3:51 Change the type to repository and 3:55 the name to repository, and update the default constructor. 3:59 Context is now underscore context, and initialize the repository property. 4:06 Passing in the context private field. 4:16 Don't forget to change the context property reference in the dispose method 4:19 to use the context field. 4:23 Press control Shift B to build Okay, 4:27 after making these changes, we've introduced a whole slew of build errors. 4:32 To resolve these errors, we'll need to move 4:36 all of our database context queries and commands to our new repository class. 4:39 Let's start with updating the comic books controller's index action method. 4:44 Copy the link word to the clipboard and switch back to the repository class. 4:48 In the repository class, 4:53 add a getComicBooks method that returns an I list of comic book entities. 4:54 Public IList comic book at a using directive for 4:59 the comic book shared model's name space, then the method name, getComicBooks. 5:05 Now, paste the query from the clipboard into this method, add the return keyword, 5:13 and change context to underscore context. 5:19 Don't forget to add a using directive for the system data entity namespace so 5:25 we can use the include method to eagerly load related entity. 5:30 Every method that we add to the repository class will return a collection of 5:36 entities, a single entity, a value type like a Boolean value, or 5:40 void if a return value isn't needed. 5:45 Notice that we're calling ToList to force execution of the query. 5:47 Forcing execution of the query allows us to return a list collection type, 5:51 like I list of t. 5:56 it makes it clear that the database query will happen here in this method. 5:58 I find this approach to be cleaner and 6:02 simpler to returning an I-queriable object from the repository. 6:05 Back in the controller, update the index action method to use the repository class 6:12 method that we just added. 6:17 Repository Get ComicBooks. 6:18 One error down 24 left to fix. 6:22 This is our next exercise. 6:26 Go ahead and 6:28 fix the remaining errors by adding new methods to the repository class. 6:29 If you get stuck don't worry. 6:33 In the next video I'll look at how I resolved each error. 6:35 Good luck and we'll see you in a bit. 6:38
You need to sign up for Treehouse in order to download course files.Sign up