-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactoryStore.cs
44 lines (39 loc) · 1.5 KB
/
FactoryStore.cs
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
using System;
using System.Collections.Generic;
using SyncWhole.Common;
using SyncWhole.Google;
using SyncWhole.Outlook;
using SyncWhole.Properties;
namespace SyncWhole
{
public static class FactoryStore
{
private static readonly Dictionary<char, Func<string, IAppointmentSourceFactory>> SourceFactoryMap =
new Dictionary<char, Func<string, IAppointmentSourceFactory>>
{
{'O', _ => new OutlookAdapterFactory()},
};
private static readonly Dictionary<char, Func<string, IAppointmentDestinationFactory>> DestinationFactoryMap =
new Dictionary<char, Func<string, IAppointmentDestinationFactory>>
{
{'G', arg => new GoogleCalendarAdapterFactory(arg)},
};
private static T BuildFactoryFromString<T>(string serializedFactory, IReadOnlyDictionary<char, Func<string, T>> factoryMap)
{
if (string.IsNullOrEmpty(serializedFactory))
{
throw new ArgumentNullException(nameof(serializedFactory));
}
if (!factoryMap.TryGetValue(serializedFactory[0], out var func))
{
throw new ArgumentOutOfRangeException(nameof(serializedFactory),
$"Unknown factory: \"{serializedFactory}\"");
}
return func(serializedFactory.Substring(1));
}
public static IAppointmentSourceFactory CurrentSource =>
BuildFactoryFromString(Settings.Default.CalendarSource, SourceFactoryMap);
public static IAppointmentDestinationFactory CurrentDestination =>
BuildFactoryFromString(Settings.Default.CalendarDestination, DestinationFactoryMap);
}
}