1
00:00:00,520 --> 00:00:02,400
Let's see how some of these situations

2
00:00:02,400 --> 00:00:03,940
play out to make things a bit more

3
00:00:03,940 --> 00:00:07,510
tangible. And let's do that with some real

4
00:00:07,510 --> 00:00:11,620
code this time. We'll start by dissecting

5
00:00:11,620 --> 00:00:14,500
our initial main method. And here we're

6
00:00:14,500 --> 00:00:17,850
using three services, a GeoLookupService,

7
00:00:17,850 --> 00:00:19,970
a WeatherService, and a ConverterService,

8
00:00:19,970 --> 00:00:21,790
all to find out that the current

9
00:00:21,790 --> 00:00:24,640
temperature in both Fahrenheit and Celsius

10
00:00:24,640 --> 00:00:27,750
is a certain value, in a given city, in a

11
00:00:27,750 --> 00:00:31,060
given state, when our input value is a zip

12
00:00:31,060 --> 00:00:33,590
code. Notice we're newing up three

13
00:00:33,590 --> 00:00:36,360
services and using them here. Well,

14
00:00:36,360 --> 00:00:38,540
certainly that's a viable way to go about

15
00:00:38,540 --> 00:00:40,290
solving this, but we're orchestrating and

16
00:00:40,290 --> 00:00:43,280
managing a lot of variables here. So, what

17
00:00:43,280 --> 00:00:45,980
if we put these services behind a facade,

18
00:00:45,980 --> 00:00:47,640
and we wouldn't have to worry about this

19
00:00:47,640 --> 00:00:50,605
level of complexity. To start, I'm going

20
00:00:50,605 --> 00:00:53,295
to come into my facade project. I'm going

21
00:00:53,295 --> 00:00:57,800
to add a new class, and instead of just

22
00:00:57,800 --> 00:00:59,540
going and creating the facade itself, I'm

23
00:00:59,540 --> 00:01:05,070
going to create an IWeatherFacade, and an

24
00:01:05,070 --> 00:01:08,170
IWeatherFacade is obviously going to be an

25
00:01:08,170 --> 00:01:13,960
interface, not a class. But why would I

26
00:01:13,960 --> 00:01:15,430
start off with an interface? Well, I

27
00:01:15,430 --> 00:01:17,000
normally wouldn't do this, I would

28
00:01:17,000 --> 00:01:19,800
normally refactor an interface out of a

29
00:01:19,800 --> 00:01:22,240
concrete type once I needed to generalize

30
00:01:22,240 --> 00:01:24,360
that concrete type. In this case, we're

31
00:01:24,360 --> 00:01:25,920
going to start with an interface just to

32
00:01:25,920 --> 00:01:28,840
show what top‑down development looks like

33
00:01:28,840 --> 00:01:30,670
given the system architecture we saw on

34
00:01:30,670 --> 00:01:33,120
our slides. Okay, so what goes in our

35
00:01:33,120 --> 00:01:36,130
interface? One method, that's all we need,

36
00:01:36,130 --> 00:01:40,610
GetTempInCity, given a certain zip code.

37
00:01:40,610 --> 00:01:42,500
Well, we're returning a

38
00:01:42,500 --> 00:01:44,730
WeatherFacadeResults class, which we don't

39
00:01:44,730 --> 00:01:48,850
have yet. So, let's create that,

40
00:01:48,850 --> 00:01:50,190
WeatherFacadeResults. We're just going to

41
00:01:50,190 --> 00:01:52,540
pop in some getters and setters here. This

42
00:01:52,540 --> 00:01:55,330
is nothing more than a simple data

43
00:01:55,330 --> 00:01:58,150
transfer object. And notice, we're going

44
00:01:58,150 --> 00:02:00,270
to be setting properties of Fahrenheit,

45
00:02:00,270 --> 00:02:02,400
Celsius, City, and State. Those are the

46
00:02:02,400 --> 00:02:04,310
things we need to output in our main

47
00:02:04,310 --> 00:02:07,680
method. Let's take our public class and

48
00:02:07,680 --> 00:02:11,480
send it to its own code file. By

49
00:02:11,480 --> 00:02:14,200
convention, perfect. Now let's go back

50
00:02:14,200 --> 00:02:17,040
over to our IWeatherFacade. Well, we don't

51
00:02:17,040 --> 00:02:19,470
actually have a weather facade yet, so

52
00:02:19,470 --> 00:02:21,920
let's create a drive type. We'll make it

53
00:02:21,920 --> 00:02:25,760
public, and we're going to implement the

54
00:02:25,760 --> 00:02:28,060
missing members of IWeatherFacade. There

55
00:02:28,060 --> 00:02:30,280
they are, it is, I should say, it's that

56
00:02:30,280 --> 00:02:33,180
simple. GetTempInCity. So what is our

57
00:02:33,180 --> 00:02:35,330
weather facade going to need to do? We're

58
00:02:35,330 --> 00:02:36,800
actually going to put in a lot of setup

59
00:02:36,800 --> 00:02:40,270
code here. What we're doing is we're

60
00:02:40,270 --> 00:02:44,680
holding our services as fields in our

61
00:02:44,680 --> 00:02:47,880
class. We've got one constructor that will

62
00:02:47,880 --> 00:02:50,760
new them up for us, and we've got another

63
00:02:50,760 --> 00:02:52,290
constructor that can take them in as

64
00:02:52,290 --> 00:02:55,660
parameters. Some talk about this pattern

65
00:02:55,660 --> 00:02:58,700
as being poor man's Inversion of Control.

66
00:02:58,700 --> 00:03:00,490
Now let's come down a bit. What do we put

67
00:03:00,490 --> 00:03:03,200
inside our GetTempInCity? Well, here's

68
00:03:03,200 --> 00:03:05,970
where we're actually making use of these

69
00:03:05,970 --> 00:03:08,220
services and storing them back into

70
00:03:08,220 --> 00:03:11,160
variables. Just go ahead and clean that

71
00:03:11,160 --> 00:03:14,320
up, convert Fahrenheit to Celsius, and you

72
00:03:14,320 --> 00:03:17,350
can see that we're newing up our result

73
00:03:17,350 --> 00:03:19,810
that's got City, State, Fahrenheit, and

74
00:03:19,810 --> 00:03:22,400
Celsius on it. And we return that as a set

75
00:03:22,400 --> 00:03:25,520
of results. Now, finally, let's come back

76
00:03:25,520 --> 00:03:28,680
and look at our program. Our program no

77
00:03:28,680 --> 00:03:30,490
longer needs to manage any of these

78
00:03:30,490 --> 00:03:34,340
things. What we can do instead is so much

79
00:03:34,340 --> 00:03:36,570
simpler. We can instantiate a weather

80
00:03:36,570 --> 00:03:39,200
facade behind an interface that we might

81
00:03:39,200 --> 00:03:42,120
use for a different implementation later.

82
00:03:42,120 --> 00:03:44,570
We then get our results by calling the

83
00:03:44,570 --> 00:03:45,850
weatherFacade.GetTempInCity method,

84
00:03:45,850 --> 00:03:48,210
passing a zipCode. Now, we've got those

85
00:03:48,210 --> 00:03:51,340
results and we can write them out. Let's

86
00:03:51,340 --> 00:03:53,620
run and see if we've got everything in

87
00:03:53,620 --> 00:03:58,440
place. Run the project, and it looks like

88
00:03:58,440 --> 00:04:00,800
it's 53 degrees Fahrenheit, 11 degrees

89
00:04:00,800 --> 00:04:04,000
Celsius in Redmond, Washington. That's just about right.

