1 00:00:00,670 --> 00:00:03,240 As I mentioned at the end of the previous video, 2 00:00:03,240 --> 00:00:07,200 something is amiss in our comic books controller class. 3 00:00:07,200 --> 00:00:08,590 No worries, though. 4 00:00:08,590 --> 00:00:12,070 In this video, we'll dissect the issue and propose a solution. 5 00:00:13,350 --> 00:00:15,770 Here's our ComicsBooksController class. 6 00:00:15,770 --> 00:00:19,210 Our controllers Detail action method is currently responsible for 7 00:00:19,210 --> 00:00:23,610 creating the comic book model instance that we pass to our view. 8 00:00:23,610 --> 00:00:28,320 Ideally, our controller wouldn't be concerned with that aspect of our app. 9 00:00:28,320 --> 00:00:29,420 Why? 10 00:00:29,420 --> 00:00:30,810 Let's take a look at a scenario. 11 00:00:31,920 --> 00:00:36,970 Imagine that we had another method in another class that needed a comic book 12 00:00:36,970 --> 00:00:41,220 model instance for Amazing Spider Man, number 700. 13 00:00:41,220 --> 00:00:46,410 With our current implementation, we'd need to repeat this code in the second method. 14 00:00:46,410 --> 00:00:50,800 Doing this would violate a design principle called DRY or 15 00:00:50,800 --> 00:00:52,650 Don't Repeat Yourself. 16 00:00:52,650 --> 00:00:55,810 So, how do we avoid repeating ourselves? 17 00:00:55,810 --> 00:01:01,180 We move our code that creates a comic book model instance into its own method named 18 00:01:01,180 --> 00:01:01,940 GetComicBook. 19 00:01:03,000 --> 00:01:07,840 Now, any method in our website that needs an instance of that comic book model, 20 00:01:07,840 --> 00:01:10,730 can call the GetComicBook method. 21 00:01:10,730 --> 00:01:13,240 Eventually, we're going to need a method 22 00:01:13,240 --> 00:01:18,570 that will return an array of comic books for our comic books list page. 23 00:01:18,570 --> 00:01:23,570 We can put that method in a class along with the GetComicBook method. 24 00:01:23,570 --> 00:01:28,650 We can think of this class as a central location, or repository, for 25 00:01:28,650 --> 00:01:31,740 storing and managing our comic book model instances. 26 00:01:32,810 --> 00:01:35,680 Let's name this class ComicBook Repository. 27 00:01:36,740 --> 00:01:39,725 By organizing our data access code in this way, 28 00:01:39,725 --> 00:01:43,290 we're implementing the Repository Design Pattern. 29 00:01:44,640 --> 00:01:48,390 Creating a design where we don't repeat ourselves allows for 30 00:01:48,390 --> 00:01:51,920 our classes to naturally be more tightly focused. 31 00:01:51,920 --> 00:01:56,810 Each class should have a single responsibility, or concern, and 32 00:01:56,810 --> 00:02:00,720 not overlap with any other classes on our website. 33 00:02:00,720 --> 00:02:05,290 This is known as the separation of concerns design principle. 34 00:02:05,290 --> 00:02:10,340 Separating concerns also helps make our website easier to maintain. 35 00:02:10,340 --> 00:02:16,400 With our improved design, if we wanted how our comic book model instances are created 36 00:02:16,400 --> 00:02:22,260 or retrieved, we'd only need to change the comic book repository class. 37 00:02:22,260 --> 00:02:25,170 The MVC design pattern is also an example of 38 00:02:25,170 --> 00:02:28,320 the Separation of Concerns Design Principle. 39 00:02:28,320 --> 00:02:32,610 The model is concerned about our website's data and business logic. 40 00:02:32,610 --> 00:02:38,140 The view is concerned about providing a visual representation of the model. 41 00:02:38,140 --> 00:02:42,070 And the controller is concerned about processing requests and 42 00:02:42,070 --> 00:02:44,130 preparing responses. 43 00:02:44,130 --> 00:02:48,570 Separating our website's concerns using the MVC design pattern 44 00:02:48,570 --> 00:02:53,280 makes our code easier to understand and more maintainable. 45 00:02:53,280 --> 00:02:56,810 Software design principles represent a set of guidelines 46 00:02:56,810 --> 00:03:01,510 that help us avoid running into problems with our code in the long run. 47 00:03:01,510 --> 00:03:06,580 Across all software development platforms and languages, these principles have 48 00:03:06,580 --> 00:03:11,860 originated from lots of developers solving the same problems over and over again. 49 00:03:13,050 --> 00:03:14,960 There are other design principles, 50 00:03:14,960 --> 00:03:17,760 aside from the ones that we've mentioned in this video. 51 00:03:17,760 --> 00:03:21,450 See the teacher's notes for links to additional resources. 52 00:03:21,450 --> 00:03:26,110 In the next video, let's add the comic book repository class to our project.