diff options
Diffstat (limited to '')
-rw-r--r-- | src/WixToolset.Core/WixToolsetServiceProvider.cs | 128 |
1 files changed, 51 insertions, 77 deletions
diff --git a/src/WixToolset.Core/WixToolsetServiceProvider.cs b/src/WixToolset.Core/WixToolsetServiceProvider.cs index a018b8dc..0c40c56d 100644 --- a/src/WixToolset.Core/WixToolsetServiceProvider.cs +++ b/src/WixToolset.Core/WixToolsetServiceProvider.cs | |||
@@ -3,6 +3,7 @@ | |||
3 | namespace WixToolset.Core | 3 | namespace WixToolset.Core |
4 | { | 4 | { |
5 | using System; | 5 | using System; |
6 | using System.Collections.Generic; | ||
6 | using WixToolset.Core.CommandLine; | 7 | using WixToolset.Core.CommandLine; |
7 | using WixToolset.Core.ExtensibilityServices; | 8 | using WixToolset.Core.ExtensibilityServices; |
8 | using WixToolset.Data; | 9 | using WixToolset.Data; |
@@ -11,95 +12,68 @@ namespace WixToolset.Core | |||
11 | 12 | ||
12 | public class WixToolsetServiceProvider : IServiceProvider | 13 | public class WixToolsetServiceProvider : IServiceProvider |
13 | { | 14 | { |
14 | private ExtensionManager extensionManager; | 15 | public WixToolsetServiceProvider() |
15 | private Messaging messaging; | ||
16 | private ParseHelper parseHelper; | ||
17 | private PreprocessHelper preprocessHelper; | ||
18 | private TupleDefinitionCreator tupleDefinitionCreator; | ||
19 | private WindowsInstallerBackendHelper windowsInstallerBackendHelper; | ||
20 | |||
21 | public object GetService(Type serviceType) | ||
22 | { | 16 | { |
23 | if (serviceType == null) throw new ArgumentNullException(nameof(serviceType)); | 17 | this.CreationFunctions = new Dictionary<Type, Func<IServiceProvider, Dictionary<Type, object>, object>> |
24 | |||
25 | // Transients. | ||
26 | if (serviceType == typeof(IPreprocessContext)) | ||
27 | { | ||
28 | return new PreprocessContext(this); | ||
29 | } | ||
30 | |||
31 | if (serviceType == typeof(ICompileContext)) | ||
32 | { | ||
33 | return new CompileContext(this); | ||
34 | } | ||
35 | |||
36 | if (serviceType == typeof(ILinkContext)) | ||
37 | { | ||
38 | return new LinkContext(this); | ||
39 | } | ||
40 | |||
41 | if (serviceType == typeof(IBindContext)) | ||
42 | { | ||
43 | return new BindContext(this); | ||
44 | } | ||
45 | |||
46 | if (serviceType == typeof(ILayoutContext)) | ||
47 | { | ||
48 | return new LayoutContext(this); | ||
49 | } | ||
50 | |||
51 | if (serviceType == typeof(IResolveContext)) | ||
52 | { | ||
53 | return new ResolveContext(this); | ||
54 | } | ||
55 | |||
56 | if (serviceType == typeof(IInscribeContext)) | ||
57 | { | ||
58 | return new InscribeContext(this); | ||
59 | } | ||
60 | |||
61 | if (serviceType == typeof(ICommandLineContext)) | ||
62 | { | 18 | { |
63 | return new CommandLineContext(this); | 19 | // Singletons. |
64 | } | 20 | { typeof(IExtensionManager), (provider, singletons) => AddSingleton(singletons, typeof(IExtensionManager), new ExtensionManager()) }, |
21 | { typeof(IMessaging), (provider, singletons) => AddSingleton(singletons, typeof(IMessaging), new Messaging()) }, | ||
22 | { typeof(ITupleDefinitionCreator), (provider, singletons) => AddSingleton(singletons, typeof(ITupleDefinitionCreator), new TupleDefinitionCreator(provider)) }, | ||
23 | { typeof(IParseHelper), (provider, singletons) => AddSingleton(singletons, typeof(IParseHelper), new ParseHelper(provider)) }, | ||
24 | { typeof(IPreprocessHelper), (provider, singletons) => AddSingleton(singletons, typeof(IPreprocessHelper), new PreprocessHelper(provider)) }, | ||
25 | { typeof(IWindowsInstallerBackendHelper), (provider, singletons) => AddSingleton(singletons, typeof(IWindowsInstallerBackendHelper), new WindowsInstallerBackendHelper(provider)) }, | ||
65 | 26 | ||
66 | if (serviceType == typeof(ICommandLine)) | 27 | // Transients. |
67 | { | 28 | { typeof(ICommandLineContext), (provider, singletons) => new CommandLineContext(provider) }, |
68 | return new CommandLineParser(); | 29 | { typeof(ICommandLine), (provider, singletons) => new CommandLineParser() }, |
69 | } | 30 | { typeof(IPreprocessContext), (provider, singletons) => new PreprocessContext(provider) }, |
31 | { typeof(ICompileContext), (provider, singletons) => new CompileContext(provider) }, | ||
32 | { typeof(ILinkContext), (provider, singletons) => new LinkContext(provider) }, | ||
33 | { typeof(IResolveContext), (provider, singletons) => new ResolveContext(provider) }, | ||
34 | { typeof(IBindContext), (provider, singletons) => new BindContext(provider) }, | ||
35 | { typeof(ILayoutContext), (provider, singletons) => new LayoutContext(provider) }, | ||
36 | { typeof(IInscribeContext), (provider, singletons) => new InscribeContext(provider) }, | ||
37 | }; | ||
38 | |||
39 | this.Singletons = new Dictionary<Type, object>(); | ||
40 | } | ||
70 | 41 | ||
71 | // Singletons. | 42 | private Dictionary<Type, Func<IServiceProvider, Dictionary<Type, object>, object>> CreationFunctions { get; } |
72 | if (serviceType == typeof(IExtensionManager)) | ||
73 | { | ||
74 | return this.extensionManager = this.extensionManager ?? new ExtensionManager(); | ||
75 | } | ||
76 | 43 | ||
77 | if (serviceType == typeof(IMessaging)) | 44 | private Dictionary<Type, object> Singletons { get; } |
78 | { | ||
79 | return this.messaging = this.messaging ?? new Messaging(); | ||
80 | } | ||
81 | 45 | ||
82 | if (serviceType == typeof(ITupleDefinitionCreator)) | 46 | public object GetService(Type serviceType) |
83 | { | 47 | { |
84 | return this.tupleDefinitionCreator = this.tupleDefinitionCreator ?? new TupleDefinitionCreator(this); | 48 | if (serviceType == null) throw new ArgumentNullException(nameof(serviceType)); |
85 | } | ||
86 | 49 | ||
87 | if (serviceType == typeof(IParseHelper)) | 50 | if (!this.Singletons.TryGetValue(serviceType, out var service)) |
88 | { | 51 | { |
89 | return this.parseHelper = this.parseHelper ?? new ParseHelper(this); | 52 | if (this.CreationFunctions.TryGetValue(serviceType, out var creationFunction)) |
53 | { | ||
54 | service = creationFunction(this, this.Singletons); | ||
55 | |||
56 | #if DEBUG | ||
57 | if (!serviceType.IsAssignableFrom(service?.GetType())) | ||
58 | { | ||
59 | throw new InvalidOperationException($"Creation function for service type: {serviceType.Name} created incompatible service with type: {service?.GetType()}"); | ||
60 | } | ||
61 | #endif | ||
62 | } | ||
90 | } | 63 | } |
91 | 64 | ||
92 | if (serviceType == typeof(IPreprocessHelper)) | 65 | return service ?? throw new ArgumentException($"Unknown service type: {serviceType.Name}", nameof(serviceType)); |
93 | { | 66 | } |
94 | return this.preprocessHelper = this.preprocessHelper ?? new PreprocessHelper(this); | ||
95 | } | ||
96 | 67 | ||
97 | if (serviceType == typeof(IWindowsInstallerBackendHelper)) | 68 | public void AddService(Type serviceType, Func<IServiceProvider, Dictionary<Type, object>, object> creationFunction) |
98 | { | 69 | { |
99 | return this.windowsInstallerBackendHelper = this.windowsInstallerBackendHelper ?? new WindowsInstallerBackendHelper(this); | 70 | this.CreationFunctions[serviceType] = creationFunction; |
100 | } | 71 | } |
101 | 72 | ||
102 | throw new ArgumentException($"Unknown service type: {serviceType.Name}", nameof(serviceType)); | 73 | private static object AddSingleton(Dictionary<Type, object> singletons, Type type, object service) |
74 | { | ||
75 | singletons.Add(type, service); | ||
76 | return service; | ||
103 | } | 77 | } |
104 | } | 78 | } |
105 | } | 79 | } |