Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Data Access Layers4:07 with James Churchill
Another developer on our team built a console application on top of our EF model. Let's review their application and the architecture that they used.
To follow along commiting your changes to this course, you'll need to fork the dotnet-comic-book-gallery-model repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd dotnet-comic-book-gallery-model git checkout tags/v5.1 -b data-access-layers
Comic Book Library Manager Project Files
If you'd like to follow along, here's the link to download the Comic Book Library Manager project files.
[MUSIC] 0:00 Welcome back. 0:04 Another developer on our team heard that we had just finished development of 0:06 a comic books data model. 0:10 They are a huge fan of comic books. 0:12 So they took a copy of our EF model and built a simple console app on top of it. 0:14 Let's take a look at what they've built. 0:19 Here's the project for the Comic Book Library Manager console lab. 0:22 After starting the app, we're presented with a list of the available comic books. 0:29 We can type a to add a comic book. 0:34 We're prompted to select the series. 0:38 And an issue number. 0:41 The description, The published on date. 0:42 The average rating. 0:55 The artist. 0:59 And the artists role. 1:01 Then we're returned to the list of comic books 1:05 which now includes the comic book that we just added. 1:08 We can type a line number to view the detail for that comic book. 1:13 From here, we can type u to update the comic book. 1:18 We're then presented with a list of the properties that we can change. 1:23 I'll type 2 to change the issue number value. 1:27 Once we've made all the changes that we want to make, enter S to save. 1:33 Now we're viewing the detail for our selected comic book again. 1:39 Notice that the issue number has been changed to five. 1:43 We can delete the comic book by entering D and then Y to confirm the deletion. 1:48 And here's the list of the comic books again 1:56 without the comic book that we just deleted. 1:59 Let's review the architecture of the app. 2:02 When they built their app, they put all the code that interacted with the EF 2:05 context class into its own class named Repository. 2:09 Whenever they need to retrieve entities from the database or add, update, or 2:14 delete an entity. 2:18 They call a method on the repository class 2:19 instead of using the contents class directly. 2:22 With this design the only part of their app that has 2:25 any knowledge of the context class is the repository class. 2:28 Using this approach makes it very simple for the rest of the app to retrieve and 2:33 persist data. 2:37 As we've seen in previous treehouse courses by organizing our data access 2:39 code in this way, we're implementing the repository design pattern. 2:44 All of the application code lives in the app layer, 2:48 the repository class along with the context and database initializer classes. 2:52 Livein the data access layer or DAL. 2:58 The entity classes are shared between the app and data access layers. 3:02 Separating our code into layers, gives us greater flexibility. 3:08 For example, 3:13 we could build additional app layer implementations like a web or desktop app. 3:14 Or maybe even a mobile app. 3:19 Without having to rewrite or change the data access layer. 3:21 In this section we'll learn how to perform CRUD operations, 3:26 specifically create, update and delete with EF context class. 3:30 As we do that we'll be implementing the various methods in the repository class 3:35 in the comic book library managers data access layer. 3:40 I'd encourage you to follow along. 3:44 Writing code is an important part of the learning process. 3:46 You'll gain a deeper understanding of the material that will cover, and 3:50 you'll improve your retention. 3:54 See the teacher's notes for a link to download the Visual Studio project. 3:56 Let's start by seeing how we can use the EF context to create new entities. 4:00 See you after the break. 4:05
You need to sign up for Treehouse in order to download course files.Sign up