diff --git a/src/Volo.Abp.Ddd.Domain/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs b/src/Volo.Abp.Ddd.Domain/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs index 47a53c92da..1f2cae53a3 100644 --- a/src/Volo.Abp.Ddd.Domain/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs +++ b/src/Volo.Abp.Ddd.Domain/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs @@ -1,5 +1,4 @@ using System; -using System.Reflection; using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; @@ -10,6 +9,13 @@ namespace Microsoft.Extensions.DependencyInjection { public static IServiceCollection AddDefaultRepository(this IServiceCollection services, Type entityType, Type repositoryImplementationType) { + //IReadOnlyRepository + var readOnlyRepositoryInterface = typeof(IReadOnlyRepository<>).MakeGenericType(entityType); + if (readOnlyRepositoryInterface.IsAssignableFrom(repositoryImplementationType)) + { + services.TryAddTransient(readOnlyRepositoryInterface, repositoryImplementationType); + } + //IBasicRepository var basicRepositoryInterface = typeof(IBasicRepository<>).MakeGenericType(entityType); if (basicRepositoryInterface.IsAssignableFrom(repositoryImplementationType)) @@ -27,17 +33,31 @@ namespace Microsoft.Extensions.DependencyInjection var primaryKeyType = EntityHelper.FindPrimaryKeyType(entityType); if (primaryKeyType != null) { - //IBasicRepository - var basicRepositoryInterfaceWithPk = typeof(IBasicRepository<,>).MakeGenericType(entityType, primaryKeyType); - if (basicRepositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) + //IReadOnlyBasicRepository + var readOnlyBasicRepositoryInterfaceWithPk = typeof(IReadOnlyBasicRepository<,>).MakeGenericType(entityType, primaryKeyType); + if (readOnlyBasicRepositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) { - services.TryAddTransient(basicRepositoryInterfaceWithPk, repositoryImplementationType); + services.TryAddTransient(readOnlyBasicRepositoryInterfaceWithPk, repositoryImplementationType); - //IRepository - var repositoryInterfaceWithPk = typeof(IRepository<,>).MakeGenericType(entityType, primaryKeyType); - if (repositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) + //IReadOnlyRepository + var readOnlyRepositoryInterfaceWithPk = typeof(IReadOnlyRepository<,>).MakeGenericType(entityType, primaryKeyType); + if (readOnlyRepositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) { - services.TryAddTransient(repositoryInterfaceWithPk, repositoryImplementationType); + services.TryAddTransient(readOnlyRepositoryInterfaceWithPk, repositoryImplementationType); + } + + //IBasicRepository + var basicRepositoryInterfaceWithPk = typeof(IBasicRepository<,>).MakeGenericType(entityType, primaryKeyType); + if (basicRepositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) + { + services.TryAddTransient(basicRepositoryInterfaceWithPk, repositoryImplementationType); + + //IRepository + var repositoryInterfaceWithPk = typeof(IRepository<,>).MakeGenericType(entityType, primaryKeyType); + if (repositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) + { + services.TryAddTransient(repositoryInterfaceWithPk, repositoryImplementationType); + } } } } diff --git a/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs b/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs index 23b7dea3ba..0d4cbc9623 100644 --- a/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs +++ b/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs @@ -29,17 +29,25 @@ namespace Volo.Abp.Domain.Repositories //Assert //MyTestAggregateRootWithoutPk + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); //MyTestAggregateRootWithGuidPk + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); //MyTestEntityWithInt32Pk + services.ShouldNotContainService(typeof(IReadOnlyRepository)); + services.ShouldNotContainService(typeof(IBasicRepository)); + services.ShouldNotContainService(typeof(IRepository)); + services.ShouldNotContainService(typeof(IReadOnlyRepository)); services.ShouldNotContainService(typeof(IBasicRepository)); + services.ShouldNotContainService(typeof(IRepository)); } [Fact]