Browse Source

Removed tryadds and added new attributes to control registration progress.

pull/81/head
Halil İbrahim Kalkan 10 years ago
parent
commit
aa52be621f
  1. 65
      src/Volo.DependencyInjection/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationExtensions.cs
  2. 24
      src/Volo.DependencyInjection/Volo/DependencyInjection/DependencyAttribute.cs
  3. 9
      src/Volo.DependencyInjection/Volo/DependencyInjection/DisableConventionalRegistrationAttribute.cs
  4. 9
      src/Volo.DependencyInjection/Volo/DependencyInjection/SkipAutoRegistrationAttribute.cs

65
src/Volo.DependencyInjection/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationExtensions.cs

@ -10,35 +10,14 @@ namespace Microsoft.Extensions.DependencyInjection
{ {
public static class ServiceCollectionRegistrationExtensions 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! Also, extract default convention to a class which implements the convention interface.
//TODO: Make this code extensible, so we can add other conventions!
public static IServiceCollection AddAssemblyOf<T>(this IServiceCollection services) public static IServiceCollection AddAssemblyOf<T>(this IServiceCollection services)
{ {
return services.InternalAddAssemblyOf<T>(false); return services.AddAssembly(typeof(T).GetTypeInfo().Assembly);
}
public static IServiceCollection TryAddAssemblyOf<T>(this IServiceCollection services)
{
return services.InternalAddAssemblyOf<T>(true);
}
internal static IServiceCollection InternalAddAssemblyOf<T>(this IServiceCollection services, bool tryAdd)
{
return services.InternalAddAssembly(typeof(T).GetTypeInfo().Assembly, tryAdd);
} }
public static IServiceCollection AddAssembly(this IServiceCollection services, Assembly 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 var types = AssemblyHelper
.GetAllTypes(assembly) .GetAllTypes(assembly)
@ -47,28 +26,17 @@ namespace Microsoft.Extensions.DependencyInjection
var typeInfo = t.GetTypeInfo(); var typeInfo = t.GetTypeInfo();
return typeInfo.IsClass && return typeInfo.IsClass &&
!typeInfo.IsAbstract && !typeInfo.IsAbstract &&
!typeInfo.IsGenericType && !typeInfo.IsGenericType;
!typeInfo.IsDefined(typeof(SkipAutoRegistrationAttribute));
}); });
return services.InternalAddTypes(tryAdd, types.ToArray()); return services.AddTypes(types.ToArray());
} }
public static IServiceCollection AddTypes(this IServiceCollection services, params Type[] types) 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) foreach (var type in types)
{ {
services.AddType(type, tryAdd); services.AddType(type);
} }
return services; return services;
@ -76,27 +44,26 @@ namespace Microsoft.Extensions.DependencyInjection
public static IServiceCollection AddType(this IServiceCollection services, Type type) 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) if (typeInfo.IsDefined(typeof(DisableConventionalRegistrationAttribute), true))
{ {
return services.AddType(type, true); return services;
} }
internal static IServiceCollection AddType(this IServiceCollection services, Type type, bool tryAdd) var dependencyAttribute = typeInfo.GetCustomAttributes<DependencyAttribute>(true).FirstOrDefault(); //TODO: Use GetCustomAttribute instead?
{
var lifeTime = GetServiceLifeTime(type); var lifeTime = dependencyAttribute?.Lifetime ?? GetServiceLifetimeFromInterfaces(type);
if (lifeTime == null) if (lifeTime == null)
{ {
return services; return services;
} }
foreach (var serviceType in AutoRegistrationHelper.GetExposedServices(type)) foreach (var serviceType in AutoRegistrationHelper.GetExposedServices(type))
{ {
var serviceDescriptor = ServiceDescriptor.Describe(serviceType, type, lifeTime.Value); var serviceDescriptor = ServiceDescriptor.Describe(serviceType, type, lifeTime.Value);
if (tryAdd) if (dependencyAttribute?.TryRegister == true)
{ {
services.TryAdd(serviceDescriptor); services.TryAdd(serviceDescriptor);
} }
@ -110,7 +77,7 @@ namespace Microsoft.Extensions.DependencyInjection
} }
[CanBeNull] [CanBeNull]
internal static ServiceLifetime? GetServiceLifeTime(Type type) internal static ServiceLifetime? GetServiceLifetimeFromInterfaces(Type type)
{ {
if (typeof(ITransientDependency).GetTypeInfo().IsAssignableFrom(type)) if (typeof(ITransientDependency).GetTypeInfo().IsAssignableFrom(type))
{ {

24
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;
}
}
}

9
src/Volo.DependencyInjection/Volo/DependencyInjection/DisableConventionalRegistrationAttribute.cs

@ -0,0 +1,9 @@
using System;
namespace Volo.DependencyInjection
{
public class DisableConventionalRegistrationAttribute : Attribute
{
}
}

9
src/Volo.DependencyInjection/Volo/DependencyInjection/SkipAutoRegistrationAttribute.cs

@ -1,9 +0,0 @@
using System;
namespace Volo.DependencyInjection
{
public class SkipAutoRegistrationAttribute : Attribute
{
}
}
Loading…
Cancel
Save