From 08068a85bec43fd5207a56bb41ee1c6c11ca2b83 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Wed, 24 Jun 2015 18:51:56 -0700 Subject: [PATCH] Make use of concise 'TryAdd***' overloads where possible --- .../MvcCoreServiceCollectionExtensions.cs | 65 ++++++++++-------- .../MvcServiceCollectionExtensions.cs | 66 +++++++++---------- 2 files changed, 70 insertions(+), 61 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/MvcCoreServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/MvcCoreServiceCollectionExtensions.cs index b10ad7891b..d32fac2abc 100644 --- a/src/Microsoft.AspNet.Mvc.Core/MvcCoreServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/MvcCoreServiceCollectionExtensions.cs @@ -44,54 +44,63 @@ namespace Microsoft.Framework.DependencyInjection // To enable unit testing internal static void AddMvcCoreServices(IServiceCollection services) { + // // Options // services.TryAddEnumerable( ServiceDescriptor.Transient, CoreMvcOptionsSetup>()); + // // Action Discovery // // These are consumed only when creating action descriptors, then they can be de-allocated - services.TryAdd(ServiceDescriptor.Transient()); - services.TryAdd(ServiceDescriptor.Transient()); ; + services.TryAddTransient(); + services.TryAddTransient(); services.TryAddEnumerable( ServiceDescriptor.Transient()); services.TryAddEnumerable( ServiceDescriptor.Transient()); - services.TryAdd(ServiceDescriptor - .Singleton()); - + services.TryAddSingleton(); + + // // Action Selection // - services.TryAdd(ServiceDescriptor.Singleton()); + services.TryAddSingleton(); + // Performs caching - services.TryAdd(ServiceDescriptor - .Singleton()); - // This provider needs access to the per-request services, but might be used many times for a given - // request. + services.TryAddSingleton(); + + // Will be cached by the DefaultActionSelector services.TryAddEnumerable( ServiceDescriptor.Transient()); - // Action Invoker + // + // Controller Factory // // This has a cache, so it needs to be a singleton - services.TryAdd(ServiceDescriptor.Singleton()); - services.TryAdd(ServiceDescriptor.Transient()); + services.TryAddSingleton(); + + // Will be cached by the DefaultControllerFactory + services.TryAddTransient(); + services.TryAddEnumerable( + ServiceDescriptor.Transient()); + + // + // Action Invoker + // // This accesses per-request services - services.TryAdd(ServiceDescriptor.Transient()); - services.TryAdd(ServiceDescriptor - .Transient()); + services.TryAddTransient(); + services.TryAddTransient(); services.TryAddEnumerable( ServiceDescriptor.Transient()); services.TryAddEnumerable( ServiceDescriptor.Transient()); - services.TryAddEnumerable( - ServiceDescriptor.Transient()); + // // ModelBinding, Validation and Formatting // // The DefaultModelMetadataProvider does significant caching and should be a singleton. - services.TryAdd(ServiceDescriptor.Singleton()); + services.TryAddSingleton(); services.TryAdd(ServiceDescriptor.Transient(serviceProvider => { var options = serviceProvider.GetRequiredService>().Options; @@ -104,18 +113,22 @@ namespace Microsoft.Framework.DependencyInjection return new DefaultObjectValidator(options.ValidationExcludeFilters, modelMetadataProvider); })); + // // Temp Data // - services.TryAdd(ServiceDescriptor.Scoped()); - // This does caching so it should stay singleton - services.TryAdd(ServiceDescriptor.Singleton()); + // Holds per-request data so it should be scoped + services.TryAddScoped(); + // This does caching so it should stay singleton + services.TryAddSingleton(); + + // // Random Infrastructure // - services.TryAdd(ServiceDescriptor.Transient()); - services.TryAdd((ServiceDescriptor.Singleton())); - services.TryAdd(ServiceDescriptor.Scoped(typeof(IScopedInstance<>), typeof(ScopedInstance<>))); - services.TryAdd(ServiceDescriptor.Scoped()); + services.TryAddTransient(); + services.TryAddSingleton(); + services.TryAddScoped(typeof(IScopedInstance<>), typeof(ScopedInstance<>)); + services.TryAddScoped(); } private static void ConfigureDefaultServices(IServiceCollection services) diff --git a/src/Microsoft.AspNet.Mvc/MvcServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Mvc/MvcServiceCollectionExtensions.cs index 84754443f5..806decbd4f 100644 --- a/src/Microsoft.AspNet.Mvc/MvcServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Mvc/MvcServiceCollectionExtensions.cs @@ -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()); - services.TryAdd(ServiceDescriptor.Transient()); + services.TryAddTransient(); // Auth services.TryAddEnumerable( @@ -173,7 +173,7 @@ namespace Microsoft.Framework.DependencyInjection .Transient()); // Formatter Mappings - services.TryAdd(ServiceDescriptor.Transient()); + services.TryAddTransient(); // JsonOutputFormatter should use the SerializerSettings on MvcOptions services.TryAdd(ServiceDescriptor.Singleton(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()); + services.TryAddScoped(); // Caches view locations that are valid for the lifetime of the application. - services.TryAdd(ServiceDescriptor.Singleton()); + services.TryAddSingleton(); services.TryAdd(ServiceDescriptor.Singleton(serviceProvider => { var cachedFileProvider = serviceProvider.GetRequiredService>(); @@ -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()); + services.TryAddTransient(); // Caches compilation artifacts across the lifetime of the application. - services.TryAdd(ServiceDescriptor.Singleton()); + services.TryAddSingleton(); // 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()); + services.TryAddSingleton(); // 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()); + services.TryAddTransient(); // The ViewStartProvider needs to be able to consume scoped instances of IRazorPageFactory - services.TryAdd(ServiceDescriptor.Scoped()); - services.TryAdd(ServiceDescriptor.Transient()); - services.TryAdd(ServiceDescriptor.Singleton()); + services.TryAddScoped(); + services.TryAddTransient(); + services.TryAddSingleton(); // Virtual path view factory needs to stay scoped so views can get get scoped services. - services.TryAdd(ServiceDescriptor.Scoped()); + services.TryAddScoped(); // View and rendering helpers - services.TryAdd(ServiceDescriptor.Transient()); - services.TryAdd(ServiceDescriptor.Transient(typeof(IHtmlHelper<>), typeof(HtmlHelper<>))); - services.TryAdd(ServiceDescriptor.Transient()); + services.TryAddTransient(); + services.TryAddTransient(typeof(IHtmlHelper<>), typeof(HtmlHelper<>)); + services.TryAddTransient(); // Only want one ITagHelperActivator so it can cache Type activation information. Types won't conflict. - services.TryAdd(ServiceDescriptor.Singleton()); + services.TryAddSingleton(); // Consumed by the Cache tag helper to cache results across the lifetime of the application. - services.TryAdd(ServiceDescriptor.Singleton()); + services.TryAddSingleton(); // DefaultHtmlGenerator is pretty much stateless but depends on Scoped services such as IUrlHelper and // IActionBindingContextProvider. Therefore it too is scoped. - services.TryAdd(ServiceDescriptor.Transient()); + services.TryAddTransient(); // These do caching so they should stay singleton - services.TryAdd(ServiceDescriptor.Singleton()); - services.TryAdd(ServiceDescriptor.Singleton()); - services.TryAdd(ServiceDescriptor.Singleton< - IViewComponentDescriptorCollectionProvider, - DefaultViewComponentDescriptorCollectionProvider>()); + services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton< + IViewComponentDescriptorCollectionProvider, + DefaultViewComponentDescriptorCollectionProvider>(); - services.TryAdd(ServiceDescriptor - .Transient()); - services.TryAdd(ServiceDescriptor - .Transient()); - services.TryAdd(ServiceDescriptor.Transient()); + services.TryAddTransient(); + services.TryAddTransient(); + services.TryAddTransient(); // Security and Authorization - services.TryAdd(ServiceDescriptor.Singleton()); - services.TryAdd(ServiceDescriptor.Singleton()); - services.TryAdd(ServiceDescriptor - .Singleton()); + services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); // Api Description - services.TryAdd(ServiceDescriptor - .Singleton()); + services.TryAddSingleton(); services.TryAddEnumerable( ServiceDescriptor.Transient()); }