Remove EncoderServices and add default services directly in the service collection extension

This commit is contained in:
Kiran Challa 2015-04-30 13:21:14 -07:00
parent 89ccdf56bf
commit 602f638a8c
2 changed files with 25 additions and 32 deletions

View File

@ -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<WebEncoderOptions> 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<IHtmlEncoder>(
CreateFactory(() => HtmlEncoder.Default, filter => new HtmlEncoder(filter))));
services.TryAdd(ServiceDescriptor.Singleton<IJavaScriptStringEncoder>(
CreateFactory(() => JavaScriptStringEncoder.Default, filter => new JavaScriptStringEncoder(filter))));
services.TryAdd(ServiceDescriptor.Singleton<IUrlEncoder>(
CreateFactory(() => UrlEncoder.Default, filter => new UrlEncoder(filter))));
if (configureOptions != null)
{
services.Configure(configureOptions);
}
return services;
}
private static Func<IServiceProvider, T> CreateFactory<T>(
Func<T> defaultFactory,
Func<ICodePointFilter, T> customFilterFactory)
{
return serviceProvider =>
{
var codePointFilter = serviceProvider?.GetService<IOptions<WebEncoderOptions>>()?
.Options?
.CodePointFilter;
return (codePointFilter != null) ? customFilterFactory(codePointFilter) : defaultFactory();
};
}
}
}

View File

@ -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<ServiceDescriptor> GetDefaultServices()
{
// Register the default encoders
// We want to call the 'Default' property getters lazily since they perform static caching
yield return ServiceDescriptor.Singleton<IHtmlEncoder>(CreateFactory(() => HtmlEncoder.Default, filter => new HtmlEncoder(filter)));
yield return ServiceDescriptor.Singleton<IJavaScriptStringEncoder>(CreateFactory(() => JavaScriptStringEncoder.Default, filter => new JavaScriptStringEncoder(filter)));
yield return ServiceDescriptor.Singleton<IUrlEncoder>(CreateFactory(() => UrlEncoder.Default, filter => new UrlEncoder(filter)));
}
private static Func<IServiceProvider, T> CreateFactory<T>(Func<T> defaultFactory, Func<ICodePointFilter, T> customFilterFactory)
{
return serviceProvider =>
{
var codePointFilter = serviceProvider?.GetService<IOptions<WebEncoderOptions>>()?.Options?.CodePointFilter;
return (codePointFilter != null) ? customFilterFactory(codePointFilter) : defaultFactory();
};
}
}
}