From 9aae370eee18b4c87300f333fa52f3cd0d7f34d1 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sun, 14 Mar 2021 11:17:20 -0700 Subject: 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. --- src/WixToolset.Extensibility/Data/IBindContext.cs | 67 ++++++++++++++++++++-- .../Data/ICommandLineContext.cs | 2 +- .../Data/ICompileContext.cs | 4 +- .../Data/IDecompileContext.cs | 2 +- .../Data/IFileSystemContext.cs | 2 +- .../Data/IInscribeContext.cs | 2 +- .../Data/ILayoutContext.cs | 4 +- .../Data/ILibraryContext.cs | 4 +- src/WixToolset.Extensibility/Data/ILinkContext.cs | 4 +- .../Data/IPreprocessContext.cs | 4 +- .../Data/IResolveContext.cs | 4 +- .../Data/IUnbindContext.cs | 3 +- .../Services/IWixToolsetServiceProvider.cs | 34 ----------- .../Services/IWixtoolsetCoreServiceProvider.cs | 2 +- .../Services/ServiceProviderExtensions.cs | 48 ++++++++++++++++ 15 files changed, 129 insertions(+), 57 deletions(-) delete mode 100644 src/WixToolset.Extensibility/Services/IWixToolsetServiceProvider.cs create mode 100644 src/WixToolset.Extensibility/Services/ServiceProviderExtensions.cs (limited to 'src') diff --git a/src/WixToolset.Extensibility/Data/IBindContext.cs b/src/WixToolset.Extensibility/Data/IBindContext.cs index fd5a3ee4..ee165671 100644 --- a/src/WixToolset.Extensibility/Data/IBindContext.cs +++ b/src/WixToolset.Extensibility/Data/IBindContext.cs @@ -2,50 +2,109 @@ namespace WixToolset.Extensibility.Data { + using System; using System.Collections.Generic; using System.Threading; using WixToolset.Data; - using WixToolset.Extensibility.Services; -#pragma warning disable 1591 // TODO: add documentation + /// + /// Bind context. + /// public interface IBindContext { - IWixToolsetServiceProvider ServiceProvider { get; } - + /// + /// Service provider. + /// + IServiceProvider ServiceProvider { get; } + + /// + /// Counnt of threads to use in cabbing. + /// int CabbingThreadCount { get; set; } + /// + /// Cabinet cache path. + /// string CabCachePath { get; set; } + /// + /// Codepage for result. + /// int Codepage { get; set; } + /// + /// Default compression level. + /// CompressionLevel? DefaultCompressionLevel { get; set; } + /// + /// Delayed fields that need to be resolved again. + /// IEnumerable DelayedFields { get; set; } + /// + /// Embedded files to extract. + /// IEnumerable ExpectedEmbeddedFiles { get; set; } + /// + /// Binder extensions. + /// IEnumerable Extensions { get; set; } + /// + /// File system extensions. + /// IEnumerable FileSystemExtensions { get; set; } + /// + /// Set of ICEs to execute. + /// IEnumerable Ices { get; set; } + /// + /// Intermedaite folder. + /// string IntermediateFolder { get; set; } + /// + /// Intermediate representation to bind. + /// Intermediate IntermediateRepresentation { get; set; } + /// + /// Output path to bind to. + /// string OutputPath { get; set; } + /// + /// Type of PDB to create. + /// PdbType PdbType { get; set; } + /// + /// Output path for PDB. + /// string PdbPath { get; set; } + /// + /// Set of ICEs to skip. + /// IEnumerable SuppressIces { get; set; } + /// + /// Skip all ICEs. + /// bool SuppressValidation { get; set; } + /// + /// Skip creation of output. + /// bool SuppressLayout { get; set; } + /// + /// Cancellation token. + /// CancellationToken CancellationToken { get; set; } } } diff --git a/src/WixToolset.Extensibility/Data/ICommandLineContext.cs b/src/WixToolset.Extensibility/Data/ICommandLineContext.cs index fbaa84d1..d8c9469e 100644 --- a/src/WixToolset.Extensibility/Data/ICommandLineContext.cs +++ b/src/WixToolset.Extensibility/Data/ICommandLineContext.cs @@ -8,7 +8,7 @@ namespace WixToolset.Extensibility.Data #pragma warning disable 1591 // TODO: add documentation public interface ICommandLineContext { - IWixToolsetServiceProvider ServiceProvider { get; } + IServiceProvider ServiceProvider { get; } IExtensionManager ExtensionManager { get; set; } diff --git a/src/WixToolset.Extensibility/Data/ICompileContext.cs b/src/WixToolset.Extensibility/Data/ICompileContext.cs index 5da5ca84..a86fee1a 100644 --- a/src/WixToolset.Extensibility/Data/ICompileContext.cs +++ b/src/WixToolset.Extensibility/Data/ICompileContext.cs @@ -2,11 +2,11 @@ namespace WixToolset.Extensibility.Data { + using System; using System.Collections.Generic; using System.Threading; using System.Xml.Linq; using WixToolset.Data; - using WixToolset.Extensibility.Services; /// /// Context provided to the compiler. @@ -16,7 +16,7 @@ namespace WixToolset.Extensibility.Data /// /// Service provider made available to the compiler and its extensions. /// - IWixToolsetServiceProvider ServiceProvider { get; } + IServiceProvider ServiceProvider { get; } /// /// Unique identifier for the compilation. diff --git a/src/WixToolset.Extensibility/Data/IDecompileContext.cs b/src/WixToolset.Extensibility/Data/IDecompileContext.cs index f783a31e..63ed27d5 100644 --- a/src/WixToolset.Extensibility/Data/IDecompileContext.cs +++ b/src/WixToolset.Extensibility/Data/IDecompileContext.cs @@ -10,7 +10,7 @@ namespace WixToolset.Extensibility.Data #pragma warning disable 1591 // TODO: add documentation public interface IDecompileContext { - IWixToolsetServiceProvider ServiceProvider { get; } + IServiceProvider ServiceProvider { get; } string DecompilePath { get; set; } diff --git a/src/WixToolset.Extensibility/Data/IFileSystemContext.cs b/src/WixToolset.Extensibility/Data/IFileSystemContext.cs index 321fa83c..2e58059a 100644 --- a/src/WixToolset.Extensibility/Data/IFileSystemContext.cs +++ b/src/WixToolset.Extensibility/Data/IFileSystemContext.cs @@ -9,7 +9,7 @@ namespace WixToolset.Extensibility.Data #pragma warning disable 1591 // TODO: add documentation public interface IFileSystemContext { - IWixToolsetServiceProvider ServiceProvider { get; } + IServiceProvider ServiceProvider { get; } string CabCachePath { get; set; } diff --git a/src/WixToolset.Extensibility/Data/IInscribeContext.cs b/src/WixToolset.Extensibility/Data/IInscribeContext.cs index 79d4d1f5..31c66aad 100644 --- a/src/WixToolset.Extensibility/Data/IInscribeContext.cs +++ b/src/WixToolset.Extensibility/Data/IInscribeContext.cs @@ -8,7 +8,7 @@ namespace WixToolset.Extensibility.Data #pragma warning disable 1591 // TODO: add documentation public interface IInscribeContext { - IWixToolsetServiceProvider ServiceProvider { get; } + IServiceProvider ServiceProvider { get; } string InputFilePath { get; set; } diff --git a/src/WixToolset.Extensibility/Data/ILayoutContext.cs b/src/WixToolset.Extensibility/Data/ILayoutContext.cs index 89d61d5c..6b6c280a 100644 --- a/src/WixToolset.Extensibility/Data/ILayoutContext.cs +++ b/src/WixToolset.Extensibility/Data/ILayoutContext.cs @@ -2,14 +2,14 @@ namespace WixToolset.Extensibility.Data { + using System; using System.Collections.Generic; using System.Threading; - using WixToolset.Extensibility.Services; #pragma warning disable 1591 // TODO: add documentation public interface ILayoutContext { - IWixToolsetServiceProvider ServiceProvider { get; } + IServiceProvider ServiceProvider { get; } IEnumerable Extensions { get; set; } diff --git a/src/WixToolset.Extensibility/Data/ILibraryContext.cs b/src/WixToolset.Extensibility/Data/ILibraryContext.cs index 68a9faa9..d6359ffc 100644 --- a/src/WixToolset.Extensibility/Data/ILibraryContext.cs +++ b/src/WixToolset.Extensibility/Data/ILibraryContext.cs @@ -2,15 +2,15 @@ namespace WixToolset.Extensibility.Data { + using System; using System.Collections.Generic; using System.Threading; using WixToolset.Data; - using WixToolset.Extensibility.Services; #pragma warning disable 1591 // TODO: add documentation public interface ILibraryContext { - IWixToolsetServiceProvider ServiceProvider { get; } + IServiceProvider ServiceProvider { get; } bool BindFiles { get; set; } diff --git a/src/WixToolset.Extensibility/Data/ILinkContext.cs b/src/WixToolset.Extensibility/Data/ILinkContext.cs index c6c9cf7d..7524d18c 100644 --- a/src/WixToolset.Extensibility/Data/ILinkContext.cs +++ b/src/WixToolset.Extensibility/Data/ILinkContext.cs @@ -2,15 +2,15 @@ namespace WixToolset.Extensibility.Data { + using System; using System.Collections.Generic; using System.Threading; using WixToolset.Data; - using WixToolset.Extensibility.Services; #pragma warning disable 1591 // TODO: add documentation public interface ILinkContext { - IWixToolsetServiceProvider ServiceProvider { get; } + IServiceProvider ServiceProvider { get; } IEnumerable Extensions { get; set; } diff --git a/src/WixToolset.Extensibility/Data/IPreprocessContext.cs b/src/WixToolset.Extensibility/Data/IPreprocessContext.cs index b07fb81f..c6bdfe3a 100644 --- a/src/WixToolset.Extensibility/Data/IPreprocessContext.cs +++ b/src/WixToolset.Extensibility/Data/IPreprocessContext.cs @@ -2,15 +2,15 @@ namespace WixToolset.Extensibility.Data { + using System; using System.Collections.Generic; using System.Threading; using WixToolset.Data; - using WixToolset.Extensibility.Services; #pragma warning disable 1591 // TODO: add documentation public interface IPreprocessContext { - IWixToolsetServiceProvider ServiceProvider { get; } + IServiceProvider ServiceProvider { get; } IEnumerable Extensions { get; set; } diff --git a/src/WixToolset.Extensibility/Data/IResolveContext.cs b/src/WixToolset.Extensibility/Data/IResolveContext.cs index 2c775932..79191a15 100644 --- a/src/WixToolset.Extensibility/Data/IResolveContext.cs +++ b/src/WixToolset.Extensibility/Data/IResolveContext.cs @@ -2,15 +2,15 @@ namespace WixToolset.Extensibility.Data { + using System; using System.Collections.Generic; using System.Threading; using WixToolset.Data; - using WixToolset.Extensibility.Services; #pragma warning disable 1591 // TODO: add documentation public interface IResolveContext { - IWixToolsetServiceProvider ServiceProvider { get; } + IServiceProvider ServiceProvider { get; } IEnumerable BindPaths { get; set; } diff --git a/src/WixToolset.Extensibility/Data/IUnbindContext.cs b/src/WixToolset.Extensibility/Data/IUnbindContext.cs index faf77f5d..6427422f 100644 --- a/src/WixToolset.Extensibility/Data/IUnbindContext.cs +++ b/src/WixToolset.Extensibility/Data/IUnbindContext.cs @@ -3,12 +3,11 @@ namespace WixToolset.Extensibility.Data { using System; - using WixToolset.Extensibility.Services; #pragma warning disable 1591 // TODO: add documentation public interface IUnbindContext { - IWixToolsetServiceProvider ServiceProvider { get; } + IServiceProvider ServiceProvider { get; } string ExportBasePath { get; set; } diff --git a/src/WixToolset.Extensibility/Services/IWixToolsetServiceProvider.cs b/src/WixToolset.Extensibility/Services/IWixToolsetServiceProvider.cs deleted file mode 100644 index 0315f7ed..00000000 --- a/src/WixToolset.Extensibility/Services/IWixToolsetServiceProvider.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. - -namespace WixToolset.Extensibility.Services -{ - using System; - - /// - /// Service provider. - /// - public interface IWixToolsetServiceProvider : IServiceProvider - { - /// - /// Gets a service from the service provider. - /// - /// Type of service to get. - T GetService() where T : class; - - /// - /// Gets a service from the service provider. - /// - /// Type of service to get. - /// Retrieved service. - /// True if the service was found, otherwise false - bool TryGetService(Type serviceType, out object service); - - /// - /// Gets a service from the service provider. - /// - /// Type of service to get. - /// Retrieved service. - /// True if the service was found, otherwise false - bool TryGetService(out T service) where T : class; - } -} diff --git a/src/WixToolset.Extensibility/Services/IWixtoolsetCoreServiceProvider.cs b/src/WixToolset.Extensibility/Services/IWixtoolsetCoreServiceProvider.cs index 2d0450dc..f5fb28fb 100644 --- a/src/WixToolset.Extensibility/Services/IWixtoolsetCoreServiceProvider.cs +++ b/src/WixToolset.Extensibility/Services/IWixtoolsetCoreServiceProvider.cs @@ -8,7 +8,7 @@ namespace WixToolset.Extensibility.Services /// /// The core of the service provider used to add services to the service provider. /// - public interface IWixToolsetCoreServiceProvider : IWixToolsetServiceProvider + public interface IWixToolsetCoreServiceProvider : IServiceProvider { /// /// Adds a service to the service locator. diff --git a/src/WixToolset.Extensibility/Services/ServiceProviderExtensions.cs b/src/WixToolset.Extensibility/Services/ServiceProviderExtensions.cs new file mode 100644 index 00000000..68484d09 --- /dev/null +++ b/src/WixToolset.Extensibility/Services/ServiceProviderExtensions.cs @@ -0,0 +1,48 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.Extensibility.Services +{ + using System; + + /// + /// Service provider extensions. + /// + public static class ServiceProviderExtensions + { + /// + /// Gets a service from the service provider. + /// + /// Type of service to get. + /// Service provider. + public static T GetService(this IServiceProvider provider) where T : class + { + return provider.GetService(typeof(T)) as T; + } + + /// + /// Gets a service from the service provider. + /// + /// Service provider. + /// Type of service to get. + /// Retrieved service. + /// True if the service was found, otherwise false + public static bool TryGetService(this IServiceProvider provider, Type serviceType, out object service) + { + service = provider.GetService(serviceType); + return service != null; + } + + /// + /// Gets a service from the service provider. + /// + /// Type of service to get. + /// Service provider. + /// Retrieved service. + /// True if the service was found, otherwise false + public static bool TryGetService(this IServiceProvider provider, out T service) where T : class + { + service = provider.GetService(typeof(T)) as T; + return service != null; + } + } +} -- cgit v1.2.3-55-g6feb