1
00:00:00,000 --> 00:00:05,106
[MUSIC]

2
00:00:05,106 --> 00:00:08,592
Let's now take a piercing
look at dependency injection.

3
00:00:08,592 --> 00:00:13,874
What exactly is dependency injection and
how is it relevant to Angular?

4
00:00:13,874 --> 00:00:17,180
Let's talk about that next.

5
00:00:17,180 --> 00:00:20,260
So, what exactly is dependency injection?

6
00:00:20,260 --> 00:00:24,600
Dependency injection Is
a software design pattern.

7
00:00:24,600 --> 00:00:28,890
This is a pattern that is useful for
implementing applications where you have

8
00:00:28,890 --> 00:00:35,940
one object that is dependent on another
object in a way that is efficient.

9
00:00:35,940 --> 00:00:40,850
Now the Dependency Injection,
as we realize, has two parts.

10
00:00:40,850 --> 00:00:43,280
Dependency and injection.

11
00:00:43,280 --> 00:00:45,760
Let's talk about the dependency first.

12
00:00:45,760 --> 00:00:50,750
Dependency means that your object
is dependent on another object.

13
00:00:50,750 --> 00:00:55,400
So, that is why that is a dependency
between your object and another object.

14
00:00:56,500 --> 00:01:02,480
Injection is talking about passing
the dependency to a dependent object.

15
00:01:02,480 --> 00:01:06,570
So, if you have an object,
if there is a mechanism that allows

16
00:01:06,570 --> 00:01:11,410
you to be able to take an object and then
make it available to a second object, so

17
00:01:11,410 --> 00:01:15,360
that the other object can make
use of it without being aware of

18
00:01:15,360 --> 00:01:19,590
exactly how the first object
is implemented or cleared.

19
00:01:19,590 --> 00:01:24,950
So, in this case the object
that is dependent is unaware,

20
00:01:24,950 --> 00:01:29,050
or does not need to worry about how
the other object is implemented.

21
00:01:29,050 --> 00:01:34,250
It just needs to make use of it,
within your object, wherever it is.

22
00:01:35,490 --> 00:01:41,090
Dependency Injection was coined
by Martin Fowler in 2004.

23
00:01:41,090 --> 00:01:47,520
And is a useful software design
stressing further on dependency.

24
00:01:48,670 --> 00:01:52,070
If an object is dependent
on another object

25
00:01:52,070 --> 00:01:56,035
there are three ways it can
access the other object.

26
00:01:56,035 --> 00:02:01,420
1, It can create the dependent object
itself by using the new operator

27
00:02:01,420 --> 00:02:03,650
you have seen this in the case of classes.

28
00:02:03,650 --> 00:02:08,280
So, for example,
if you need a new object of a class type,

29
00:02:08,280 --> 00:02:14,592
then you create that object using
the new within languages, like C++ or

30
00:02:14,592 --> 00:02:20,830
Java, and even type script.

31
00:02:20,830 --> 00:02:26,850
Then, you can make use of
that particular object.

32
00:02:26,850 --> 00:02:30,950
The other way is to declare the other
object as a global variable, and

33
00:02:30,950 --> 00:02:34,780
then you look up the dependency
using the global variable.

34
00:02:34,780 --> 00:02:39,970
The third way of doing this is to
have the dependency passed into you,

35
00:02:39,970 --> 00:02:41,730
wherever it is needed.

36
00:02:41,730 --> 00:02:45,690
So if you're dependent on something else,
then that something else will be

37
00:02:45,690 --> 00:02:50,130
injected into you by a system
wherever it is required.

38
00:02:50,130 --> 00:02:54,680
So, now the third option gives you
a lot of flexibility in the way

39
00:02:54,680 --> 00:02:57,610
that software is designed.

40
00:02:57,610 --> 00:03:00,440
So in this approach,
in the third approach,

41
00:03:00,440 --> 00:03:05,560
there is no need for
hard coding after dependency.

42
00:03:05,560 --> 00:03:10,470
In the first two approaches,
the dependency is hard coded in there,

43
00:03:10,470 --> 00:03:13,470
because you need to be fully
aware of how you create

44
00:03:13,470 --> 00:03:18,180
the object that you are dependent upon,
within your own object.

45
00:03:18,180 --> 00:03:19,577
In the third object,

46
00:03:19,577 --> 00:03:24,389
because the other object is being
injected into you whenever you need it,

47
00:03:24,389 --> 00:03:29,220
you don't really care how it is created,
as long as you have access to it.

48
00:03:29,220 --> 00:03:34,125
And the other advantage that this
approach brings about is that it

49
00:03:34,125 --> 00:03:38,855
facilitates ease of testing your
application because when you

50
00:03:38,855 --> 00:03:43,760
need an object that you are dependent
upon, instead of passing in

51
00:03:43,760 --> 00:03:49,020
the dependent object you could even
create a mock object of the type and

52
00:03:49,020 --> 00:03:53,850
then pass it in in order to
test your specific object.

53
00:03:53,850 --> 00:03:57,530
This separation between the dependent
object and the other object is very,

54
00:03:57,530 --> 00:04:02,250
very useful as we realize
in carrying out testing.

55
00:04:02,250 --> 00:04:08,886
We'll discuss this briefly when we look at
angular testing later on in this course.

56
00:04:08,886 --> 00:04:15,850
Dependency Injection brings about four
different roles that we need to consider.

57
00:04:15,850 --> 00:04:20,770
First of course is the service that
you're going to make use of within

58
00:04:22,460 --> 00:04:27,160
your component for example and
that needs to be injected in.

59
00:04:27,160 --> 00:04:30,270
Second the client which is
dependent upon the service

60
00:04:30,270 --> 00:04:33,380
which in this case is your component.

61
00:04:33,380 --> 00:04:39,470
Third, the interface, once injected
how do you make use of that service.

62
00:04:39,470 --> 00:04:43,420
And finally the injector,
the entity that is responsible for

63
00:04:43,420 --> 00:04:47,230
injecting that object into your object.

64
00:04:48,620 --> 00:04:50,730
As you will pretty soon realize,

65
00:04:50,730 --> 00:04:56,540
angular express dependency injection
very much in the way it is implemented.

66
00:04:56,540 --> 00:05:01,090
So this is what facilitates
the separation of a business logic

67
00:05:01,090 --> 00:05:05,040
from the dependency construction so
you can write your business logic in

68
00:05:05,040 --> 00:05:09,660
the independent object and
then inject them wherever it is require.

69
00:05:09,660 --> 00:05:13,590
And the dependency is pass into
the object that is consuming it

70
00:05:13,590 --> 00:05:15,130
wherever it is needed.

71
00:05:15,130 --> 00:05:19,760
Now, how is this injection taken care of?

72
00:05:19,760 --> 00:05:24,380
This injection is taken care of by
the angular injection subsytem.

73
00:05:24,380 --> 00:05:30,250
The angular injection subsystem takes
care of creation of these services and

74
00:05:30,250 --> 00:05:35,120
then inject them into your
component wherever you need them.

75
00:05:35,120 --> 00:05:38,540
So it also takes care of resolving
the dependencies and also,

76
00:05:38,540 --> 00:05:45,030
providing those objects to other
components that require these objects.

77
00:05:45,030 --> 00:05:49,180
So the angular injector subsystem
provides all these mechanisms for

78
00:05:49,180 --> 00:05:50,770
us to enable this.

79
00:05:50,770 --> 00:05:52,450
How does it actually work?

80
00:05:52,450 --> 00:05:57,101
We will look at that in
the exercise that follows,

81
00:05:57,101 --> 00:06:02,527
where we will use a service that
we create In the components

82
00:06:02,527 --> 00:06:06,977
that we designed for
our Angular application.

83
00:06:06,977 --> 00:06:12,169
[MUSIC]