Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Now that we have our generic base repository class, let's update our comic books and comic book artists repositories to use it.
Follow Along
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/v3.7 -b using-our-generic-base-repository-class
Keyboard Shortcuts
-
CTRL+PERIOD
orCTRL+DOT
- Quick Actions
Additional .NET Generics Resources
Now, that we have our generic
base repository class,
0:00
let's update our comic books and
comic book artists repositories to use it.
0:03
First, let's update the comic
books repository class.
0:08
To start inherit from
the base repository class,
0:12
specifying comic book for
the T entity generic type parameter.
0:15
Colon, BaseRepository<ComicBook> and
we get build errors.
0:20
The error in the class is telling us that
we didn't implement the abstract get and
0:28
getList methods.
0:33
We'll get to that in just a bit.
0:35
For now, let's move on to
the error with the constructor.
0:36
There is no argument given that
corresponds to the required
0:40
formal parameter context of base
repository of type ComicBook,
0:43
base repository context.
0:47
The constructive for the base class
require us the pass in a context instance.
0:51
To do that we can use the base keyword in
order to call the base class constructor
0:56
and pass in the context parameter that is
being passed to the ComicBook Repository
1:01
constructor :base(context),
then pass in the context parameter.
1:07
We no longer need
the context private field,
1:13
as we'll use the context
property in the base class.
1:15
The green squiggle under
the getlist method name
1:18
is telling us that our method is hiding
the inherited method of the same name.
1:21
This error is easy to fix.
1:26
We just need to add the override
keyword to make our intentions clear to
1:27
the compiler.
1:31
public, override, and
do the same for the Get method.
1:33
Public, override.
1:40
We also need to remove the add,
update, and delete methods.
1:48
That might seem counter-intuitive but
remember,
1:53
the base repository class is providing
the implementations for these methods.
1:56
To resolve the remaining errors,
we need to replace all references to
2:01
the _context private field with
the base class's Context property.
2:05
Now, let's finish up by updating
the ComicBookArtistsRepository class.
2:17
Inherit from the BaseRepository class,
2:25
specify ComicBookArtist for
the T entity generic type parameter,
2:28
update the constructor to call
the base class constructor.
2:32
And remove the context private field.
2:40
The signature for the get method currently
doesn't match the base classes get method.
2:45
If we put our cursor on the class name and
press control period or
2:51
dot, we can select to
implement the abstract class.
2:55
Which will add overrides for
the missing methods, then we can cut and
2:59
paste the old get method implementation
down into the new get method.
3:03
And remove the old get add and
delete methods.
3:08
The new get method supports the ability
for the caller to specify whether or
3:14
not the related entities are included or
not.
3:18
So, we need to update our
method implementation
3:21
to respect that parameter value.
3:24
To start, let's call the AsQueryable
method on the ComicBookArtists DB
3:26
set property in order to
get a IQueryable object.
3:31
Var comicBookArtist equals
3:34
Context.ComicBookArtists.AsQueryable.
3:38
Then, let's add a conditional statement
that checks if the include related
3:45
entities parameter is set to true.
3:49
If include related entities, and
within the if statement code block we can
3:52
add the calls to the include method
to eagerly load the related entities.
3:57
ComicBookArtists equals comicBookArtists.
4:02
Then, cut and
paste the include method calls to there.
4:09
Then finish up with returning the result
of a query to retrieve the comicBookArtist
4:16
with the passed in ID parameter value.
4:21
Looks like we can just modify
this to suit our purposes.
4:23
Return comicBookArtists where
cba goes to cba.Id == id and
4:26
then calling SingleOrDefault.
4:32
The original version of
the repository didn't include a method
4:39
to get a collection of comic book artists.
4:43
But the base class requires us to
override its abstract getList method.
4:45
Since our web app doesn't need a method
to get a list of comic book artists,
4:50
we can just leave the step-out
method that throws a new instance of
4:55
the NotImplementedException class and
that's it.
4:59
Now, let's run and test our web app.
5:07
Here is our list of Comic Books,
5:13
let's add a new Comic Book Bone and
then issue number 5.
5:15
Jeff Smith for the artist and
script for the role.
5:27
Here's the detail for that new issue,
let's add another artist.
5:33
Jeff Smith again,
this time pencils for the role.
5:40
Here's that new artist in his role,
let's delete that artist
5:46
And delete the comic book.
5:54
Looks like everything's
still works as expected.
5:59
You need to sign up for Treehouse in order to download course files.
Sign up