From 602f638a8c08c8473eeb299380036d4cd74615d3 Mon Sep 17 00:00:00 2001 From: Kiran Challa Date: Thu, 30 Apr 2015 13:21:14 -0700 Subject: [PATCH] Remove EncoderServices and add default services directly in the service collection extension --- .../EncoderServiceCollectionExtensions.cs | 26 +++++++++++++++- .../EncoderServices.cs | 31 ------------------- 2 files changed, 25 insertions(+), 32 deletions(-) delete mode 100644 src/Microsoft.Framework.WebEncoders/EncoderServices.cs diff --git a/src/Microsoft.Framework.WebEncoders/EncoderServiceCollectionExtensions.cs b/src/Microsoft.Framework.WebEncoders/EncoderServiceCollectionExtensions.cs index aae3ffbcf2..3d0851e9d4 100644 --- a/src/Microsoft.Framework.WebEncoders/EncoderServiceCollectionExtensions.cs +++ b/src/Microsoft.Framework.WebEncoders/EncoderServiceCollectionExtensions.cs @@ -3,6 +3,7 @@ using System; using Microsoft.Framework.Internal; +using Microsoft.Framework.OptionsModel; using Microsoft.Framework.WebEncoders; namespace Microsoft.Framework.DependencyInjection @@ -17,12 +18,35 @@ namespace Microsoft.Framework.DependencyInjection public static IServiceCollection AddWebEncoders([NotNull] this IServiceCollection services, Action configureOptions) { services.AddOptions(); - services.TryAdd(EncoderServices.GetDefaultServices()); + + // Register the default encoders + // We want to call the 'Default' property getters lazily since they perform static caching + services.TryAdd(ServiceDescriptor.Singleton( + CreateFactory(() => HtmlEncoder.Default, filter => new HtmlEncoder(filter)))); + services.TryAdd(ServiceDescriptor.Singleton( + CreateFactory(() => JavaScriptStringEncoder.Default, filter => new JavaScriptStringEncoder(filter)))); + services.TryAdd(ServiceDescriptor.Singleton( + CreateFactory(() => UrlEncoder.Default, filter => new UrlEncoder(filter)))); + if (configureOptions != null) { services.Configure(configureOptions); } + return services; } + + private static Func CreateFactory( + Func defaultFactory, + Func customFilterFactory) + { + return serviceProvider => + { + var codePointFilter = serviceProvider?.GetService>()? + .Options? + .CodePointFilter; + return (codePointFilter != null) ? customFilterFactory(codePointFilter) : defaultFactory(); + }; + } } } diff --git a/src/Microsoft.Framework.WebEncoders/EncoderServices.cs b/src/Microsoft.Framework.WebEncoders/EncoderServices.cs deleted file mode 100644 index d7759f5134..0000000000 --- a/src/Microsoft.Framework.WebEncoders/EncoderServices.cs +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.Framework.DependencyInjection; -using Microsoft.Framework.OptionsModel; - -namespace Microsoft.Framework.WebEncoders -{ - public static class EncoderServices - { - public static IEnumerable GetDefaultServices() - { - // Register the default encoders - // We want to call the 'Default' property getters lazily since they perform static caching - yield return ServiceDescriptor.Singleton(CreateFactory(() => HtmlEncoder.Default, filter => new HtmlEncoder(filter))); - yield return ServiceDescriptor.Singleton(CreateFactory(() => JavaScriptStringEncoder.Default, filter => new JavaScriptStringEncoder(filter))); - yield return ServiceDescriptor.Singleton(CreateFactory(() => UrlEncoder.Default, filter => new UrlEncoder(filter))); - } - - private static Func CreateFactory(Func defaultFactory, Func customFilterFactory) - { - return serviceProvider => - { - var codePointFilter = serviceProvider?.GetService>()?.Options?.CodePointFilter; - return (codePointFilter != null) ? customFilterFactory(codePointFilter) : defaultFactory(); - }; - } - } -}