This workshop will be retired on May 1, 2025.
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
In this video, we'll learn about DI containers and how to select a DI container library for use in our project.
Popular DI Containers for .NET
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
So this is great.
0:00
We've refactored our controller code
to declare its single dependency,
0:02
the repository class as
a constructor parameter.
0:06
But what's going to supply an instance
of our repository to the controller?
0:09
If we were instantiating instances of the
controller ourselves, then we could supply
0:13
the necessary dependency when we
up the controller object instance.
0:17
For instance, we could instantiate
an instance of the context class.
0:21
Then an instance of the repository class,
passing in the instance of
0:29
the context class, cuz that's
the dependency of the repository class.
0:34
Then we can construct
an instance of our controller.
0:39
PlanetsController, passing in
our instance of the repository.
0:45
But NVC is responsible for
instantiating our controllers.
0:49
So, this doesn't work.
0:53
In fact, if we try running our app,
we'll get an error.
0:55
No parameterless constructor defined for
this object.
0:59
This error is telling us that if you
see a tip to create an instance of
1:03
our controller,
1:07
it wasn't able to do so, because it
no longer has a default constructor.
1:07
A constructor without any parameters.
1:12
We need something that can
manage the creation and
1:15
lifetime of our controllers' dependencies.
1:18
To do that, we can integrate into our
NVC app something called a DI container.
1:20
There are many DI container
options available for .NET.
1:26
Here are some of the most
popular libraries.
1:30
The concepts of working with
a DI container are more or
1:32
less the same regardless
of which you decide to use.
1:35
In fact, most of the time, switching to
a new container will just affect your
1:39
app's configuration and startup code.
1:44
When deciding which DI container
to use in your project,
1:47
let's consider the following
selection criteria.
1:50
Usability, or developer ease of use.
1:53
Is the container easy for
developers to install, configure, and use?
1:56
Performance.
2:02
Is the container fast enough for
your specific situation?
2:03
Documentation.
2:06
Are the docs detailed and easy to follow?
2:08
And viability.
2:11
Is the library being regularly
maintained and supported?
2:13
In this workshop, we'll be using
a simple injector for our DI container.
2:17
It's easy to use, it's fast, and at
the time of this recording, it's Repo and
2:21
GitHub is actively being maintained.
2:26
Next, let's install into our project and
2:29
configure this simple
injector DI container.
2:32
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up