forked from ashyrokoriadov/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
152 lines (132 loc) · 6.42 KB
/
Program.cs
File metadata and controls
152 lines (132 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using Autofac;
using Observer.Constants;
using Observer.Events;
using Observer.Implementations;
using Observer.Interfaces;
using Observer.Models;
using Observer.ObservableEntities;
using Observer.Repos;
using Observer.Subscribers;
using System;
namespace Observer
{
class Program
{
static ISubscriber<MessageSent> _emailNotifications;
static ISubscriber<MessageSent> _smsNotifications;
static ISubscriber<MessageSent> _uiNotifications;
static MessageBox _messageBox;
static void Main(string[] args)
{
RegisterDependencies();
InitaliseUserRepo();
using (var scope = Container.BeginLifetimeScope())
{
_messageBox = scope.Resolve<MessageBox>();
_emailNotifications
= scope.ResolveNamed<ISubscriber<MessageSent>>(SubscribersTypes.EMAIL_SUBSCRIBER);
_smsNotifications
= scope.ResolveNamed<ISubscriber<MessageSent>>(SubscribersTypes.SMS_SUBSCRIBER);
_uiNotifications
= scope.ResolveNamed<ISubscriber<MessageSent>>(SubscribersTypes.UI_SUBSCRIBER);
DisplayMenu();
while (true)
{
var key = Console.ReadKey().Key;
Console.WriteLine();
if (key == ConsoleKey.Q)
break;
HandleUserInput(key);
}
}
}
private static void InitaliseUserRepo()
{
using (var scope = Container.BeginLifetimeScope())
{
var userRepo = scope.Resolve<IRepo<User>>();
user1.IsLoggedIn = true;
userRepo.Add(user1);
user2.IsLoggedIn = false;
userRepo.Add(user2);
user3.IsLoggedIn = true;
userRepo.Add(user3);
}
}
private static void DisplayMenu()
{
Console.WriteLine("Отправка сообщений:");
Console.WriteLine("\t - нажмите А чтобы отправить сообщение Александру");
Console.WriteLine("\t - нажмите М чтобы отправить сообщение Михаилу");
Console.WriteLine("\t - нажмите Т чтобы отправить сообщение Татьяне");
Console.WriteLine("");
Console.WriteLine("Подписка поставщиков уведомлений:");
Console.WriteLine($"\t - нажмите G чтобы подписать {SubscribersTypes.EMAIL_SUBSCRIBER}");
Console.WriteLine($"\t - нажмите H чтобы подписать {SubscribersTypes.SMS_SUBSCRIBER}");
Console.WriteLine($"\t - нажмите J чтобы подписать {SubscribersTypes.UI_SUBSCRIBER}");
Console.WriteLine("");
Console.WriteLine("Подписка поставщиков уведомлений:");
Console.WriteLine($"\t - нажмите V чтобы отписать {SubscribersTypes.EMAIL_SUBSCRIBER}");
Console.WriteLine($"\t - нажмите B чтобы отписать {SubscribersTypes.SMS_SUBSCRIBER}");
Console.WriteLine($"\t - нажмите N чтобы отписать {SubscribersTypes.UI_SUBSCRIBER}");
Console.WriteLine("");
Console.WriteLine("Нажмите Q чтобы выйти из приложения");
Console.WriteLine("");
}
private static void HandleUserInput(ConsoleKey key)
{
switch (key)
{
case ConsoleKey.A:
_messageBox.SendMessage(new Message("Андрей", "Александр", "Привет!"));
break;
case ConsoleKey.M:
_messageBox.SendMessage(new Message("Андрей", "Михаил", "Привет!"));
break;
case ConsoleKey.T:
_messageBox.SendMessage(new Message("Андрей", "Татьяна", "Привет!"));
break;
case ConsoleKey.G:
_messageBox.Subscribe(_emailNotifications);
break;
case ConsoleKey.H:
_messageBox.Subscribe(_smsNotifications);
break;
case ConsoleKey.J:
_messageBox.Subscribe(_uiNotifications);
break;
case ConsoleKey.V:
_messageBox.Unsubscribe(_emailNotifications);
break;
case ConsoleKey.B:
_messageBox.Unsubscribe(_smsNotifications);
break;
case ConsoleKey.N:
_messageBox.Unsubscribe(_uiNotifications);
break;
}
}
private static void RegisterDependencies()
{
Builder.RegisterType<GenericRepo<User>>().As<IRepo<User>>().SingleInstance();
Builder.RegisterType<GenericRepo<ISubscriber<MessageSent>>>()
.As<IRepo<ISubscriber<MessageSent>>>()
.SingleInstance();
Builder.RegisterType<GoogleMessageSender>().As<IMessageSender<Message>>();
Builder.RegisterType<MessageBox>().AsSelf().SingleInstance();
Builder.RegisterType<EmailNotificationDispatcher>()
.Named<ISubscriber<MessageSent>>(SubscribersTypes.EMAIL_SUBSCRIBER);
Builder.RegisterType<SmsNotificationDispatcher>()
.Named<ISubscriber<MessageSent>>(SubscribersTypes.SMS_SUBSCRIBER);
Builder.RegisterType<UiNotificationDispatcher>()
.Named<ISubscriber<MessageSent>>(SubscribersTypes.UI_SUBSCRIBER);
}
private static IContainer _container;
public static IContainer Container => _container ?? (_container = Builder.Build());
private static ContainerBuilder _builder;
public static ContainerBuilder Builder => _builder ?? (_builder = new ContainerBuilder());
}
}