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

View File

@ -108,7 +108,7 @@ namespace Microsoft.Framework.DependencyInjection
var controllerTypeProvider = new FixedSetControllerTypeProvider();
foreach (var type in controllerTypes)
{
services.TryAdd(ServiceDescriptor.Transient(type, type));
services.TryAddTransient(type, type);
controllerTypeProvider.ControllerTypes.Add(type.GetTypeInfo());
}
@ -161,7 +161,7 @@ namespace Microsoft.Framework.DependencyInjection
// Cors
services.TryAddEnumerable(
ServiceDescriptor.Transient<IApplicationModelProvider, CorsApplicationModelProvider>());
services.TryAdd(ServiceDescriptor.Transient<CorsAuthorizationFilter, CorsAuthorizationFilter>());
services.TryAddTransient<CorsAuthorizationFilter, CorsAuthorizationFilter>();
// Auth
services.TryAddEnumerable(
@ -173,7 +173,7 @@ namespace Microsoft.Framework.DependencyInjection
.Transient<IControllerPropertyActivator, ViewDataDictionaryControllerPropertyActivator>());
// Formatter Mappings
services.TryAdd(ServiceDescriptor.Transient<FormatFilter, FormatFilter>());
services.TryAddTransient<FormatFilter, FormatFilter>();
// JsonOutputFormatter should use the SerializerSettings on MvcOptions
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
// specific services.
services.TryAdd(ServiceDescriptor.Scoped<ICompositeViewEngine, CompositeViewEngine>());
services.TryAddScoped<ICompositeViewEngine, CompositeViewEngine>();
// 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 =>
{
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.
services.TryAdd(ServiceDescriptor.Transient<IMvcRazorHost, MvcRazorHost>());
services.TryAddTransient<IMvcRazorHost, MvcRazorHost>();
// 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
// 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
// caches. RazorCompilation service is just an adapter service, and it is transient to ensure
// 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
services.TryAdd(ServiceDescriptor.Scoped<IViewStartProvider, ViewStartProvider>());
services.TryAdd(ServiceDescriptor.Transient<IRazorViewFactory, RazorViewFactory>());
services.TryAdd(ServiceDescriptor.Singleton<IRazorPageActivator, RazorPageActivator>());
services.TryAddScoped<IViewStartProvider, ViewStartProvider>();
services.TryAddTransient<IRazorViewFactory, RazorViewFactory>();
services.TryAddSingleton<IRazorPageActivator, RazorPageActivator>();
// 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
services.TryAdd(ServiceDescriptor.Transient<IHtmlHelper, HtmlHelper>());
services.TryAdd(ServiceDescriptor.Transient(typeof(IHtmlHelper<>), typeof(HtmlHelper<>)));
services.TryAdd(ServiceDescriptor.Transient<IJsonHelper, JsonHelper>());
services.TryAddTransient<IHtmlHelper, HtmlHelper>();
services.TryAddTransient(typeof(IHtmlHelper<>), typeof(HtmlHelper<>));
services.TryAddTransient<IJsonHelper, JsonHelper>();
// 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.
services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());
services.TryAddSingleton<IMemoryCache, MemoryCache>();
// DefaultHtmlGenerator is pretty much stateless but depends on Scoped services such as IUrlHelper and
// IActionBindingContextProvider. Therefore it too is scoped.
services.TryAdd(ServiceDescriptor.Transient<IHtmlGenerator, DefaultHtmlGenerator>());
services.TryAddTransient<IHtmlGenerator, DefaultHtmlGenerator>();
// These do caching so they should stay singleton
services.TryAdd(ServiceDescriptor.Singleton<IViewComponentSelector, DefaultViewComponentSelector>());
services.TryAdd(ServiceDescriptor.Singleton<IViewComponentActivator, DefaultViewComponentActivator>());
services.TryAdd(ServiceDescriptor.Singleton<
IViewComponentDescriptorCollectionProvider,
DefaultViewComponentDescriptorCollectionProvider>());
services.TryAddSingleton<IViewComponentSelector, DefaultViewComponentSelector>();
services.TryAddSingleton<IViewComponentActivator, DefaultViewComponentActivator>();
services.TryAddSingleton<
IViewComponentDescriptorCollectionProvider,
DefaultViewComponentDescriptorCollectionProvider>();
services.TryAdd(ServiceDescriptor
.Transient<IViewComponentDescriptorProvider, DefaultViewComponentDescriptorProvider>());
services.TryAdd(ServiceDescriptor
.Transient<IViewComponentInvokerFactory, DefaultViewComponentInvokerFactory>());
services.TryAdd(ServiceDescriptor.Transient<IViewComponentHelper, DefaultViewComponentHelper>());
services.TryAddTransient<IViewComponentDescriptorProvider, DefaultViewComponentDescriptorProvider>();
services.TryAddTransient<IViewComponentInvokerFactory, DefaultViewComponentInvokerFactory>();
services.TryAddTransient<IViewComponentHelper, DefaultViewComponentHelper>();
// Security and Authorization
services.TryAdd(ServiceDescriptor.Singleton<IClaimUidExtractor, DefaultClaimUidExtractor>());
services.TryAdd(ServiceDescriptor.Singleton<AntiForgery, AntiForgery>());
services.TryAdd(ServiceDescriptor
.Singleton<IAntiForgeryAdditionalDataProvider, DefaultAntiForgeryAdditionalDataProvider>());
services.TryAddSingleton<IClaimUidExtractor, DefaultClaimUidExtractor>();
services.TryAddSingleton<AntiForgery, AntiForgery>();
services.TryAddSingleton<IAntiForgeryAdditionalDataProvider, DefaultAntiForgeryAdditionalDataProvider>();
// Api Description
services.TryAdd(ServiceDescriptor
.Singleton<IApiDescriptionGroupCollectionProvider, ApiDescriptionGroupCollectionProvider>());
services.TryAddSingleton<IApiDescriptionGroupCollectionProvider, ApiDescriptionGroupCollectionProvider>();
services.TryAddEnumerable(
ServiceDescriptor.Transient<IApiDescriptionProvider, DefaultApiDescriptionProvider>());
}