using System; using Microsoft.Extensions.DependencyInjection; using JavaScriptEngineSwitcher.Core; namespace JavaScriptEngineSwitcher.Extensions.MsDependencyInjection { /// /// Extension methods for adding the JS engine switcher in an /// public static class JsEngineSwitcherServiceCollectionExtensions { /// /// Adds a default instance of the JS engine switcher to the /// /// The services available in the application /// Instance of the public static JsEngineFactoryCollection AddJsEngineSwitcher(this IServiceCollection services) { if (services is null) { throw new ArgumentNullException(nameof(services)); } var options = new JsEngineSwitcherOptions(); IJsEngineSwitcher engineSwitcher = CreateJsEngineSwitcher(options); ApplyOptionsToJsEngineSwitcher(engineSwitcher, options); RegisterJsEngineSwitcher(services, engineSwitcher, options); return engineSwitcher.EngineFactories; } /// /// Adds a specified instance of the JS engine switcher to the /// /// The services available in the application /// Instance of the JS engine switcher /// Instance of the public static JsEngineFactoryCollection AddJsEngineSwitcher(this IServiceCollection services, IJsEngineSwitcher engineSwitcher) { if (services is null) { throw new ArgumentNullException(nameof(services)); } if (engineSwitcher is null) { throw new ArgumentNullException(nameof(engineSwitcher)); } var options = new JsEngineSwitcherOptions(); IJsEngineSwitcher currentEngineSwitcher = GetJsEngineSwitcher(engineSwitcher, options); ApplyOptionsToJsEngineSwitcher(currentEngineSwitcher, options); RegisterJsEngineSwitcher(services, currentEngineSwitcher, options); return currentEngineSwitcher.EngineFactories; } /// /// Adds a default instance of the JS engine switcher to the /// /// The services available in the application /// The which need to be configured /// Instance of the public static JsEngineFactoryCollection AddJsEngineSwitcher(this IServiceCollection services, Action configure) { if (services is null) { throw new ArgumentNullException(nameof(services)); } if (configure is null) { throw new ArgumentNullException(nameof(configure)); } var options = new JsEngineSwitcherOptions(); configure(options); IJsEngineSwitcher engineSwitcher = CreateJsEngineSwitcher(options); ApplyOptionsToJsEngineSwitcher(engineSwitcher, options); RegisterJsEngineSwitcher(services, engineSwitcher, options); return engineSwitcher.EngineFactories; } /// /// Adds a specified instance of the JS engine switcher to /// /// The services available in the application /// Instance of the JS engine switcher /// The which need to be configured /// Instance of the public static JsEngineFactoryCollection AddJsEngineSwitcher(this IServiceCollection services, IJsEngineSwitcher engineSwitcher, Action configure) { if (services is null) { throw new ArgumentNullException(nameof(services)); } if (engineSwitcher is null) { throw new ArgumentNullException(nameof(engineSwitcher)); } if (configure is null) { throw new ArgumentNullException(nameof(configure)); } var options = new JsEngineSwitcherOptions(); configure(options); IJsEngineSwitcher currentEngineSwitcher = GetJsEngineSwitcher(engineSwitcher, options); ApplyOptionsToJsEngineSwitcher(currentEngineSwitcher, options); RegisterJsEngineSwitcher(services, currentEngineSwitcher, options); return currentEngineSwitcher.EngineFactories; } #region Helper methods /// /// Creates an instance of the JS engine switcher /// /// Options of the JS engine switcher /// Instance of the JS engine switcher private static IJsEngineSwitcher CreateJsEngineSwitcher(JsEngineSwitcherOptions options) { IJsEngineSwitcher engineSwitcher = options.AllowCurrentProperty ? JsEngineSwitcher.Current : new JsEngineSwitcher() ; return engineSwitcher; } /// /// Gets a instance of the JS engine switcher /// /// Instance of the JS engine switcher /// Options of the JS engine switcher /// Current instance of the JS engine switcher private static IJsEngineSwitcher GetJsEngineSwitcher(IJsEngineSwitcher engineSwitcher, JsEngineSwitcherOptions options) { IJsEngineSwitcher currentEngineSwitcher = options.AllowCurrentProperty ? JsEngineSwitcher.Current : engineSwitcher ; return currentEngineSwitcher; } /// /// Applies a options to the JS engine switcher /// /// Instance of the JS engine switcher /// Options of the JS engine switcher private static void ApplyOptionsToJsEngineSwitcher(IJsEngineSwitcher engineSwitcher, JsEngineSwitcherOptions options) { JsEngineSwitcher.AllowCurrentProperty = options.AllowCurrentProperty; engineSwitcher.DefaultEngineName = options.DefaultEngineName; } /// /// Registers a instance of the JS engine switcher /// /// The services available in the application /// Instance of the JS engine switcher /// Options of the JS engine switcher private static void RegisterJsEngineSwitcher(IServiceCollection services, IJsEngineSwitcher engineSwitcher, JsEngineSwitcherOptions options) { // Register the current instance of JS engine switcher as a service services.AddSingleton(engineSwitcher); // Set the current instance of JS engine switcher if (options.AllowCurrentProperty) { JsEngineSwitcher.Current = engineSwitcher; } } #endregion } }