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)