1
00:00:01,070 --> 00:00:03,100
These days we're newing up objects like

2
00:00:03,100 --> 00:00:05,590
you see here less and less, and instead

3
00:00:05,590 --> 00:00:07,460
we're often using dependency injection to

4
00:00:07,460 --> 00:00:09,700
create our objects for us. So let's

5
00:00:09,700 --> 00:00:11,470
understand how we can use the decorator

6
00:00:11,470 --> 00:00:13,130
pattern when we're using dependency

7
00:00:13,130 --> 00:00:16,110
injection in our applications. If you're

8
00:00:16,110 --> 00:00:18,170
not familiar with dependency injection,

9
00:00:18,170 --> 00:00:19,480
this is a technique you're going to want

10
00:00:19,480 --> 00:00:21,560
to get good at because it really is one of

11
00:00:21,560 --> 00:00:23,490
the cornerstones of modern application

12
00:00:23,490 --> 00:00:25,710
development. We have an excellent course

13
00:00:25,710 --> 00:00:28,040
here on Pluralsight by my friend, Jeremy

14
00:00:28,040 --> 00:00:29,120
Clark, that will give you a solid

15
00:00:29,120 --> 00:00:31,090
foundation in what dependency injection is

16
00:00:31,090 --> 00:00:33,720
all about and how it is used. So if that

17
00:00:33,720 --> 00:00:35,610
is of interest to you, be sure to check

18
00:00:35,610 --> 00:00:38,070
that course out. I've refactored our

19
00:00:38,070 --> 00:00:40,030
controller class to make use of dependency

20
00:00:40,030 --> 00:00:42,240
injection, and now you can see we have an

21
00:00:42,240 --> 00:00:44,760
IWeatherService object being injected into

22
00:00:44,760 --> 00:00:47,080
our constructor, and we no longer have all

23
00:00:47,080 --> 00:00:49,050
of that code we had before that was

24
00:00:49,050 --> 00:00:50,500
instantiating our service object and

25
00:00:50,500 --> 00:00:53,080
decorator objects. The creation of an

26
00:00:53,080 --> 00:00:55,270
appropriate IWeatherService object is now

27
00:00:55,270 --> 00:00:57,340
the responsibility of the IoC, or

28
00:00:57,340 --> 00:01:00,080
Inversion of Control Container. How the

29
00:01:00,080 --> 00:01:02,380
IoC container is configured varies

30
00:01:02,380 --> 00:01:04,410
depending on if you're using .NET Core or

31
00:01:04,410 --> 00:01:06,970
.NET Framework, so we'll look at examples

32
00:01:06,970 --> 00:01:10,830
of each. If you're using .NET Core, most

33
00:01:10,830 --> 00:01:12,840
likely you're using the built‑in IoC

34
00:01:12,840 --> 00:01:14,660
Container. And if you're simply

35
00:01:14,660 --> 00:01:16,480
configuring the WeatherService class in

36
00:01:16,480 --> 00:01:18,300
the container, your code would look

37
00:01:18,300 --> 00:01:20,480
something like this: what you see on lines

38
00:01:20,480 --> 00:01:24,180
35 and 36. So how do we add decorators to

39
00:01:24,180 --> 00:01:26,230
this configuration? There are two

40
00:01:26,230 --> 00:01:29,580
different ways. First, we can take control

41
00:01:29,580 --> 00:01:32,550
of the instantiation process. We already

42
00:01:32,550 --> 00:01:35,130
see on line 36 that I'm providing a lambda

43
00:01:35,130 --> 00:01:37,590
function to new up a WeatherService API

44
00:01:37,590 --> 00:01:40,450
object and provided an API key as part of

45
00:01:40,450 --> 00:01:42,930
the constructor. I could expand this

46
00:01:42,930 --> 00:01:45,430
lambda function out like this, though. And

47
00:01:45,430 --> 00:01:47,240
now you see that familiar code from the

48
00:01:47,240 --> 00:01:50,370
last module within the lambda function. So

49
00:01:50,370 --> 00:01:52,760
now, when the HomeController or any other

50
00:01:52,760 --> 00:01:54,850
piece of code in our application needs an

51
00:01:54,850 --> 00:01:57,400
IWeatherService object, this function is

52
00:01:57,400 --> 00:02:00,210
going to be executed. And as you can see,

53
00:02:00,210 --> 00:02:01,790
this is going to new up the WeatherService

54
00:02:01,790 --> 00:02:05,030
object surrounded by these decorators. If

55
00:02:05,030 --> 00:02:07,060
we wanted to add another decorator, we

56
00:02:07,060 --> 00:02:08,740
would just come in and we would modify the

57
00:02:08,740 --> 00:02:12,100
code in this lambda function. If you

58
00:02:12,100 --> 00:02:14,110
prefer a more fluent syntax, you can

59
00:02:14,110 --> 00:02:16,540
install a NuGet package called Scrutor,

60
00:02:16,540 --> 00:02:18,270
and you can see the NuGet install

61
00:02:18,270 --> 00:02:21,590
information on this page. If you click on

62
00:02:21,590 --> 00:02:23,640
the Project Site link, that will take you

63
00:02:23,640 --> 00:02:25,630
to the GitHub repository for the project,

64
00:02:25,630 --> 00:02:27,360
which has the documentation about how to

65
00:02:27,360 --> 00:02:30,230
use the library. What this looks like in

66
00:02:30,230 --> 00:02:33,630
our example project is like this. As you

67
00:02:33,630 --> 00:02:36,560
can see, these first 2 lines, lines 35 and

68
00:02:36,560 --> 00:02:38,550
36, are the same as our original

69
00:02:38,550 --> 00:02:41,580
configuration. What Scrutor gives us,

70
00:02:41,580 --> 00:02:44,010
among other things, is this additional

71
00:02:44,010 --> 00:02:46,780
extension method, Decorate, and as you can

72
00:02:46,780 --> 00:02:49,300
see, that takes a lambda function, and

73
00:02:49,300 --> 00:02:52,180
this lambda function has two arguments.

74
00:02:52,180 --> 00:02:54,560
The first object, inner, represents the

75
00:02:54,560 --> 00:02:56,890
object that the decorator will be wrapping

76
00:02:56,890 --> 00:02:59,040
around, so the decorated object. And the

77
00:02:59,040 --> 00:03:01,100
second argument represents the IoC

78
00:03:01,100 --> 00:03:03,770
Container, which in our case we need so we

79
00:03:03,770 --> 00:03:06,000
can get instances of the logger and memory

80
00:03:06,000 --> 00:03:08,800
cache objects to inject into our decorator

81
00:03:08,800 --> 00:03:11,980
objects. Both of these methods work

82
00:03:11,980 --> 00:03:14,310
equally well and is really just about

83
00:03:14,310 --> 00:03:16,980
which syntax you prefer. Personally, I

84
00:03:16,980 --> 00:03:18,740
like the fluent syntax that Scrutor

85
00:03:18,740 --> 00:03:20,410
provides because I think it's a little bit

86
00:03:20,410 --> 00:03:22,570
more readable, but you can choose either

87
00:03:22,570 --> 00:03:27,040
approach. If you're using ASP.NET MVC 5,

88
00:03:27,040 --> 00:03:28,690
you're probably going to be using either

89
00:03:28,690 --> 00:03:31,810
Autofac or Ninject for your IoC Container.

90
00:03:31,810 --> 00:03:33,910
And in this case, what you want to do is

91
00:03:33,910 --> 00:03:35,720
go to the documentation page for the

92
00:03:35,720 --> 00:03:37,700
library that you're using and do a search

93
00:03:37,700 --> 00:03:40,830
on decorators. Here, I'm looking at the

94
00:03:40,830 --> 00:03:43,520
documentation for Autofac, and you can see

95
00:03:43,520 --> 00:03:45,220
that they give examples of how to wire

96
00:03:45,220 --> 00:03:47,950
things up. The important thing isn't to

97
00:03:47,950 --> 00:03:50,190
remember this exact syntax, but to

98
00:03:50,190 --> 00:03:51,870
remember that if you ever need it, you can

99
00:03:51,870 --> 00:03:54,550
come here and look it up. If you take this

100
00:03:54,550 --> 00:03:56,450
and translate that into our sample

101
00:03:56,450 --> 00:03:58,590
project, this is what things look like

102
00:03:58,590 --> 00:04:02,480
when using Autofac as an IoC Container.

103
00:04:02,480 --> 00:04:03,890
Again, when our HomeController or any

104
00:04:03,890 --> 00:04:05,120
other piece of code needs an

105
00:04:05,120 --> 00:04:07,590
IWeatherService object, it will get not

106
00:04:07,590 --> 00:04:09,330
just a WeatherService object, but an

107
00:04:09,330 --> 00:04:11,130
object that has been decorated as we've

108
00:04:11,130 --> 00:04:14,510
specified here. So the big takeaway from

109
00:04:14,510 --> 00:04:16,820
this is you can use decorators even when

110
00:04:16,820 --> 00:04:19,490
you're using dependency injection in your

111
00:04:19,490 --> 00:04:21,440
applications. You'll get that same wrapped

112
00:04:21,440 --> 00:04:23,510
object structure like we saw before when

113
00:04:23,510 --> 00:04:25,695
we were newing up our objects ourselves,

114
00:04:25,695 --> 00:04:26,990
it's just configured a little bit

115
00:04:26,990 --> 00:04:32,000
differently, and in this clip, we've seen examples of how to do that.

