1 00:00:00,340 --> 00:00:03,810 We've aligned the beginnings of the context and controller class lifetimes, 2 00:00:03,810 --> 00:00:06,870 by instantiating an instance of the context class, 3 00:00:06,870 --> 00:00:08,960 in the controller class constructor. 4 00:00:08,960 --> 00:00:12,180 But we still need to align the end of the context's lifetime 5 00:00:12,180 --> 00:00:14,360 to the end of the controller's lifetime. 6 00:00:14,360 --> 00:00:19,160 We can do that, by disposing our context when the controller's disposed. 7 00:00:19,160 --> 00:00:20,770 Let's take a moment to review, 8 00:00:20,770 --> 00:00:23,840 why it's important to dispose of the context after you're done with it? 9 00:00:24,850 --> 00:00:28,540 As we've seen in an earlier course, our context class inherits from 10 00:00:28,540 --> 00:00:33,830 the EF DbContext class, which implements the IDisposable interface. 11 00:00:33,830 --> 00:00:36,470 The IDisposable interface provides a mechanism for 12 00:00:36,470 --> 00:00:41,150 releasing unmanaged resources, through its single method, Dispose. 13 00:00:41,150 --> 00:00:43,430 Unmanaged resources need to be released or 14 00:00:43,430 --> 00:00:46,650 cleaned up, in order to prevent memory leaks. 15 00:00:46,650 --> 00:00:50,530 Applications that have memory leaks will gradually, over time, 16 00:00:50,530 --> 00:00:52,960 consume more and more memory. 17 00:00:52,960 --> 00:00:55,540 Which will cause the application to slow down, and 18 00:00:55,540 --> 00:00:59,710 potentially even slow down the server that the application is hosted on. 19 00:00:59,710 --> 00:01:03,920 When we use our context to persist or retrieve data from the database, 20 00:01:03,920 --> 00:01:08,540 EF will open a connection to the database, which is an unmanaged resource. 21 00:01:08,540 --> 00:01:13,320 By calling our context's Dispose method, which is inherited from the DBContext base 22 00:01:13,320 --> 00:01:18,060 class, we're letting EF know, that the database connection can be closed. 23 00:01:18,060 --> 00:01:22,370 Previously, we placed the instantiation of the context within a using statement. 24 00:01:22,370 --> 00:01:26,180 In order to ensure that context isDispose method was called. 25 00:01:26,180 --> 00:01:29,823 Now that we are instantiating the context within the controller's constructor, 26 00:01:29,823 --> 00:01:32,800 it's no longer possible to use that approach. 27 00:01:32,800 --> 00:01:36,530 Luckily, there is a way to explicitly dispose of the context. 28 00:01:37,720 --> 00:01:38,651 As it turns out, 29 00:01:38,651 --> 00:01:42,517 the controller base class implements the IDisposable interface. 30 00:01:44,045 --> 00:01:46,660 There are a lot of interfaces here. 31 00:01:46,660 --> 00:01:48,245 Let's scroll to the right a bit. 32 00:01:51,910 --> 00:01:54,790 And here's the IDisposable interface. 33 00:01:54,790 --> 00:01:55,620 This means, 34 00:01:55,620 --> 00:02:00,490 that the controller base class includes a public nonvirtual Dispose method. 35 00:02:00,490 --> 00:02:05,248 The public nonvirtual Dispose method is called by consumers of this type, 36 00:02:05,248 --> 00:02:07,061 specifically in this case, 37 00:02:07,061 --> 00:02:10,622 the MVC framework when it's done with the controller. 38 00:02:10,622 --> 00:02:15,146 The public nonvirtual Dispose method in turn calls this protected 39 00:02:15,146 --> 00:02:17,340 virtual Dispose method. 40 00:02:17,340 --> 00:02:22,660 The protected virtual Dispose method is where all of the actual cleanup of managed 41 00:02:22,660 --> 00:02:25,380 and unmanaged resources is done. 42 00:02:25,380 --> 00:02:28,160 Since this method is marked with the virtual keyword, 43 00:02:28,160 --> 00:02:31,690 we can override it in the comicBooks controller class. 44 00:02:31,690 --> 00:02:36,580 That gives us the hook that we need in order to explicitly dispose our context. 45 00:02:36,580 --> 00:02:40,038 Scroll down to the bottom of the comicBooks controller class, and 46 00:02:40,038 --> 00:02:42,583 override the Dispose method. 47 00:02:42,583 --> 00:02:47,275 Remember, typing override will prompt Visual Studio to display a list of methods 48 00:02:47,275 --> 00:02:50,143 in the base class that are available to override. 49 00:02:51,274 --> 00:02:56,470 We can then simply just select the method we want to override, and press enter. 50 00:02:56,470 --> 00:03:00,930 In order to guard against the case, if the Dispose method is called more than once, 51 00:03:00,930 --> 00:03:03,097 let's define a private field to track, 52 00:03:03,097 --> 00:03:05,663 if the Dispose method has already been called. 53 00:03:07,719 --> 00:03:09,933 Then, just inside of the Dispose method, 54 00:03:09,933 --> 00:03:13,210 let's check the value of the disposed private field. 55 00:03:13,210 --> 00:03:17,360 And if it's true, we can short circuit the method by immediately returning. 56 00:03:18,470 --> 00:03:20,880 The context is a managed resource. 57 00:03:20,880 --> 00:03:25,636 So we should only dispose it, if the disposing parameter value is true. 58 00:03:25,636 --> 00:03:29,983 Following this pattern, prevents us from attempting to release managed resources 59 00:03:29,983 --> 00:03:32,790 that may have already been reclaimed. 60 00:03:32,790 --> 00:03:38,020 Then after disposing the context, we need to set the disposed private to true, 61 00:03:38,020 --> 00:03:41,470 indicating that the Dispose method was called. 62 00:03:41,470 --> 00:03:42,762 Let's test our changes. 63 00:03:42,762 --> 00:03:46,590 Outside breakpoints, just inside of the Dispose method, 64 00:03:51,580 --> 00:03:53,239 And the constructor. 65 00:03:57,740 --> 00:03:59,440 And run the web app. 66 00:04:03,080 --> 00:04:06,050 Here we are at our breakpoint, in the constructor. 67 00:04:06,050 --> 00:04:08,850 So, our controller is being instantiated. 68 00:04:08,850 --> 00:04:11,193 Press F5 to continue execution. 69 00:04:13,948 --> 00:04:17,430 And here we are at our breakpoint in the Dispose method. 70 00:04:17,430 --> 00:04:20,030 Now our controller is being disposed. 71 00:04:20,030 --> 00:04:24,682 Press F10 to step down to the Dispose method call on the context. 72 00:04:30,260 --> 00:04:32,874 And F10 again, to execute the method call. 73 00:04:34,210 --> 00:04:37,131 Then F5, to continue execution again. 74 00:04:38,434 --> 00:04:40,390 And here's our list of Comic Books. 75 00:04:42,100 --> 00:04:46,280 The dispose pattern is a commonly misunderstood design pattern that is, 76 00:04:46,280 --> 00:04:49,650 unfortunately, often implemented incorrectly. 77 00:04:49,650 --> 00:04:53,580 For this reason, it's a good idea to always review Microsoft's official 78 00:04:53,580 --> 00:04:59,240 documentation under MSDN website, when you need to implement the dispose pattern. 79 00:04:59,240 --> 00:05:02,330 For more information about the dispose pattern, see the teacher's notes. 80 00:05:03,740 --> 00:05:07,230 Now that we're properly handling the lifetime of our context, 81 00:05:07,230 --> 00:05:11,090 let's move on to updating our controller to handle creates and updates.