Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Instead of a single repository class we can have multiple repository classes, each focused on a single entity type. Let's start by splitting out all of the repository methods that deal with comic books into its own repository class.
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.3 -b breaking-apart-our-repository
Keyboard Shortcuts
-
CTRL+SHIFT+B
- Build Solution
Additional Learning
-
0:00
Okay, we now have a repository, that's great, but
-
0:04
let's take another look at our design.
-
0:07
As we work on our project implementing this series and
-
0:10
artist controllers, we'll continue to add methods to our repository class.
-
0:14
Overtime, our repository can become bloated.
-
0:18
Also, our repository is currently concerned about persisting data for
-
0:22
more than one entity type, comic books and comic book artists.
-
0:26
We might think of our design is violating the single responsibility principle,
-
0:30
which states, that every class should have responsibility
-
0:34
over a single part of the functionality provided by your application.
-
0:38
Luckily for us, the solution to this problem is easy.
-
0:41
Instead of a single repository class, we can have multiple repository classes,
-
0:47
each focused on a single entity type.
-
0:50
Let's split out all of the repository methods that deal with comic books,
-
0:53
into its own repository class.
-
0:56
To start, let's add a class named ComicBooksRepository to the shared class
-
1:01
libraries date folder.
-
1:02
By default, classes use the internal access modifier, if one isn't specified.
-
1:09
That restricts access to this class to code within this project or assembly.
-
1:14
Adding a public access modifier,
-
1:15
will make this class accessible to code within the web app project.
-
1:19
Now, let's set up our database context instance.
-
1:23
At a private field of type Context named _context.
-
1:28
And a constructor that accepts a context instance, and
-
1:31
uses that the set the private field.
-
1:38
Remember, this is the same pattern that we used for the repository class,
-
1:41
which allows the controller to manage the lifetime of the context.
-
1:46
Now, we can select and cut the ComicBook related methods to the clipboard.
-
1:51
GetComicBooks, GetComicBook, AddComicBook, UpdateComicBook,
-
1:56
DeleteComicBook and ComicBookSeriesHasIssueNumber.
-
2:00
And paste those methods from the clipboard into the ComicBooksRepository class.
-
2:09
Let's also move over to ComicBookArtistRoleCombination method.
-
2:27
Looks like we've got some using directives to add.
-
2:36
For the namespaces, ComicBookShared.Models and
-
2:43
System.Data.Entity.
-
2:46
Now that we have a repository that's focused on comic books,
-
2:50
we can simplify our method names.
-
2:53
Consumers of this class will already know that they're working with comic books.
-
2:57
So we can remove any references to comic book from the method names.
-
3:01
For instance, we can change GetComicBooks to GetList,
-
3:10
And GetComicBook to simply Get.
-
3:13
And we can remove ComicBook from the CRUD method names.
-
3:32
For now, I'll leave the validation method names as they are.
-
3:36
Currently, we're instantiating an instance of the repository
-
3:40
in the BaseController class.
-
3:42
Doing this in the controller base class,
-
3:44
made the repository available to all of our controllers.
-
3:47
When you have a single repository class, this approach makes a lot of sense.
-
3:52
But when you have multiple focus repository classes, it's probably best to
-
3:56
allow each controller to instantiate the repositories that they need.
-
4:01
Given that, let's update the ComicBooksController to instantiate
-
4:04
an instance of our new ComicBooksRepository.
-
4:07
Add a private field for the repository.
-
4:21
And add a default constructor to instantiate an instance of the repository.
-
4:34
We need an instance of our database context to
-
4:37
pass into the ComicBooksRepository constructor.
-
4:40
In the BaseController class, the context is currently a private field.
-
4:44
We'll need to switch that over to be in a protected property, so
-
4:48
that it's accessible to the descendant controller classes.
-
4:51
And update any references to the private field to use the new property.
-
5:02
Back at the ComicBooksController, update the call to the ComicBooksRepository
-
5:06
constructor to pass in the base controller's context property.
-
5:10
Now we can update all of the bad repository references and method calls.
-
5:14
Repository.GetComicBooks becomes,
-
5:19
_comicBooksRepository, GetList.
-
5:26
Repository.GetComicBook becomes _comicBooksRepository get.
-
5:39
Repository.AddComicBook becomes _comicBooksRepository.Add.
-
5:45
Another call to Repository.GetComicBook to update.
-
5:53
Repository.UpdateComicBook becomes _comicBooksRepository.Update,
-
5:59
and another call to Repository.GetComicBook to update.
-
6:04
Repository.DeleteComicBook becomes _comicBooksRespository.Delete,
-
6:11
Repository.ComicBookSeriesHasIssueNumber becomes
-
6:16
_comicBookRepository.ComicBookSeriesHasIs- sueNumber.
-
6:21
And that fixes all of the build errors in the ComicBooksController.
-
6:25
Let's build the project by pressing Ctrl+Shift+B.
-
6:32
And the ComicBookArtistController has some build errors that we need to fix.
-
6:37
This controller will also need an instance on the ComicBook repository.
-
6:41
You might be tempted at this point to just move the ComicBooksRepository instance
-
6:46
into the BaseController.
-
6:47
But let's hold off on doing that.
-
6:50
I suspect that the series and
-
6:51
artist controllers won't need an instance of that repository.
-
6:55
So let's stick with our plan of letting each controller instantiate
-
6:58
the repositories that they need.
-
7:01
First, the private field.
-
7:11
Then, the default constructor.
-
7:27
And update the bad method calls
-
7:32
Repository.GetComicBook becomes
-
7:37
_comicBooksRepository.Get.
-
7:45
Once more, And
-
7:51
Repository.ComicBookHasArtistRoleCombina- tion
-
7:58
becomes _comicBook.Repository.ComicBookHasArtistR-
-
8:07
oleCombination.
-
8:11
And now the project is successfully building again.
-
8:27
In repository class, we have six methods left.
-
8:33
GetSeriesList, GetArtists, GetRoles, GetComicBookArtist,
-
8:39
AddComicBookArtist, and DeleteComicBookArtist.
-
8:44
Let's keep series, artist, and
-
8:46
role related methods in this class, at least for now.
-
8:50
Later on, when we have more than one method for
-
8:52
each of those entity types, we'll add a repository for those types.
-
8:56
But let's go ahead and
-
8:57
move the ComicBookArtist-related methods to a new repository class.
-
9:02
In fact, let's make that your next exercise.
-
9:06
Add a new repository class, move the GetComicBookArtist,
-
9:10
AddComicBookArtist, and DeleteComicBookArtist methods to it.
-
9:14
And update the ComicBookArtist controller as needed,
-
9:17
to get the project building again.
-
9:20
See you after the break.
You need to sign up for Treehouse in order to download course files.
Sign up