Adding a Base Controller Class6:56 with James Churchill
Let's see how we can prevent duplicating the code—from controller to controller—that's used to manage the lifetime of the database context.
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/v2.8 -b adding-a-base-controller-class
CTRL+H- Quick Replace
SHIFT+ALT+ENTER- Toggle fullscreen mode
[MUSIC] 0:00 In this section we'll look at some ways we can improve the design and 0:04 organization of our data access code. 0:08 As we make changes, we'll strive to improve code reuse by removing or 0:10 preventing unnecessarily duplicated code. 0:15 I'll also make in our code more readable and maintainable. 0:18 The first problem that we'll tackle is seeing how we can prevent duplicating 0:22 the code from controller to controller 0:25 that's used to manage the lifetime of the database context. 0:28 I've opened the ComicBooksController class here in the top half of visual studio. 0:31 And the ComicBookArtistController class in the bottom half. 0:36 We can see that both controllers inherit from the MVC controller base class, 0:40 define a private field for the database context and 0:45 use a default constructor to instantiate the database context instance. 0:49 And if we scroll down to the bottom of each class, we can see that they both use 0:55 the same code from managing the disposal of the database context instance. 0:59 Having a shared base class gives us an opportunity to create our own 1:06 controller base class that both controllers can inherit from. 1:09 Then we can move the database context code into our new controller base class. 1:13 Let's see how this is done. 1:18 To start add a new class to the Controllers folder named BaseController. 1:19 Our controller base class will never need to be instantiated directly. 1:27 So let's ensure that it's not possible to do that 1:30 by adding the abstract keyword right before the class keyword. 1:33 Let's also inherit from the MVC Controller class. 1:37 Now, let's add code to instantiate an instance of the context class. 1:41 In general, we should always try to limit access to code 1:45 by using the most restrictive access modifier that we can. 1:49 In this case, we can use a protected property, which will restrict access to 1:52 members within this class or in the descending classes. 1:56 Protected, context at a using directive for the ComicBookShared data name space. 2:02 Context for the property name and get set. 2:10 We can initialize this property to an instance of the context class 2:13 within a default constructor. 2:16 Public BaseController, 2:18 Context = new Context. 2:22 And since we're only setting the property value from within this class 2:27 we can make the property setter private 2:30 by adding the private keyword right before the set keyword. 2:33 Now let's grab a copy of the disposed method from the comic books 2:36 controller class. 2:39 Let's also grab the disposed private field. 2:41 I'll move the disposed private field up to the top of the class. 2:52 A typically defined field's at the top of the class, though this is a code style 2:58 preference that you and your teammates should decide upon and apply consistently. 3:02 We switched our context instance from a field to a property. 3:07 So we'll need to update the reference in the dispose method and 3:11 that completes our base controller class. 3:14 Now, we need to update our controllers to use our new base controller. 3:17 To start we can update the class to inherit from our new base 3:29 controller class instead of the MVC controller class. 3:33 Remember, the controller will still descend from the MVC controller class, 3:36 even though now it inherits from the base controller class. 3:41 That's because the base controller class inherits from the MVC controller class. 3:44 This change of class inheritance is called a class hierarchy. 3:51 Class hierarchies are very common in object oriented languages like C#. 3:55 Since our new BaseController is now providing the database context instance, 4:00 in managing its lifetime, we can remove this private field and default constructor 4:04 And the _diposed private field, and the dispose method. 4:12 Notice all the red here in the vertical scroll bar. 4:24 We have a number of references to the context private field 4:27 that need to be updated to use the context protected property. 4:31 I will replace every instance of underscore context with 4:34 context By selecting the _context text and pressing Ctrl+H. 4:38 You can click replace next to review each change or 4:46 replace all to replace every instance in the current scope 4:49 which you wanna make sure is set to current document. 4:53 Now let's update our other controller. 4:58 Change the base class to our new BaseController class. 5:08 Remove the private field and default constructor. 5:14 [SOUND] Remove the dispose field and dispose method. 5:22 [SOUND] And 5:25 update all the underscore context field references to the context property. 5:35 Let's finish up by testing our changes. 5:45 Here's our list of comic books. 5:49 Let's add a new comic book. 5:52 And another artist. 6:09 Delete an artist. 6:20 And delete the comic book. 6:26 Looks like we didn't break anything. 6:32 Having a custom controller base class will make it easier to 6:35 update the remaining controllers in our web app to use EF. 6:38 Without having to duplicate the code that manages the lifetime 6:42 of the database context from controller to controller. 6:45 In the next video, we'll continue with our data access code refactoring 6:49 by reintroducing the repository design pattern 6:53
You need to sign up for Treehouse in order to download course files.Sign up