Use generics in WixToolsetServiceProvider to make the compiler help with the type checking.
Use generics in WixToolsetServiceProvider to make the compiler help with the type checking.
Sean Hall committed
Sep 2, 2018 at 14:18 UTC
2b41623f6d75acf6301f689db31570888c8abb3e
1 file changed
+31
-26
src/WixToolset.Core/WixToolsetServiceProvider.cs
+31
-26
@@ -14,34 +14,32 @@ namespace WixToolset.Core
14
{
15
public WixToolsetServiceProvider()
16
{
17
- this.CreationFunctions = new Dictionary<Type, Func<IServiceProvider, Dictionary<Type, object>, object>>
18
- {
17
+ this.CreationFunctions = new Dictionary<Type, Func<IServiceProvider, Dictionary<Type, object>, object>>();
18
+ this.Singletons = new Dictionary<Type, object>();
19
+
20
// Singletons.
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(IBackendHelper), (provider, singletons) => AddSingleton(singletons, typeof(IBackendHelper), new BackendHelper(provider)) },
26
- { typeof(IWindowsInstallerBackendHelper), (provider, singletons) => AddSingleton(singletons, typeof(IWindowsInstallerBackendHelper), new WindowsInstallerBackendHelper(provider)) },
21
+ this.AddService((provider, singletons) => AddSingleton<IExtensionManager>(singletons, new ExtensionManager()));
22
+ this.AddService((provider, singletons) => AddSingleton<IMessaging>(singletons, new Messaging()));
23
+ this.AddService((provider, singletons) => AddSingleton<ITupleDefinitionCreator>(singletons, new TupleDefinitionCreator(provider)));
24
+ this.AddService((provider, singletons) => AddSingleton<IParseHelper>(singletons, new ParseHelper(provider)));
25
+ this.AddService((provider, singletons) => AddSingleton<IPreprocessHelper>(singletons, new PreprocessHelper(provider)));
26
+ this.AddService((provider, singletons) => AddSingleton<IBackendHelper>(singletons, new BackendHelper(provider)));
27
+ this.AddService((provider, singletons) => AddSingleton<IWindowsInstallerBackendHelper>(singletons, new WindowsInstallerBackendHelper(provider)));
28
29
// Transients.
29
- { typeof(ICommandLineArguments), (provider, singletons) => new CommandLineArguments(provider) },
30
- { typeof(ICommandLineContext), (provider, singletons) => new CommandLineContext(provider) },
31
- { typeof(ICommandLineParser), (provider, singletons) => new CommandLineParser(provider) },
32
- { typeof(IPreprocessContext), (provider, singletons) => new PreprocessContext(provider) },
33
- { typeof(ICompileContext), (provider, singletons) => new CompileContext(provider) },
34
- { typeof(ILinkContext), (provider, singletons) => new LinkContext(provider) },
35
- { typeof(IResolveContext), (provider, singletons) => new ResolveContext(provider) },
36
- { typeof(IBindContext), (provider, singletons) => new BindContext(provider) },
37
- { typeof(ILayoutContext), (provider, singletons) => new LayoutContext(provider) },
38
- { typeof(IInscribeContext), (provider, singletons) => new InscribeContext(provider) },
39
-
40
- // Internal implementations.
41
- { typeof(ILocalizer), (provider, singletons) => new Localizer(provider) },
42
- };
30
+ this.AddService<ICommandLineArguments>((provider, singletons) => new CommandLineArguments(provider));
31
+ this.AddService<ICommandLineContext>((provider, singletons) => new CommandLineContext(provider));
32
+ this.AddService<ICommandLineParser>((provider, singletons) => new CommandLineParser(provider));
33
+ this.AddService<IPreprocessContext>((provider, singletons) => new PreprocessContext(provider));
34
+ this.AddService<ICompileContext>((provider, singletons) => new CompileContext(provider));
35
+ this.AddService<ILinkContext>((provider, singletons) => new LinkContext(provider));
36
+ this.AddService<IResolveContext>((provider, singletons) => new ResolveContext(provider));
37
+ this.AddService<IBindContext>((provider, singletons) => new BindContext(provider));
38
+ this.AddService<ILayoutContext>((provider, singletons) => new LayoutContext(provider));
39
+ this.AddService<IInscribeContext>((provider, singletons) => new InscribeContext(provider));
40
44
- this.Singletons = new Dictionary<Type, object>();
41
+ // Internal implementations.
42
+ this.AddService<ILocalizer>((provider, singletons) => new Localizer(provider));
43
}
44
45
private Dictionary<Type, Func<IServiceProvider, Dictionary<Type, object>, object>> CreationFunctions { get; }
@@ -80,9 +78,16 @@ namespace WixToolset.Core
78
this.CreationFunctions[serviceType] = creationFunction;
79
}
80
83
- private static object AddSingleton(Dictionary<Type, object> singletons, Type type, object service)
81
+ public void AddService<T>(Func<IServiceProvider, Dictionary<Type, object>, T> creationFunction)
82
+ where T : class
83
+ {
84
+ AddService(typeof(T), creationFunction);
85
+ }
86
+
87
+ private static T AddSingleton<T>(Dictionary<Type, object> singletons, T service)
88
+ where T : class
89
{
85
- singletons.Add(type, service);
90
+ singletons.Add(typeof(T), service);
91
return service;
92
}
93
}