diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DefaultConventionalRegistrar.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DefaultConventionalRegistrar.cs index 5f4d64f512..476aaf3846 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DefaultConventionalRegistrar.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DefaultConventionalRegistrar.cs @@ -26,7 +26,7 @@ namespace Volo.Abp.DependencyInjection return; } - var exposedServiceTypes = ExposedServiceExplorer.GetExposedServices(type); + var exposedServiceTypes = GetExposedServiceTypes(type); TriggerServiceExposing(services, type, exposedServiceTypes); @@ -54,6 +54,11 @@ namespace Volo.Abp.DependencyInjection } } + protected virtual List GetExposedServiceTypes(Type type) + { + return ExposedServiceExplorer.GetExposedServices(type); + } + protected virtual ServiceDescriptor CreateServiceDescriptor( Type implementationType, Type exposingServiceType, @@ -117,10 +122,10 @@ namespace Volo.Abp.DependencyInjection protected virtual ServiceLifetime? GetLifeTimeOrNull(Type type, [CanBeNull] DependencyAttribute dependencyAttribute) { - return dependencyAttribute?.Lifetime ?? GetServiceLifetimeFromClassHierarcy(type); + return dependencyAttribute?.Lifetime ?? GetServiceLifetimeFromClassHierarchy(type); } - protected virtual ServiceLifetime? GetServiceLifetimeFromClassHierarcy(Type type) + protected virtual ServiceLifetime? GetServiceLifetimeFromClassHierarchy(Type type) { if (typeof(ITransientDependency).GetTypeInfo().IsAssignableFrom(type)) { @@ -140,4 +145,4 @@ namespace Volo.Abp.DependencyInjection return null; } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/AbpDddDomainModule.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/AbpDddDomainModule.cs index 41767d213e..523a6ee4f2 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/AbpDddDomainModule.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/AbpDddDomainModule.cs @@ -1,5 +1,7 @@ -using Volo.Abp.Auditing; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Auditing; using Volo.Abp.Data; +using Volo.Abp.Domain.Repositories; using Volo.Abp.EventBus; using Volo.Abp.ExceptionHandling; using Volo.Abp.Guids; @@ -28,6 +30,9 @@ namespace Volo.Abp.Domain )] public class AbpDddDomainModule : AbpModule { - + public override void PreConfigureServices(ServiceConfigurationContext context) + { + context.Services.AddConventionalRegistrar(new AbpRepositoryConventionalRegistrar()); + } } } diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/AbpRepositoryConventionalRegistrar.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/AbpRepositoryConventionalRegistrar.cs new file mode 100644 index 0000000000..17c01a23dd --- /dev/null +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/AbpRepositoryConventionalRegistrar.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Domain.Repositories +{ + public class AbpRepositoryConventionalRegistrar : DefaultConventionalRegistrar + { + public static bool ExposeRepositoryClasses { get; set; } + + protected override bool IsConventionalRegistrationDisabled(Type type) + { + if (!typeof(IRepository).IsAssignableFrom(type)) + { + return false; + } + + return base.IsConventionalRegistrationDisabled(type); + } + + protected override List GetExposedServiceTypes(Type type) + { + if (ExposeRepositoryClasses) + { + return base.GetExposedServiceTypes(type); + } + + return base.GetExposedServiceTypes(type) + .Where(x => x.IsInterface) + .ToList(); + } + + protected override ServiceLifetime? GetServiceLifetimeFromClassHierarchy(Type type) + { + return base.GetServiceLifetimeFromClassHierarchy(type) ?? + ServiceLifetime.Transient; + } + } +} diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs index ae1b03475c..9904a184ff 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs @@ -9,11 +9,10 @@ using Volo.Abp.Uow; namespace Volo.Abp.Domain.Repositories { - public abstract class BasicRepositoryBase : - IBasicRepository, + public abstract class BasicRepositoryBase : + IBasicRepository, IServiceProviderAccessor, - IUnitOfWorkEnabled, - ITransientDependency + IUnitOfWorkEnabled where TEntity : class, IEntity { public IServiceProvider ServiceProvider { get; set; } @@ -59,7 +58,7 @@ namespace Volo.Abp.Domain.Repositories } public abstract Task FindAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default); - + public virtual async Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default) { var entity = await FindAsync(id, cancellationToken: cancellationToken);