Solution: Finishing the "Artists" Section7:41 with James Churchill
Let's review my solution for finishing the "Artists" section of the web app.
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/Final -b solution-finishing-the-artists-section
Finished Project Code
Here's how to get the code for the finished project using repositories (as it was shown in the course):
git clone email@example.com:treehouse-projects/dotnet-comic-book-library-manager.git cd dotnet-comic-book-library-manager
To review the code for the query and command version of the project, run this command (after running the above commands):
git checkout query-and-command-version
Additional Project Idea
Need an idea for an additional project? How about updating the Comic Book Gallery website from the ASP.NET MVC Basics course to use the data access code in the class library project from this course?
Here's how to get the code for the finished Comic Book Gallery website project:
git clone firstname.lastname@example.org:treehouse-projects/aspnet-comic-book-gallery.git cd aspnet-comic-book-gallery
Additional Treehouse Resources
For more information about Entity Framework, see these Treehouse courses and workshops.
- Entity Framework Basics
- Entity Framework Migrations
- Entity Framework Data Concurrency
- Entity Framework Query Performance
For more information about ASP.NET MVC, see these Treehouse courses.
If you ever have a question about EF or get stuck on something, check out the Treehouse Community. You’ll find there other students and a great team of moderators who can help.
Let's take a look at my solution at completing the artist section. 0:00 For consistency, I decided to create an entity type focus repository for 0:04 the artist entity type. 0:08 I started by adding an artist repository class to the class library's data folder. 0:10 I inherited from the base repository class and specified the artist entity type for 0:18 the entity type generic parameter. 0:23 Then I added a constructor, and implemented the get and 0:25 getList abstract methods. 0:28 In the get method, 0:31 I called the db set AsQueryable method to get a reference to an iQueryable object. 0:32 In order to equally load the comic book's navigation collection properly, 0:37 if the includeRelatedEntities parameter is set to true. 0:41 It can be difficult to see at first, but I'm actually eagerly loading 0:44 four navigation properties with these two calls to the include method. 0:49 The ComicBooks collection, which is a collection of comic book artist entities. 0:54 The comic book artist entities ComicBook in Role properties and 0:58 the ComicBook.Series property. 1:04 Remember as we discussed in an earlier video, 1:08 every property in the expression's property path will be included. 1:11 In the getList method, I call the ToList method on the Context.Artists DBSet 1:14 property, ordering the list by the artist name property. 1:20 And I finished the repository by an artist has name method that uses the any 1:25 operator to check if there is an artist with a different Id but the same name. 1:30 Next, I turn my attention to the artist controller class. 1:35 I started by updating the controller to inherit from our BaseController class. 1:39 Then I added the private field for the ArtistsRepository and 1:43 added a constructor to initialize the private field. 1:47 In the index action method, I replaced the instantiation of the list collection 1:51 with a call to the repository's getList method. 1:56 In the detail action method, I replaced the instantiation of the artist entity 1:59 class with a call to the repository's get method. 2:04 In the add post action method, 2:07 I added a call to the repository's add method to add the artist to the database. 2:09 In the edit Get action method, 2:14 I added another call to the repository's Get method. 2:16 This time, setting the include related entities parameter to false. 2:21 And then the edit post action method, 2:25 I added a call to the repository's update method. 2:27 To persist to the database any changes made to the artist entity. 2:30 In the delete action method I added another call to the repositories get 2:34 method and then the delete post action method. 2:39 I added a call to the repository delete method 2:42 to delete the artist entity from the database. 2:46 And lastly, I updated the validate artist method 2:49 with a call to the repository's artist has named method that completed 2:52 all of the necessary changes to implement the artist section of the web app. 2:57 I then moved on, to do a bit of clean up work in the repository class. 3:01 The GetArtists method had the same implementation as the Indy type focus 3:10 repositories getList method. 3:14 So I remove that method. 3:17 Removing the get artist method from the repository class introduced errors in 3:22 the comic book artist add view model and comic books add view model classes. 3:27 As the Init methods in those classes contained a call to that method. 3:32 To resolve the error in the ComicBookArtistsAddViewModel class, 3:36 I added another parameter for the artistRepository. 3:40 And we place the call to the get artist method with a call to the artist 3:43 repositories getList method. 3:48 Then, I made the same change to the comic book's add view model class. 3:54 Change the signature of the Init method. 4:09 In the ComicBooksAddViewModel class, cause an error. 4:11 ComicBooksAddViewModelInit, no suitable method found to override. 4:15 I resolved this error by adding an artistRepository parameter 4:21 to the base class's Init method, so 4:24 its signature matched the signature of the Init method in the descendant class. 4:26 And I update the call to the base class's Init method by 4:31 passing in the artistRepository parameter. 4:35 Changing the signature of the view model's Init method broke every call to those 4:38 methods in the ComicBooksController and ComicBookArtistController classes. 4:43 A total of six errors. 4:48 To resolve those errors, I needed an instance of the artist's repository class 4:50 that I could pass to each Init method call. 4:54 In the coming [controller class, I added a private field for the artist 4:57 repository and update the constructor to initialize the private field. 5:01 Then I update each call to the Init method, 5:12 the pass in _artistsRepository private field. 5:15 Then I repeated that process in the ComicBookArtistController Class. 5:26 Once I had completed all of my changes, I ran and tested the app 6:04 Congrats on finishing the Comic Book Library Manager web app. 6:17 Great job seeing this project all the way through to the end. 6:21 I ended up completing two versions of the web app. 6:25 One that utilizes repositories, and another that utilizes query and 6:28 command classes. 6:32 See the teacher's notes for links to download my completed project files. 6:33 Now is a great time to reinforce what you've learned in this course by working 6:37 on a project. 6:41 For instance, you could update the comic book gallery website 6:42 from an earlier Treehouse course on ASP.NET MVC 6:46 to use the EF related code in the shared class library project from this course. 6:49 Or you could modify the Comic Book Library Manager web app 6:55 to work just about any dataset. 6:58 Music, movies, books, the possibilities are endless. 7:01 Pick something that interests you and get started today. 7:06 Now that you're familiar how to use EF with ASP.net MVC, 7:09 you can leverage your knowledge to create powerful data driven web apps. 7:13 But your ASP.NET journey doesn't stop here. 7:17 In future Treehouse content we'll show you how to authenticate users, restrict access 7:20 based upon a user's identity and roles, how to create web-based APIs, and more. 7:26 To learn more about EF and ASP.NET see the teacher's notes for 7:31 a list of other Treehouse resources. 7:35 Have fun creating your next project and we'll see you next time. 7:38
You need to sign up for Treehouse in order to download course files.Sign up