From aa52be621f75c53596eec8c919ca0875bc951063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sun, 18 Dec 2016 14:01:12 +0300 Subject: [PATCH] Removed tryadds and added new attributes to control registration progress. --- ...ServiceCollectionRegistrationExtensions.cs | 65 +++++-------------- .../DependencyAttribute.cs | 24 +++++++ ...isableConventionalRegistrationAttribute.cs | 9 +++ .../SkipAutoRegistrationAttribute.cs | 9 --- 4 files changed, 49 insertions(+), 58 deletions(-) create mode 100644 src/Volo.DependencyInjection/Volo/DependencyInjection/DependencyAttribute.cs create mode 100644 src/Volo.DependencyInjection/Volo/DependencyInjection/DisableConventionalRegistrationAttribute.cs delete mode 100644 src/Volo.DependencyInjection/Volo/DependencyInjection/SkipAutoRegistrationAttribute.cs diff --git a/src/Volo.DependencyInjection/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationExtensions.cs b/src/Volo.DependencyInjection/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationExtensions.cs index 99e899f93b..d5d3211ba0 100644 --- a/src/Volo.DependencyInjection/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationExtensions.cs +++ b/src/Volo.DependencyInjection/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationExtensions.cs @@ -10,35 +10,14 @@ namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionRegistrationExtensions { - //TODO: When to use TryAdd, when to use Add (create an attribute to declare it) - //TODO: Make this code extensible, so we can add other conventions! + //TODO: Make this code extensible, so we can add other conventions! Also, extract default convention to a class which implements the convention interface. public static IServiceCollection AddAssemblyOf(this IServiceCollection services) { - return services.InternalAddAssemblyOf(false); - } - - public static IServiceCollection TryAddAssemblyOf(this IServiceCollection services) - { - return services.InternalAddAssemblyOf(true); - } - - internal static IServiceCollection InternalAddAssemblyOf(this IServiceCollection services, bool tryAdd) - { - return services.InternalAddAssembly(typeof(T).GetTypeInfo().Assembly, tryAdd); + return services.AddAssembly(typeof(T).GetTypeInfo().Assembly); } public static IServiceCollection AddAssembly(this IServiceCollection services, Assembly assembly) - { - return services.InternalAddAssembly(assembly, false); - } - - public static IServiceCollection TryAddAssembly(this IServiceCollection services, Assembly assembly) - { - return services.InternalAddAssembly(assembly, true); - } - - internal static IServiceCollection InternalAddAssembly(this IServiceCollection services, Assembly assembly, bool tryAdd) { var types = AssemblyHelper .GetAllTypes(assembly) @@ -47,28 +26,17 @@ namespace Microsoft.Extensions.DependencyInjection var typeInfo = t.GetTypeInfo(); return typeInfo.IsClass && !typeInfo.IsAbstract && - !typeInfo.IsGenericType && - !typeInfo.IsDefined(typeof(SkipAutoRegistrationAttribute)); + !typeInfo.IsGenericType; }); - return services.InternalAddTypes(tryAdd, types.ToArray()); + return services.AddTypes(types.ToArray()); } public static IServiceCollection AddTypes(this IServiceCollection services, params Type[] types) - { - return services.InternalAddTypes(false, types); - } - - public static IServiceCollection TryAddTypes(this IServiceCollection services, params Type[] types) - { - return services.InternalAddTypes(true, types); - } - - internal static IServiceCollection InternalAddTypes(this IServiceCollection services, bool tryAdd, params Type[] types) { foreach (var type in types) { - services.AddType(type, tryAdd); + services.AddType(type); } return services; @@ -76,27 +44,26 @@ namespace Microsoft.Extensions.DependencyInjection public static IServiceCollection AddType(this IServiceCollection services, Type type) { - return services.AddType(type, false); - } + var typeInfo = type.GetTypeInfo(); - public static IServiceCollection TryAddType(this IServiceCollection services, Type type) - { - return services.AddType(type, true); - } + if (typeInfo.IsDefined(typeof(DisableConventionalRegistrationAttribute), true)) + { + return services; + } - internal static IServiceCollection AddType(this IServiceCollection services, Type type, bool tryAdd) - { - var lifeTime = GetServiceLifeTime(type); + var dependencyAttribute = typeInfo.GetCustomAttributes(true).FirstOrDefault(); //TODO: Use GetCustomAttribute instead? + + var lifeTime = dependencyAttribute?.Lifetime ?? GetServiceLifetimeFromInterfaces(type); if (lifeTime == null) { return services; } - + foreach (var serviceType in AutoRegistrationHelper.GetExposedServices(type)) { var serviceDescriptor = ServiceDescriptor.Describe(serviceType, type, lifeTime.Value); - if (tryAdd) + if (dependencyAttribute?.TryRegister == true) { services.TryAdd(serviceDescriptor); } @@ -110,7 +77,7 @@ namespace Microsoft.Extensions.DependencyInjection } [CanBeNull] - internal static ServiceLifetime? GetServiceLifeTime(Type type) + internal static ServiceLifetime? GetServiceLifetimeFromInterfaces(Type type) { if (typeof(ITransientDependency).GetTypeInfo().IsAssignableFrom(type)) { diff --git a/src/Volo.DependencyInjection/Volo/DependencyInjection/DependencyAttribute.cs b/src/Volo.DependencyInjection/Volo/DependencyInjection/DependencyAttribute.cs new file mode 100644 index 0000000000..82b56ea1f2 --- /dev/null +++ b/src/Volo.DependencyInjection/Volo/DependencyInjection/DependencyAttribute.cs @@ -0,0 +1,24 @@ +using System; +using JetBrains.Annotations; +using Microsoft.Extensions.DependencyInjection; + +namespace Volo.DependencyInjection +{ + public class DependencyAttribute : Attribute + { + [CanBeNull] + public virtual ServiceLifetime? Lifetime { get; set; } + + public virtual bool TryRegister { get; set; } + + public DependencyAttribute() + { + + } + + public DependencyAttribute(ServiceLifetime lifetime) + { + Lifetime = lifetime; + } + } +} \ No newline at end of file diff --git a/src/Volo.DependencyInjection/Volo/DependencyInjection/DisableConventionalRegistrationAttribute.cs b/src/Volo.DependencyInjection/Volo/DependencyInjection/DisableConventionalRegistrationAttribute.cs new file mode 100644 index 0000000000..3e1244a276 --- /dev/null +++ b/src/Volo.DependencyInjection/Volo/DependencyInjection/DisableConventionalRegistrationAttribute.cs @@ -0,0 +1,9 @@ +using System; + +namespace Volo.DependencyInjection +{ + public class DisableConventionalRegistrationAttribute : Attribute + { + + } +} \ No newline at end of file diff --git a/src/Volo.DependencyInjection/Volo/DependencyInjection/SkipAutoRegistrationAttribute.cs b/src/Volo.DependencyInjection/Volo/DependencyInjection/SkipAutoRegistrationAttribute.cs deleted file mode 100644 index b83feeee44..0000000000 --- a/src/Volo.DependencyInjection/Volo/DependencyInjection/SkipAutoRegistrationAttribute.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace Volo.DependencyInjection -{ - public class SkipAutoRegistrationAttribute : Attribute - { - - } -} \ No newline at end of file