Make use of concise 'TryAdd***' overloads where possible

This commit is contained in:
Ryan Nowak 2015-06-24 18:51:56 -07:00
parent f055618c8c
commit 08068a85be
2 changed files with 70 additions and 61 deletions

View File

@ -44,54 +44,63 @@ namespace Microsoft.Framework.DependencyInjection
// To enable unit testing // To enable unit testing
internal static void AddMvcCoreServices(IServiceCollection services) internal static void AddMvcCoreServices(IServiceCollection services)
{ {
//
// Options // Options
// //
services.TryAddEnumerable( services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, CoreMvcOptionsSetup>()); ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, CoreMvcOptionsSetup>());
//
// Action Discovery // Action Discovery
// //
// These are consumed only when creating action descriptors, then they can be de-allocated // These are consumed only when creating action descriptors, then they can be de-allocated
services.TryAdd(ServiceDescriptor.Transient<IAssemblyProvider, DefaultAssemblyProvider>()); services.TryAddTransient<IAssemblyProvider, DefaultAssemblyProvider>();
services.TryAdd(ServiceDescriptor.Transient<IControllerTypeProvider, DefaultControllerTypeProvider>()); ; services.TryAddTransient<IControllerTypeProvider, DefaultControllerTypeProvider>();
services.TryAddEnumerable( services.TryAddEnumerable(
ServiceDescriptor.Transient<IApplicationModelProvider, DefaultApplicationModelProvider>()); ServiceDescriptor.Transient<IApplicationModelProvider, DefaultApplicationModelProvider>());
services.TryAddEnumerable( services.TryAddEnumerable(
ServiceDescriptor.Transient<IActionDescriptorProvider, ControllerActionDescriptorProvider>()); ServiceDescriptor.Transient<IActionDescriptorProvider, ControllerActionDescriptorProvider>());
services.TryAdd(ServiceDescriptor services.TryAddSingleton<IActionDescriptorsCollectionProvider, DefaultActionDescriptorsCollectionProvider>();
.Singleton<IActionDescriptorsCollectionProvider, DefaultActionDescriptorsCollectionProvider>());
//
// Action Selection // Action Selection
// //
services.TryAdd(ServiceDescriptor.Singleton<IActionSelector, DefaultActionSelector>()); services.TryAddSingleton<IActionSelector, DefaultActionSelector>();
// Performs caching // Performs caching
services.TryAdd(ServiceDescriptor services.TryAddSingleton<IActionSelectorDecisionTreeProvider, ActionSelectorDecisionTreeProvider>();
.Singleton<IActionSelectorDecisionTreeProvider, ActionSelectorDecisionTreeProvider>());
// This provider needs access to the per-request services, but might be used many times for a given // Will be cached by the DefaultActionSelector
// request.
services.TryAddEnumerable( services.TryAddEnumerable(
ServiceDescriptor.Transient<IActionConstraintProvider, DefaultActionConstraintProvider>()); ServiceDescriptor.Transient<IActionConstraintProvider, DefaultActionConstraintProvider>());
// Action Invoker //
// Controller Factory
// //
// This has a cache, so it needs to be a singleton // This has a cache, so it needs to be a singleton
services.TryAdd(ServiceDescriptor.Singleton<IControllerFactory, DefaultControllerFactory>()); services.TryAddSingleton<IControllerFactory, DefaultControllerFactory>();
services.TryAdd(ServiceDescriptor.Transient<IControllerActivator, DefaultControllerActivator>());
// Will be cached by the DefaultControllerFactory
services.TryAddTransient<IControllerActivator, DefaultControllerActivator>();
services.TryAddEnumerable(
ServiceDescriptor.Transient<IControllerPropertyActivator, DefaultControllerPropertyActivator>());
//
// Action Invoker
//
// This accesses per-request services // This accesses per-request services
services.TryAdd(ServiceDescriptor.Transient<IActionInvokerFactory, ActionInvokerFactory>()); services.TryAddTransient<IActionInvokerFactory, ActionInvokerFactory>();
services.TryAdd(ServiceDescriptor services.TryAddTransient<IControllerActionArgumentBinder, DefaultControllerActionArgumentBinder>();
.Transient<IControllerActionArgumentBinder, DefaultControllerActionArgumentBinder>());
services.TryAddEnumerable( services.TryAddEnumerable(
ServiceDescriptor.Transient<IActionInvokerProvider, ControllerActionInvokerProvider>()); ServiceDescriptor.Transient<IActionInvokerProvider, ControllerActionInvokerProvider>());
services.TryAddEnumerable( services.TryAddEnumerable(
ServiceDescriptor.Transient<IFilterProvider, DefaultFilterProvider>()); ServiceDescriptor.Transient<IFilterProvider, DefaultFilterProvider>());
services.TryAddEnumerable(
ServiceDescriptor.Transient<IControllerPropertyActivator, DefaultControllerPropertyActivator>());
//
// ModelBinding, Validation and Formatting // ModelBinding, Validation and Formatting
// //
// The DefaultModelMetadataProvider does significant caching and should be a singleton. // The DefaultModelMetadataProvider does significant caching and should be a singleton.
services.TryAdd(ServiceDescriptor.Singleton<IModelMetadataProvider, DefaultModelMetadataProvider>()); services.TryAddSingleton<IModelMetadataProvider, DefaultModelMetadataProvider>();
services.TryAdd(ServiceDescriptor.Transient<ICompositeMetadataDetailsProvider>(serviceProvider => services.TryAdd(ServiceDescriptor.Transient<ICompositeMetadataDetailsProvider>(serviceProvider =>
{ {
var options = serviceProvider.GetRequiredService<IOptions<MvcOptions>>().Options; var options = serviceProvider.GetRequiredService<IOptions<MvcOptions>>().Options;
@ -104,18 +113,22 @@ namespace Microsoft.Framework.DependencyInjection
return new DefaultObjectValidator(options.ValidationExcludeFilters, modelMetadataProvider); return new DefaultObjectValidator(options.ValidationExcludeFilters, modelMetadataProvider);
})); }));
//
// Temp Data // Temp Data
// //
services.TryAdd(ServiceDescriptor.Scoped<ITempDataDictionary, TempDataDictionary>()); // Holds per-request data so it should be scoped
// This does caching so it should stay singleton services.TryAddScoped<ITempDataDictionary, TempDataDictionary>();
services.TryAdd(ServiceDescriptor.Singleton<ITempDataProvider, SessionStateTempDataProvider>());
// This does caching so it should stay singleton
services.TryAddSingleton<ITempDataProvider, SessionStateTempDataProvider>();
//
// Random Infrastructure // Random Infrastructure
// //
services.TryAdd(ServiceDescriptor.Transient<MvcMarkerService, MvcMarkerService>()); services.TryAddTransient<MvcMarkerService, MvcMarkerService>();
services.TryAdd((ServiceDescriptor.Singleton<ITypeActivatorCache, DefaultTypeActivatorCache>())); services.TryAddSingleton<ITypeActivatorCache, DefaultTypeActivatorCache>();
services.TryAdd(ServiceDescriptor.Scoped(typeof(IScopedInstance<>), typeof(ScopedInstance<>))); services.TryAddScoped(typeof(IScopedInstance<>), typeof(ScopedInstance<>));
services.TryAdd(ServiceDescriptor.Scoped<IUrlHelper, UrlHelper>()); services.TryAddScoped<IUrlHelper, UrlHelper>();
} }
private static void ConfigureDefaultServices(IServiceCollection services) private static void ConfigureDefaultServices(IServiceCollection services)

View File

@ -108,7 +108,7 @@ namespace Microsoft.Framework.DependencyInjection
var controllerTypeProvider = new FixedSetControllerTypeProvider(); var controllerTypeProvider = new FixedSetControllerTypeProvider();
foreach (var type in controllerTypes) foreach (var type in controllerTypes)
{ {
services.TryAdd(ServiceDescriptor.Transient(type, type)); services.TryAddTransient(type, type);
controllerTypeProvider.ControllerTypes.Add(type.GetTypeInfo()); controllerTypeProvider.ControllerTypes.Add(type.GetTypeInfo());
} }
@ -161,7 +161,7 @@ namespace Microsoft.Framework.DependencyInjection
// Cors // Cors
services.TryAddEnumerable( services.TryAddEnumerable(
ServiceDescriptor.Transient<IApplicationModelProvider, CorsApplicationModelProvider>()); ServiceDescriptor.Transient<IApplicationModelProvider, CorsApplicationModelProvider>());
services.TryAdd(ServiceDescriptor.Transient<CorsAuthorizationFilter, CorsAuthorizationFilter>()); services.TryAddTransient<CorsAuthorizationFilter, CorsAuthorizationFilter>();
// Auth // Auth
services.TryAddEnumerable( services.TryAddEnumerable(
@ -173,7 +173,7 @@ namespace Microsoft.Framework.DependencyInjection
.Transient<IControllerPropertyActivator, ViewDataDictionaryControllerPropertyActivator>()); .Transient<IControllerPropertyActivator, ViewDataDictionaryControllerPropertyActivator>());
// Formatter Mappings // Formatter Mappings
services.TryAdd(ServiceDescriptor.Transient<FormatFilter, FormatFilter>()); services.TryAddTransient<FormatFilter, FormatFilter>();
// JsonOutputFormatter should use the SerializerSettings on MvcOptions // JsonOutputFormatter should use the SerializerSettings on MvcOptions
services.TryAdd(ServiceDescriptor.Singleton<JsonOutputFormatter>(serviceProvider => services.TryAdd(ServiceDescriptor.Singleton<JsonOutputFormatter>(serviceProvider =>
@ -186,10 +186,10 @@ namespace Microsoft.Framework.DependencyInjection
// The provider is inexpensive to initialize and provides ViewEngines that may require request // The provider is inexpensive to initialize and provides ViewEngines that may require request
// specific services. // specific services.
services.TryAdd(ServiceDescriptor.Scoped<ICompositeViewEngine, CompositeViewEngine>()); services.TryAddScoped<ICompositeViewEngine, CompositeViewEngine>();
// Caches view locations that are valid for the lifetime of the application. // Caches view locations that are valid for the lifetime of the application.
services.TryAdd(ServiceDescriptor.Singleton<IViewLocationCache, DefaultViewLocationCache>()); services.TryAddSingleton<IViewLocationCache, DefaultViewLocationCache>();
services.TryAdd(ServiceDescriptor.Singleton<IChunkTreeCache>(serviceProvider => services.TryAdd(ServiceDescriptor.Singleton<IChunkTreeCache>(serviceProvider =>
{ {
var cachedFileProvider = serviceProvider.GetRequiredService<IOptions<RazorViewEngineOptions>>(); var cachedFileProvider = serviceProvider.GetRequiredService<IOptions<RazorViewEngineOptions>>();
@ -197,65 +197,61 @@ namespace Microsoft.Framework.DependencyInjection
})); }));
// The host is designed to be discarded after consumption and is very inexpensive to initialize. // The host is designed to be discarded after consumption and is very inexpensive to initialize.
services.TryAdd(ServiceDescriptor.Transient<IMvcRazorHost, MvcRazorHost>()); services.TryAddTransient<IMvcRazorHost, MvcRazorHost>();
// Caches compilation artifacts across the lifetime of the application. // Caches compilation artifacts across the lifetime of the application.
services.TryAdd(ServiceDescriptor.Singleton<ICompilerCache, CompilerCache>()); services.TryAddSingleton<ICompilerCache, CompilerCache>();
// This caches compilation related details that are valid across the lifetime of the application // This caches compilation related details that are valid across the lifetime of the application
// and is required to be a singleton. // and is required to be a singleton.
services.TryAdd(ServiceDescriptor.Singleton<ICompilationService, RoslynCompilationService>()); services.TryAddSingleton<ICompilationService, RoslynCompilationService>();
// Both the compiler cache and roslyn compilation service hold on the compilation related // Both the compiler cache and roslyn compilation service hold on the compilation related
// caches. RazorCompilation service is just an adapter service, and it is transient to ensure // caches. RazorCompilation service is just an adapter service, and it is transient to ensure
// the IMvcRazorHost dependency does not maintain state. // the IMvcRazorHost dependency does not maintain state.
services.TryAdd(ServiceDescriptor.Transient<IRazorCompilationService, RazorCompilationService>()); services.TryAddTransient<IRazorCompilationService, RazorCompilationService>();
// The ViewStartProvider needs to be able to consume scoped instances of IRazorPageFactory // The ViewStartProvider needs to be able to consume scoped instances of IRazorPageFactory
services.TryAdd(ServiceDescriptor.Scoped<IViewStartProvider, ViewStartProvider>()); services.TryAddScoped<IViewStartProvider, ViewStartProvider>();
services.TryAdd(ServiceDescriptor.Transient<IRazorViewFactory, RazorViewFactory>()); services.TryAddTransient<IRazorViewFactory, RazorViewFactory>();
services.TryAdd(ServiceDescriptor.Singleton<IRazorPageActivator, RazorPageActivator>()); services.TryAddSingleton<IRazorPageActivator, RazorPageActivator>();
// Virtual path view factory needs to stay scoped so views can get get scoped services. // Virtual path view factory needs to stay scoped so views can get get scoped services.
services.TryAdd(ServiceDescriptor.Scoped<IRazorPageFactory, VirtualPathRazorPageFactory>()); services.TryAddScoped<IRazorPageFactory, VirtualPathRazorPageFactory>();
// View and rendering helpers // View and rendering helpers
services.TryAdd(ServiceDescriptor.Transient<IHtmlHelper, HtmlHelper>()); services.TryAddTransient<IHtmlHelper, HtmlHelper>();
services.TryAdd(ServiceDescriptor.Transient(typeof(IHtmlHelper<>), typeof(HtmlHelper<>))); services.TryAddTransient(typeof(IHtmlHelper<>), typeof(HtmlHelper<>));
services.TryAdd(ServiceDescriptor.Transient<IJsonHelper, JsonHelper>()); services.TryAddTransient<IJsonHelper, JsonHelper>();
// Only want one ITagHelperActivator so it can cache Type activation information. Types won't conflict. // Only want one ITagHelperActivator so it can cache Type activation information. Types won't conflict.
services.TryAdd(ServiceDescriptor.Singleton<ITagHelperActivator, DefaultTagHelperActivator>()); services.TryAddSingleton<ITagHelperActivator, DefaultTagHelperActivator>();
// Consumed by the Cache tag helper to cache results across the lifetime of the application. // Consumed by the Cache tag helper to cache results across the lifetime of the application.
services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>()); services.TryAddSingleton<IMemoryCache, MemoryCache>();
// DefaultHtmlGenerator is pretty much stateless but depends on Scoped services such as IUrlHelper and // DefaultHtmlGenerator is pretty much stateless but depends on Scoped services such as IUrlHelper and
// IActionBindingContextProvider. Therefore it too is scoped. // IActionBindingContextProvider. Therefore it too is scoped.
services.TryAdd(ServiceDescriptor.Transient<IHtmlGenerator, DefaultHtmlGenerator>()); services.TryAddTransient<IHtmlGenerator, DefaultHtmlGenerator>();
// These do caching so they should stay singleton // These do caching so they should stay singleton
services.TryAdd(ServiceDescriptor.Singleton<IViewComponentSelector, DefaultViewComponentSelector>()); services.TryAddSingleton<IViewComponentSelector, DefaultViewComponentSelector>();
services.TryAdd(ServiceDescriptor.Singleton<IViewComponentActivator, DefaultViewComponentActivator>()); services.TryAddSingleton<IViewComponentActivator, DefaultViewComponentActivator>();
services.TryAdd(ServiceDescriptor.Singleton< services.TryAddSingleton<
IViewComponentDescriptorCollectionProvider, IViewComponentDescriptorCollectionProvider,
DefaultViewComponentDescriptorCollectionProvider>()); DefaultViewComponentDescriptorCollectionProvider>();
services.TryAdd(ServiceDescriptor services.TryAddTransient<IViewComponentDescriptorProvider, DefaultViewComponentDescriptorProvider>();
.Transient<IViewComponentDescriptorProvider, DefaultViewComponentDescriptorProvider>()); services.TryAddTransient<IViewComponentInvokerFactory, DefaultViewComponentInvokerFactory>();
services.TryAdd(ServiceDescriptor services.TryAddTransient<IViewComponentHelper, DefaultViewComponentHelper>();
.Transient<IViewComponentInvokerFactory, DefaultViewComponentInvokerFactory>());
services.TryAdd(ServiceDescriptor.Transient<IViewComponentHelper, DefaultViewComponentHelper>());
// Security and Authorization // Security and Authorization
services.TryAdd(ServiceDescriptor.Singleton<IClaimUidExtractor, DefaultClaimUidExtractor>()); services.TryAddSingleton<IClaimUidExtractor, DefaultClaimUidExtractor>();
services.TryAdd(ServiceDescriptor.Singleton<AntiForgery, AntiForgery>()); services.TryAddSingleton<AntiForgery, AntiForgery>();
services.TryAdd(ServiceDescriptor services.TryAddSingleton<IAntiForgeryAdditionalDataProvider, DefaultAntiForgeryAdditionalDataProvider>();
.Singleton<IAntiForgeryAdditionalDataProvider, DefaultAntiForgeryAdditionalDataProvider>());
// Api Description // Api Description
services.TryAdd(ServiceDescriptor services.TryAddSingleton<IApiDescriptionGroupCollectionProvider, ApiDescriptionGroupCollectionProvider>();
.Singleton<IApiDescriptionGroupCollectionProvider, ApiDescriptionGroupCollectionProvider>());
services.TryAddEnumerable( services.TryAddEnumerable(
ServiceDescriptor.Transient<IApiDescriptionProvider, DefaultApiDescriptionProvider>()); ServiceDescriptor.Transient<IApiDescriptionProvider, DefaultApiDescriptionProvider>());
} }