From 739db98e161cfa5413d303cacce1cd78849a01f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 16 Feb 2021 16:40:54 +0300 Subject: [PATCH] Resolved #7750: Re-registering custom repositories should also override the default repositories for the same entity. --- .../ServiceCollectionRepositoryExtensions.cs | 38 ++++++++++++++----- .../AbpRepositoryConventionalRegistrar.cs | 3 ++ .../Repositories/RepositoryRegistrarBase.cs | 2 +- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/framework/src/Volo.Abp.Ddd.Domain/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs b/framework/src/Volo.Abp.Ddd.Domain/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs index ac66f35864..60826a14dd 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs @@ -7,32 +7,36 @@ namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionRepositoryExtensions { - public static IServiceCollection AddDefaultRepository(this IServiceCollection services, Type entityType, Type repositoryImplementationType) + public static IServiceCollection AddDefaultRepository( + this IServiceCollection services, + Type entityType, + Type repositoryImplementationType, + bool replaceExisting = false) { //IReadOnlyBasicRepository var readOnlyBasicRepositoryInterface = typeof(IReadOnlyBasicRepository<>).MakeGenericType(entityType); if (readOnlyBasicRepositoryInterface.IsAssignableFrom(repositoryImplementationType)) { - services.TryAddTransient(readOnlyBasicRepositoryInterface, repositoryImplementationType); + RegisterService(services, readOnlyBasicRepositoryInterface, repositoryImplementationType, replaceExisting); //IReadOnlyRepository var readOnlyRepositoryInterface = typeof(IReadOnlyRepository<>).MakeGenericType(entityType); if (readOnlyRepositoryInterface.IsAssignableFrom(repositoryImplementationType)) { - services.TryAddTransient(readOnlyRepositoryInterface, repositoryImplementationType); + RegisterService(services, readOnlyRepositoryInterface, repositoryImplementationType, replaceExisting); } //IBasicRepository var basicRepositoryInterface = typeof(IBasicRepository<>).MakeGenericType(entityType); if (basicRepositoryInterface.IsAssignableFrom(repositoryImplementationType)) { - services.TryAddTransient(basicRepositoryInterface, repositoryImplementationType); + RegisterService(services, basicRepositoryInterface, repositoryImplementationType, replaceExisting); //IRepository var repositoryInterface = typeof(IRepository<>).MakeGenericType(entityType); if (repositoryInterface.IsAssignableFrom(repositoryImplementationType)) { - services.TryAddTransient(repositoryInterface, repositoryImplementationType); + RegisterService(services, repositoryInterface, repositoryImplementationType, replaceExisting); } } } @@ -44,26 +48,26 @@ namespace Microsoft.Extensions.DependencyInjection var readOnlyBasicRepositoryInterfaceWithPk = typeof(IReadOnlyBasicRepository<,>).MakeGenericType(entityType, primaryKeyType); if (readOnlyBasicRepositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) { - services.TryAddTransient(readOnlyBasicRepositoryInterfaceWithPk, repositoryImplementationType); + RegisterService(services, readOnlyBasicRepositoryInterfaceWithPk, repositoryImplementationType, replaceExisting); //IReadOnlyRepository var readOnlyRepositoryInterfaceWithPk = typeof(IReadOnlyRepository<,>).MakeGenericType(entityType, primaryKeyType); if (readOnlyRepositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) { - services.TryAddTransient(readOnlyRepositoryInterfaceWithPk, repositoryImplementationType); + RegisterService(services, readOnlyRepositoryInterfaceWithPk, repositoryImplementationType, replaceExisting); } //IBasicRepository var basicRepositoryInterfaceWithPk = typeof(IBasicRepository<,>).MakeGenericType(entityType, primaryKeyType); if (basicRepositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) { - services.TryAddTransient(basicRepositoryInterfaceWithPk, repositoryImplementationType); + RegisterService(services, basicRepositoryInterfaceWithPk, repositoryImplementationType, replaceExisting); //IRepository var repositoryInterfaceWithPk = typeof(IRepository<,>).MakeGenericType(entityType, primaryKeyType); if (repositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) { - services.TryAddTransient(repositoryInterfaceWithPk, repositoryImplementationType); + RegisterService(services, repositoryInterfaceWithPk, repositoryImplementationType, replaceExisting); } } } @@ -71,5 +75,21 @@ namespace Microsoft.Extensions.DependencyInjection return services; } + + private static void RegisterService( + IServiceCollection services, + Type serviceType, + Type implementationType, + bool replaceExisting) + { + if (replaceExisting) + { + services.Replace(ServiceDescriptor.Transient(serviceType, implementationType)); + } + else + { + services.TryAddTransient(serviceType, implementationType); + } + } } } \ No newline at end of file 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 index f11812f2ea..f7b9527aa0 100644 --- 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 @@ -6,6 +6,9 @@ using Volo.Abp.DependencyInjection; namespace Volo.Abp.Domain.Repositories { + /* Repositories are not injected by class by default. + * This class specializes repository registration to apply this rule. + */ public class AbpRepositoryConventionalRegistrar : DefaultConventionalRegistrar { public static bool ExposeRepositoryClasses { get; set; } diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs index f3b2fc388f..548a636eff 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs @@ -20,7 +20,7 @@ namespace Volo.Abp.Domain.Repositories { foreach (var customRepository in Options.CustomRepositories) { - Options.Services.AddDefaultRepository(customRepository.Key, customRepository.Value); + Options.Services.AddDefaultRepository(customRepository.Key, customRepository.Value, replaceExisting: true); } if (Options.RegisterDefaultRepositories)