Browse Source

Refactor.

pull/10376/head
maliming 5 years ago
parent
commit
2919be386e
No known key found for this signature in database GPG Key ID: 96224957E51C89E
  1. 40
      framework/src/Volo.Abp.AspNetCore.Components/Volo/Abp/AspNetCore/Components/DependencyInjection/AbpWebAssemblyConventionalRegistrar.cs
  2. 86
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalRegistrar.cs
  3. 42
      framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpSignalRConventionalRegistrar.cs
  4. 23
      framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperConventionalRegistrar.cs
  5. 24
      framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/AbpQuartzConventionalRegistrar.cs
  6. 95
      framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ConventionalRegistrarBase.cs
  7. 95
      framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DefaultConventionalRegistrar.cs
  8. 15
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/AbpRepositoryConventionalRegistrar.cs
  9. 39
      framework/src/Volo.Abp.FluentValidation/Volo/Abp/FluentValidation/AbpFluentValidationConventionalRegistrar.cs
  10. 26
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/AbpMongoDbConventionalRegistrar.cs
  11. 11
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalRegistrar_Tests.cs

40
framework/src/Volo.Abp.AspNetCore.Components/Volo/Abp/AspNetCore/Components/DependencyInjection/AbpWebAssemblyConventionalRegistrar.cs

@ -1,43 +1,31 @@
using System;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Components.DependencyInjection
{
public class AbpWebAssemblyConventionalRegistrar : ConventionalRegistrarBase
public class AbpWebAssemblyConventionalRegistrar : DefaultConventionalRegistrar
{
public override void AddType(IServiceCollection services, Type type)
protected override bool IsConventionalRegistrationDisabled(Type type)
{
if (IsConventionalRegistrationDisabled(type))
{
return;
}
if (!IsComponent(type))
{
return;
}
var serviceTypes = ExposedServiceExplorer.GetExposedServices(type);
TriggerServiceExposing(services, type, serviceTypes);
foreach (var serviceType in serviceTypes)
{
services.Add(
ServiceDescriptor.Describe(
serviceType,
type,
ServiceLifetime.Transient
)
);
}
return !IsComponent(type) || base.IsConventionalRegistrationDisabled(type);
}
private static bool IsComponent(Type type)
{
return typeof(ComponentBase).IsAssignableFrom(type);
}
protected override ServiceLifetime? GetLifeTimeOrNull(Type type, [CanBeNull] DependencyAttribute dependencyAttribute)
{
return dependencyAttribute?.Lifetime ?? GetWebAssemblyServiceLifetime(type);
}
protected virtual ServiceLifetime GetWebAssemblyServiceLifetime(Type type)
{
return ServiceLifetime.Transient;
}
}
}

86
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalRegistrar.cs

@ -1,81 +1,18 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection
{
public class AbpAspNetCoreMvcConventionalRegistrar : ConventionalRegistrarBase
public class AbpAspNetCoreMvcConventionalRegistrar : DefaultConventionalRegistrar
{
public override void AddType(IServiceCollection services, Type type)
protected override bool IsConventionalRegistrationDisabled(Type type)
{
if (IsConventionalRegistrationDisabled(type))
{
return;
}
if (!IsMvcService(type))
{
return;
}
var lifeTime = GetMvcServiceLifetime(type);
var dependencyAttribute = GetDependencyAttributeOrNull(type);
var exposedServiceTypes = GetExposedServiceTypes(type);
TriggerServiceExposing(services, type, exposedServiceTypes);
foreach (var exposedServiceType in exposedServiceTypes)
{
var serviceDescriptor = CreateServiceDescriptor(
type,
exposedServiceType,
exposedServiceTypes,
lifeTime
);
if (dependencyAttribute?.ReplaceServices == true)
{
services.Replace(serviceDescriptor);
}
else if (dependencyAttribute?.TryRegister == true)
{
services.TryAdd(serviceDescriptor);
}
else
{
services.Add(serviceDescriptor);
}
}
}
protected virtual DependencyAttribute GetDependencyAttributeOrNull(Type type)
{
return type.GetCustomAttribute<DependencyAttribute>(true);
}
protected virtual List<Type> GetExposedServiceTypes(Type type)
{
return ExposedServiceExplorer.GetExposedServices(type);
}
protected virtual ServiceDescriptor CreateServiceDescriptor(
Type implementationType,
Type exposingServiceType,
List<Type> allExposingServiceTypes,
ServiceLifetime lifeTime)
{
return ServiceDescriptor.Describe(
exposingServiceType,
implementationType,
lifeTime
);
return !IsMvcService(type) || base.IsConventionalRegistrationDisabled(type);
}
protected virtual bool IsMvcService(Type type)
@ -85,11 +22,6 @@ namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection
IsViewComponent(type);
}
protected virtual ServiceLifetime GetMvcServiceLifetime(Type type)
{
return ServiceLifetime.Transient;
}
private static bool IsPageModel(Type type)
{
return typeof(PageModel).IsAssignableFrom(type) || type.IsDefined(typeof(PageModelAttribute), true);
@ -104,5 +36,15 @@ namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection
{
return typeof(ViewComponent).IsAssignableFrom(type) || type.IsDefined(typeof(ViewComponentAttribute), true);
}
protected override ServiceLifetime? GetLifeTimeOrNull(Type type, [CanBeNull] DependencyAttribute dependencyAttribute)
{
return dependencyAttribute?.Lifetime ?? GetMvcServiceLifetime(type);
}
protected virtual ServiceLifetime GetMvcServiceLifetime(Type type)
{
return ServiceLifetime.Transient;
}
}
}

42
framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpSignalRConventionalRegistrar.cs

@ -1,43 +1,31 @@
using System;
using JetBrains.Annotations;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.SignalR
{
public class AbpSignalRConventionalRegistrar : ConventionalRegistrarBase
public class AbpSignalRConventionalRegistrar : DefaultConventionalRegistrar
{
public override void AddType(IServiceCollection services, Type type)
protected override bool IsConventionalRegistrationDisabled(Type type)
{
if (IsConventionalRegistrationDisabled(type))
{
return;
}
if (!IsHub(type))
{
return;
}
var serviceTypes = ExposedServiceExplorer.GetExposedServices(type);
TriggerServiceExposing(services, type, serviceTypes);
foreach (var serviceType in serviceTypes)
{
services.Add(
ServiceDescriptor.Describe(
serviceType,
type,
ServiceLifetime.Transient
)
);
}
return !IsHub(type) || base.IsConventionalRegistrationDisabled(type);
}
private static bool IsHub(Type type)
{
return typeof(Hub).IsAssignableFrom(type);
}
protected override ServiceLifetime? GetLifeTimeOrNull(Type type, [CanBeNull] DependencyAttribute dependencyAttribute)
{
return dependencyAttribute?.Lifetime ?? GetSignalRServiceLifetime(type);
}
protected virtual ServiceLifetime GetSignalRServiceLifetime(Type type)
{
return ServiceLifetime.Transient;
}
}
}

23
framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperConventionalRegistrar.cs

@ -3,12 +3,11 @@ using System.Linq;
using AutoMapper;
using AutoMapper.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AutoMapper
{
public class AbpAutoMapperConventionalRegistrar : ConventionalRegistrarBase
public class AbpAutoMapperConventionalRegistrar : DefaultConventionalRegistrar
{
protected readonly Type[] OpenTypes = {
typeof(IValueResolver<,,>),
@ -18,17 +17,19 @@ namespace Volo.Abp.AutoMapper
typeof(IMappingAction<,>)
};
public override void AddType(IServiceCollection services, Type type)
protected override bool IsConventionalRegistrationDisabled(Type type)
{
if (IsConventionalRegistrationDisabled(type))
{
return;
}
return !OpenTypes.Any(type.ImplementsGenericInterface) || base.IsConventionalRegistrationDisabled(type);
}
if (type.IsClass && !type.IsAbstract && OpenTypes.Any(type.ImplementsGenericInterface))
{
services.TryAddTransient(type);
}
protected override ServiceLifetime? GetLifeTimeOrNull(Type type, DependencyAttribute dependencyAttribute)
{
return base.GetLifeTimeOrNull(type, dependencyAttribute) ?? GetAutoMapperServiceLifetime(type);
}
protected virtual ServiceLifetime GetAutoMapperServiceLifetime(Type type)
{
return ServiceLifetime.Transient;
}
}
}

24
framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/AbpQuartzConventionalRegistrar.cs

@ -1,27 +1,21 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.BackgroundWorkers.Quartz
{
public class AbpQuartzConventionalRegistrar : DefaultConventionalRegistrar
{
public override void AddType(IServiceCollection services, Type type)
protected override bool IsConventionalRegistrationDisabled(Type type)
{
if (!typeof(IQuartzBackgroundWorker).IsAssignableFrom(type))
{
return;
}
var dependencyAttribute = GetDependencyAttributeOrNull(type);
var lifeTime = GetLifeTimeOrNull(type, dependencyAttribute);
if (lifeTime == null)
{
return;
}
return !typeof(IQuartzBackgroundWorker).IsAssignableFrom(type) || base.IsConventionalRegistrationDisabled(type);
}
services.Add(ServiceDescriptor.Describe(typeof(IQuartzBackgroundWorker), type, lifeTime.Value));
protected override List<Type> GetExposedServiceTypes(Type type)
{
var serviceTypes = base.GetExposedServiceTypes(type);
serviceTypes.AddIfNotContains(typeof(IQuartzBackgroundWorker));
return serviceTypes;
}
}
}

95
framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ConventionalRegistrarBase.cs

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Reflection;
@ -50,5 +51,97 @@ namespace Volo.Abp.DependencyInjection
}
}
}
protected virtual DependencyAttribute GetDependencyAttributeOrNull(Type type)
{
return type.GetCustomAttribute<DependencyAttribute>(true);
}
protected virtual ServiceLifetime? GetLifeTimeOrNull(Type type, [CanBeNull] DependencyAttribute dependencyAttribute)
{
return dependencyAttribute?.Lifetime ?? GetServiceLifetimeFromClassHierarchy(type);
}
protected virtual ServiceLifetime? GetServiceLifetimeFromClassHierarchy(Type type)
{
if (typeof(ITransientDependency).GetTypeInfo().IsAssignableFrom(type))
{
return ServiceLifetime.Transient;
}
if (typeof(ISingletonDependency).GetTypeInfo().IsAssignableFrom(type))
{
return ServiceLifetime.Singleton;
}
if (typeof(IScopedDependency).GetTypeInfo().IsAssignableFrom(type))
{
return ServiceLifetime.Scoped;
}
return null;
}
protected virtual List<Type> GetExposedServiceTypes(Type type)
{
return ExposedServiceExplorer.GetExposedServices(type);
}
protected virtual ServiceDescriptor CreateServiceDescriptor(
Type implementationType,
Type exposingServiceType,
List<Type> allExposingServiceTypes,
ServiceLifetime lifeTime)
{
if (lifeTime.IsIn(ServiceLifetime.Singleton, ServiceLifetime.Scoped))
{
var redirectedType = GetRedirectedTypeOrNull(
implementationType,
exposingServiceType,
allExposingServiceTypes
);
if (redirectedType != null)
{
return ServiceDescriptor.Describe(
exposingServiceType,
provider => provider.GetService(redirectedType),
lifeTime
);
}
}
return ServiceDescriptor.Describe(
exposingServiceType,
implementationType,
lifeTime
);
}
protected virtual Type GetRedirectedTypeOrNull(
Type implementationType,
Type exposingServiceType,
List<Type> allExposingServiceTypes)
{
if (allExposingServiceTypes.Count < 2)
{
return null;
}
if (exposingServiceType == implementationType)
{
return null;
}
if (allExposingServiceTypes.Contains(implementationType))
{
return implementationType;
}
return allExposingServiceTypes.FirstOrDefault(
t => t != exposingServiceType && exposingServiceType.IsAssignableFrom(t)
);
}
}
}
}

95
framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DefaultConventionalRegistrar.cs

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
@ -53,96 +49,5 @@ namespace Volo.Abp.DependencyInjection
}
}
}
protected virtual List<Type> GetExposedServiceTypes(Type type)
{
return ExposedServiceExplorer.GetExposedServices(type);
}
protected virtual ServiceDescriptor CreateServiceDescriptor(
Type implementationType,
Type exposingServiceType,
List<Type> allExposingServiceTypes,
ServiceLifetime lifeTime)
{
if (lifeTime.IsIn(ServiceLifetime.Singleton, ServiceLifetime.Scoped))
{
var redirectedType = GetRedirectedTypeOrNull(
implementationType,
exposingServiceType,
allExposingServiceTypes
);
if (redirectedType != null)
{
return ServiceDescriptor.Describe(
exposingServiceType,
provider => provider.GetService(redirectedType),
lifeTime
);
}
}
return ServiceDescriptor.Describe(
exposingServiceType,
implementationType,
lifeTime
);
}
protected virtual Type GetRedirectedTypeOrNull(
Type implementationType,
Type exposingServiceType,
List<Type> allExposingServiceTypes)
{
if (allExposingServiceTypes.Count < 2)
{
return null;
}
if (exposingServiceType == implementationType)
{
return null;
}
if (allExposingServiceTypes.Contains(implementationType))
{
return implementationType;
}
return allExposingServiceTypes.FirstOrDefault(
t => t != exposingServiceType && exposingServiceType.IsAssignableFrom(t)
);
}
protected virtual DependencyAttribute GetDependencyAttributeOrNull(Type type)
{
return type.GetCustomAttribute<DependencyAttribute>(true);
}
protected virtual ServiceLifetime? GetLifeTimeOrNull(Type type, [CanBeNull] DependencyAttribute dependencyAttribute)
{
return dependencyAttribute?.Lifetime ?? GetServiceLifetimeFromClassHierarchy(type);
}
protected virtual ServiceLifetime? GetServiceLifetimeFromClassHierarchy(Type type)
{
if (typeof(ITransientDependency).GetTypeInfo().IsAssignableFrom(type))
{
return ServiceLifetime.Transient;
}
if (typeof(ISingletonDependency).GetTypeInfo().IsAssignableFrom(type))
{
return ServiceLifetime.Singleton;
}
if (typeof(IScopedDependency).GetTypeInfo().IsAssignableFrom(type))
{
return ServiceLifetime.Scoped;
}
return null;
}
}
}

15
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/AbpRepositoryConventionalRegistrar.cs

@ -15,12 +15,7 @@ namespace Volo.Abp.Domain.Repositories
protected override bool IsConventionalRegistrationDisabled(Type type)
{
if (!typeof(IRepository).IsAssignableFrom(type))
{
return true;
}
return base.IsConventionalRegistrationDisabled(type);
return !typeof(IRepository).IsAssignableFrom(type) || base.IsConventionalRegistrationDisabled(type);
}
protected override List<Type> GetExposedServiceTypes(Type type)
@ -38,7 +33,13 @@ namespace Volo.Abp.Domain.Repositories
protected override ServiceLifetime? GetServiceLifetimeFromClassHierarchy(Type type)
{
return base.GetServiceLifetimeFromClassHierarchy(type) ??
ServiceLifetime.Transient;
GetRepositoryServiceLifetime(type);
}
protected virtual ServiceLifetime GetRepositoryServiceLifetime(Type type)
{
return ServiceLifetime.Transient;
}
}
}

39
framework/src/Volo.Abp.FluentValidation/Volo/Abp/FluentValidation/AbpFluentValidationConventionalRegistrar.cs

@ -6,34 +6,35 @@ using Volo.Abp.DependencyInjection;
namespace Volo.Abp.FluentValidation
{
public class AbpFluentValidationConventionalRegistrar : ConventionalRegistrarBase
public class AbpFluentValidationConventionalRegistrar : DefaultConventionalRegistrar
{
public override void AddType(IServiceCollection services, Type type)
protected override bool IsConventionalRegistrationDisabled(Type type)
{
if (IsConventionalRegistrationDisabled(type))
{
return;
}
return !typeof(IValidator).IsAssignableFrom(type) || base.IsConventionalRegistrationDisabled(type);
}
if (!typeof(IValidator).IsAssignableFrom(type))
{
return;
}
protected override ServiceLifetime? GetLifeTimeOrNull(Type type, DependencyAttribute dependencyAttribute)
{
return base.GetLifeTimeOrNull(type, dependencyAttribute) ?? GetValidatorServiceLifetime(type);
}
protected virtual ServiceLifetime GetValidatorServiceLifetime(Type type)
{
return ServiceLifetime.Transient;
}
protected override List<Type> GetExposedServiceTypes(Type type)
{
var validatingType = GetFirstGenericArgumentOrNull(type, 1);
if (validatingType == null)
{
return;
return new List<Type>();
}
var serviceType = typeof(IValidator<>).MakeGenericType(validatingType);
TriggerServiceExposing(services, type, new List<Type>{ serviceType });
services.AddTransient(
serviceType,
type
);
return new List<Type>()
{
typeof(IValidator<>).MakeGenericType(validatingType)
};
}
private static Type GetFirstGenericArgumentOrNull(Type type, int depth)

26
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/AbpMongoDbConventionalRegistrar.cs

@ -1,29 +1,25 @@
using System;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.MongoDB.DependencyInjection
{
public class AbpMongoDbConventionalRegistrar : DefaultConventionalRegistrar
{
public override void AddType(IServiceCollection services, Type type)
protected override bool IsConventionalRegistrationDisabled(Type type)
{
if (!typeof(IAbpMongoDbContext).IsAssignableFrom(type) || type == typeof(AbpMongoDbContext))
{
return;
}
var dependencyAttribute = GetDependencyAttributeOrNull(type);
var lifeTime = GetLifeTimeOrNull(type, dependencyAttribute);
return !typeof(IAbpMongoDbContext).IsAssignableFrom(type) || type == typeof(AbpMongoDbContext) || base.IsConventionalRegistrationDisabled(type);
}
if (lifeTime == null)
{
return;
}
protected override ServiceLifetime? GetLifeTimeOrNull(Type type, [CanBeNull] DependencyAttribute dependencyAttribute)
{
return dependencyAttribute?.Lifetime ?? GetAbpMongoDbContextLifetime(type);
}
services.Add(ServiceDescriptor.Describe(typeof(IAbpMongoDbContext), type, ServiceLifetime.Transient));
protected virtual ServiceLifetime GetAbpMongoDbContextLifetime(Type type)
{
return ServiceLifetime.Transient;
}
}
}

11
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalRegistrar_Tests.cs

@ -23,9 +23,9 @@ namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection
services.AddTypes(typeof(My_Test_PageModel), typeof(My_Test_Controller), typeof(My_Test_ViewComponent));
//Assert
services.ShouldContainTransient(typeof(My_Test_PageModel));
services.ShouldContainTransient(typeof(My_Test_Controller));
services.ShouldContainTransient(typeof(My_Test_ViewComponent));
services.ShouldContainTransient(typeof(My_Test_PageModel));
services.ShouldContainTransient(typeof(My_Test_Controller));
services.ShouldContainTransient(typeof(My_Test_ViewComponent));
var serviceProvider = services.BuildServiceProvider();
serviceProvider.GetServices<My_Test_PageModel>().Count().ShouldBe(1);
@ -46,8 +46,7 @@ namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection
services.AddTypes(typeof(My_Second_Test_ViewComponent), typeof(My_Third_Test_ViewComponent));
//Assert
services.ShouldContainTransient(typeof(My_Second_Test_ViewComponent),
typeof(My_Third_Test_ViewComponent));
services.ShouldContainTransient(typeof(My_Second_Test_ViewComponent), typeof(My_Third_Test_ViewComponent));
services.ShouldNotContainService(typeof(My_Third_Test_ViewComponent));
var serviceProvider = services.BuildServiceProvider();
@ -78,4 +77,4 @@ namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection
public class My_Third_Test_ViewComponent : My_Second_Test_ViewComponent
{
}
}
}

Loading…
Cancel
Save