Welcome back.
In this video we're going to look at dependency injection and specifically into dependencies.
So dependency injection is a very powerful concept in especially object oriented programming languages.
And in order to understand what dependencies are and dependency injection is, we first need to really
dive deeper into dependencies.
Okay.
So we're going to use a very simple example, which is going to be about a builder that needs tools.
So what are dependencies?
Dependencies refer to objects or components that a class needs in order to function properly.
Think of dependencies as the necessary tools or services a class relies on to perform its tasks.
So imagine you're a builder.
To build a house, you need various tools like a hammer, a saw, a nails, and a bunch more other tools.
And these tools are your dependencies.
So let's look at the dependencies in a practical example in our code okay.
So let's start with a public class hammer okay.
So we have a hammer.
And we will have a method in here which we will just call use okay.
So we should be able to use that hammer.
And when we do that then whatever hammer logic happens.
So hammering nails for example.
Very simple example.
You're hammering nails.
What is the hammer for the hammer nails.
Or to break something right.
Sometimes it's just to break something if you are renovating or whatever.
So that's our hammer.
Now let's create our builder public class builder.
All right.
And now our builder needs a hammer.
So what we're going to do is we're going to have a hammer.
It will be a private hammer.
So it will not be a property or whatever.
And then in the constructor public builder we need to set that hammer.
So the hammer will be a new hammer okay.
So here the builder is responsible for creating its dependencies.
So what does that mean.
Well that means that the builder is responsible of bringing his hammer.
If the builder doesn't set his hammer when creating himself or when going to work, so to speak, when
he doesn't bring his hammer, he can't do the work.
So there is our dependency.
And then I can now go ahead and say, the builder gets starting to build a house and we're not going
to pass anything here.
We're just going to use our hammer.
And then we're going to say, the house is built, so house built.
So obviously we need more than just the hammer.
So how about we also bring our saw.
So therefore let's go ahead and create a saw public class saw.
Now the saw will also have a method.
And you could call this saw method saw or sawing or something like that.
Or you could also just say okay public void use.
All right.
So this method will just be also a simple Console.WriteLine statement.
And I'm going to say sawing wood.
All right.
And now our builder also mustn't forget his saw.
And let's use it like this.
So now we have a saw but we haven't brought it with us yet.
So we can bring it with us.
Because Saws exists, right?
As a builder we know Saws exists.
So at this point I know that a saw exists, and at this point I am actually bringing my saw.
I'm bringing my new saw.
Obviously, it doesn't always have to be a new saw, but we need to use the new keyword here to create
a new saw.
So now our builder, whenever he builds a house, can also go ahead and use his saw.
Okay, so where is the dependency here?
The dependency is that the builder depends on the hammer and the saw.
So this is where now the saw and the hammer are dependencies of the builder.
That's what we're basically saying.
All right.
So that's pretty much it for dependencies.
Now in the next video let's look at into what dependency injection is.
So how do we inject dependencies.
So see you in the next video.