@joebigelow / wix / commits / 9aae370e

Use extension methods instead of a custom interface for IServiceProvider

There are several helpful methods for getting services out of an IServiceProvider. Instead of introducing a custom interface to inject those methods into the inheritance tree, this change uses extension methods to add the helper methods and reduce the number of custom interfaces.

Rob Mensching committed Mar 14, 2021 at 11:17 UTC 9aae370eee18b4c87300f333fa52f3cd0d7f34d1
14 files changed +100 -28
src/WixToolset.Extensibility/Data/IBindContext.cs
+63 -4
@@ -2,50 +2,109 @@
2
3 namespace WixToolset.Extensibility.Data
4 {
5 + using System;
6 using System.Collections.Generic;
7 using System.Threading;
8 using WixToolset.Data;
8 - using WixToolset.Extensibility.Services;
9
10 -#pragma warning disable 1591 // TODO: add documentation
10 + /// <summary>
11 + /// Bind context.
12 + /// </summary>
13 public interface IBindContext
14 {
13 - IWixToolsetServiceProvider ServiceProvider { get; }
14 -
15 + /// <summary>
16 + /// Service provider.
17 + /// </summary>
18 + IServiceProvider ServiceProvider { get; }
19 +
20 + /// <summary>
21 + /// Counnt of threads to use in cabbing.
22 + /// </summary>
23 int CabbingThreadCount { get; set; }
24
25 + /// <summary>
26 + /// Cabinet cache path.
27 + /// </summary>
28 string CabCachePath { get; set; }
29
30 + /// <summary>
31 + /// Codepage for result.
32 + /// </summary>
33 int Codepage { get; set; }
34
35 + /// <summary>
36 + /// Default compression level.
37 + /// </summary>
38 CompressionLevel? DefaultCompressionLevel { get; set; }
39
40 + /// <summary>
41 + /// Delayed fields that need to be resolved again.
42 + /// </summary>
43 IEnumerable<IDelayedField> DelayedFields { get; set; }
44
45 + /// <summary>
46 + /// Embedded files to extract.
47 + /// </summary>
48 IEnumerable<IExpectedExtractFile> ExpectedEmbeddedFiles { get; set; }
49
50 + /// <summary>
51 + /// Binder extensions.
52 + /// </summary>
53 IEnumerable<IBinderExtension> Extensions { get; set; }
54
55 + /// <summary>
56 + /// File system extensions.
57 + /// </summary>
58 IEnumerable<IFileSystemExtension> FileSystemExtensions { get; set; }
59
60 + /// <summary>
61 + /// Set of ICEs to execute.
62 + /// </summary>
63 IEnumerable<string> Ices { get; set; }
64
65 + /// <summary>
66 + /// Intermedaite folder.
67 + /// </summary>
68 string IntermediateFolder { get; set; }
69
70 + /// <summary>
71 + /// Intermediate representation to bind.
72 + /// </summary>
73 Intermediate IntermediateRepresentation { get; set; }
74
75 + /// <summary>
76 + /// Output path to bind to.
77 + /// </summary>
78 string OutputPath { get; set; }
79
80 + /// <summary>
81 + /// Type of PDB to create.
82 + /// </summary>
83 PdbType PdbType { get; set; }
84
85 + /// <summary>
86 + /// Output path for PDB.
87 + /// </summary>
88 string PdbPath { get; set; }
89
90 + /// <summary>
91 + /// Set of ICEs to skip.
92 + /// </summary>
93 IEnumerable<string> SuppressIces { get; set; }
94
95 + /// <summary>
96 + /// Skip all ICEs.
97 + /// </summary>
98 bool SuppressValidation { get; set; }
99
100 + /// <summary>
101 + /// Skip creation of output.
102 + /// </summary>
103 bool SuppressLayout { get; set; }
104
105 + /// <summary>
106 + /// Cancellation token.
107 + /// </summary>
108 CancellationToken CancellationToken { get; set; }
109 }
110 }
src/WixToolset.Extensibility/Data/ICommandLineContext.cs
+1 -1
@@ -8,7 +8,7 @@ namespace WixToolset.Extensibility.Data
8 #pragma warning disable 1591 // TODO: add documentation
9 public interface ICommandLineContext
10 {
11 - IWixToolsetServiceProvider ServiceProvider { get; }
11 + IServiceProvider ServiceProvider { get; }
12
13 IExtensionManager ExtensionManager { get; set; }
14
src/WixToolset.Extensibility/Data/ICompileContext.cs
+2 -2
@@ -2,11 +2,11 @@
2
3 namespace WixToolset.Extensibility.Data
4 {
5 + using System;
6 using System.Collections.Generic;
7 using System.Threading;
8 using System.Xml.Linq;
9 using WixToolset.Data;
9 - using WixToolset.Extensibility.Services;
10
11 /// <summary>
12 /// Context provided to the compiler.
@@ -16,7 +16,7 @@ namespace WixToolset.Extensibility.Data
16 /// <summary>
17 /// Service provider made available to the compiler and its extensions.
18 /// </summary>
19 - IWixToolsetServiceProvider ServiceProvider { get; }
19 + IServiceProvider ServiceProvider { get; }
20
21 /// <summary>
22 /// Unique identifier for the compilation.
src/WixToolset.Extensibility/Data/IDecompileContext.cs
+1 -1
@@ -10,7 +10,7 @@ namespace WixToolset.Extensibility.Data
10 #pragma warning disable 1591 // TODO: add documentation
11 public interface IDecompileContext
12 {
13 - IWixToolsetServiceProvider ServiceProvider { get; }
13 + IServiceProvider ServiceProvider { get; }
14
15 string DecompilePath { get; set; }
16
src/WixToolset.Extensibility/Data/IFileSystemContext.cs
+1 -1
@@ -9,7 +9,7 @@ namespace WixToolset.Extensibility.Data
9 #pragma warning disable 1591 // TODO: add documentation
10 public interface IFileSystemContext
11 {
12 - IWixToolsetServiceProvider ServiceProvider { get; }
12 + IServiceProvider ServiceProvider { get; }
13
14 string CabCachePath { get; set; }
15
src/WixToolset.Extensibility/Data/IInscribeContext.cs
+1 -1
@@ -8,7 +8,7 @@ namespace WixToolset.Extensibility.Data
8 #pragma warning disable 1591 // TODO: add documentation
9 public interface IInscribeContext
10 {
11 - IWixToolsetServiceProvider ServiceProvider { get; }
11 + IServiceProvider ServiceProvider { get; }
12
13 string InputFilePath { get; set; }
14
src/WixToolset.Extensibility/Data/ILayoutContext.cs
+2 -2
@@ -2,14 +2,14 @@
2
3 namespace WixToolset.Extensibility.Data
4 {
5 + using System;
6 using System.Collections.Generic;
7 using System.Threading;
7 - using WixToolset.Extensibility.Services;
8
9 #pragma warning disable 1591 // TODO: add documentation
10 public interface ILayoutContext
11 {
12 - IWixToolsetServiceProvider ServiceProvider { get; }
12 + IServiceProvider ServiceProvider { get; }
13
14 IEnumerable<ILayoutExtension> Extensions { get; set; }
15
src/WixToolset.Extensibility/Data/ILibraryContext.cs
+2 -2
@@ -2,15 +2,15 @@
2
3 namespace WixToolset.Extensibility.Data
4 {
5 + using System;
6 using System.Collections.Generic;
7 using System.Threading;
8 using WixToolset.Data;
8 - using WixToolset.Extensibility.Services;
9
10 #pragma warning disable 1591 // TODO: add documentation
11 public interface ILibraryContext
12 {
13 - IWixToolsetServiceProvider ServiceProvider { get; }
13 + IServiceProvider ServiceProvider { get; }
14
15 bool BindFiles { get; set; }
16
src/WixToolset.Extensibility/Data/ILinkContext.cs
+2 -2
@@ -2,15 +2,15 @@
2
3 namespace WixToolset.Extensibility.Data
4 {
5 + using System;
6 using System.Collections.Generic;
7 using System.Threading;
8 using WixToolset.Data;
8 - using WixToolset.Extensibility.Services;
9
10 #pragma warning disable 1591 // TODO: add documentation
11 public interface ILinkContext
12 {
13 - IWixToolsetServiceProvider ServiceProvider { get; }
13 + IServiceProvider ServiceProvider { get; }
14
15 IEnumerable<ILinkerExtension> Extensions { get; set; }
16
src/WixToolset.Extensibility/Data/IPreprocessContext.cs
+2 -2
@@ -2,15 +2,15 @@
2
3 namespace WixToolset.Extensibility.Data
4 {
5 + using System;
6 using System.Collections.Generic;
7 using System.Threading;
8 using WixToolset.Data;
8 - using WixToolset.Extensibility.Services;
9
10 #pragma warning disable 1591 // TODO: add documentation
11 public interface IPreprocessContext
12 {
13 - IWixToolsetServiceProvider ServiceProvider { get; }
13 + IServiceProvider ServiceProvider { get; }
14
15 IEnumerable<IPreprocessorExtension> Extensions { get; set; }
16
src/WixToolset.Extensibility/Data/IResolveContext.cs
+2 -2
@@ -2,15 +2,15 @@
2
3 namespace WixToolset.Extensibility.Data
4 {
5 + using System;
6 using System.Collections.Generic;
7 using System.Threading;
8 using WixToolset.Data;
8 - using WixToolset.Extensibility.Services;
9
10 #pragma warning disable 1591 // TODO: add documentation
11 public interface IResolveContext
12 {
13 - IWixToolsetServiceProvider ServiceProvider { get; }
13 + IServiceProvider ServiceProvider { get; }
14
15 IEnumerable<IBindPath> BindPaths { get; set; }
16
src/WixToolset.Extensibility/Data/IUnbindContext.cs
+1 -2
@@ -3,12 +3,11 @@
3 namespace WixToolset.Extensibility.Data
4 {
5 using System;
6 - using WixToolset.Extensibility.Services;
6
7 #pragma warning disable 1591 // TODO: add documentation
8 public interface IUnbindContext
9 {
11 - IWixToolsetServiceProvider ServiceProvider { get; }
10 + IServiceProvider ServiceProvider { get; }
11
12 string ExportBasePath { get; set; }
13
src/WixToolset.Extensibility/Services/IWixtoolsetCoreServiceProvider.cs
+1 -1
@@ -8,7 +8,7 @@ namespace WixToolset.Extensibility.Services
8 /// <summary>
9 /// The core of the service provider used to add services to the service provider.
10 /// </summary>
11 - public interface IWixToolsetCoreServiceProvider : IWixToolsetServiceProvider
11 + public interface IWixToolsetCoreServiceProvider : IServiceProvider
12 {
13 /// <summary>
14 /// Adds a service to the service locator.
src/WixToolset.Extensibility/Services/ServiceProviderExtensions.cs renamed
+19 -5
@@ -5,30 +5,44 @@ namespace WixToolset.Extensibility.Services
5 using System;
6
7 /// <summary>
8 - /// Service provider.
8 + /// Service provider extensions.
9 /// </summary>
10 - public interface IWixToolsetServiceProvider : IServiceProvider
10 + public static class ServiceProviderExtensions
11 {
12 /// <summary>
13 /// Gets a service from the service provider.
14 /// </summary>
15 /// <typeparam name="T">Type of service to get.</typeparam>
16 - T GetService<T>() where T : class;
16 + /// <param name="provider">Service provider.</param>
17 + public static T GetService<T>(this IServiceProvider provider) where T : class
18 + {
19 + return provider.GetService(typeof(T)) as T;
20 + }
21
22 /// <summary>
23 /// Gets a service from the service provider.
24 /// </summary>
25 + /// <param name="provider">Service provider.</param>
26 /// <param name="serviceType">Type of service to get.</param>
27 /// <param name="service">Retrieved service.</param>
28 /// <returns>True if the service was found, otherwise false</returns>
24 - bool TryGetService(Type serviceType, out object service);
29 + public static bool TryGetService(this IServiceProvider provider, Type serviceType, out object service)
30 + {
31 + service = provider.GetService(serviceType);
32 + return service != null;
33 + }
34
35 /// <summary>
36 /// Gets a service from the service provider.
37 /// </summary>
38 /// <typeparam name="T">Type of service to get.</typeparam>
39 + /// <param name="provider">Service provider.</param>
40 /// <param name="service">Retrieved service.</param>
41 /// <returns>True if the service was found, otherwise false</returns>
32 - bool TryGetService<T>(out T service) where T : class;
42 + public static bool TryGetService<T>(this IServiceProvider provider, out T service) where T : class
43 + {
44 + service = provider.GetService(typeof(T)) as T;
45 + return service != null;
46 + }
47 }
48 }