From 9a5707d16771bd10af6db9afdb009311ff59b914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 3 Jan 2017 17:24:33 +0300 Subject: [PATCH] Refactor and fix ServiceCollectionRepositoryExtensions --- .../ServiceCollectionRepositoryExtensions.cs | 61 +++++++++++++------ 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs b/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs index 964c869175..9b4c5a1e15 100644 --- a/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs +++ b/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Reflection; using Microsoft.Extensions.DependencyInjection.Extensions; +using Volo.Abp; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; using Volo.Abp.Reflection; @@ -9,33 +10,53 @@ namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionRepositoryExtensions { - //TODO: validate repository and entity if they match! - public static void AddDefaultRepository(this IServiceCollection services, Type entityType, Type repositoryImplementationType) + { + AddDefaultRepositoryForGenericPrimaryKey(services, entityType, repositoryImplementationType); + + if (BothSupportsDefaultPrimaryKey(entityType, repositoryImplementationType)) + { + AddDefaultRepositoryForDefaultPrimaryKey(services, entityType, repositoryImplementationType); + } + } + + private static void AddDefaultRepositoryForGenericPrimaryKey(IServiceCollection services, Type entityType, Type repositoryImplementationType) { var primaryKeyType = EntityHelper.GetPrimaryKeyType(entityType); - services.TryAddTransient( - typeof(IRepository<,>).MakeGenericType(entityType, primaryKeyType), - repositoryImplementationType - ); + //IRepository + var repositoryInterface = typeof(IRepository<,>).MakeGenericType(entityType, primaryKeyType); + if (!repositoryInterface.GetTypeInfo().IsAssignableFrom(repositoryImplementationType)) + { + throw new AbpException($"Given repositoryImplementationType ({repositoryImplementationType}) must implement {repositoryInterface}"); + } + + services.TryAddTransient(repositoryInterface, repositoryImplementationType); - services.TryAddTransient( //TODO: May not support IQueryableRepository - typeof(IQueryableRepository<,>).MakeGenericType(entityType, primaryKeyType), - repositoryImplementationType - ); + //IQueryableRepository + var queryableRepositoryInterface = typeof(IQueryableRepository<,>).MakeGenericType(entityType, primaryKeyType); + if (queryableRepositoryInterface.GetTypeInfo().IsAssignableFrom(repositoryImplementationType)) + { + services.TryAddTransient(queryableRepositoryInterface, repositoryImplementationType); + } + } - if (BothSupportsDefaultPrimaryKey(entityType, repositoryImplementationType)) + private static void AddDefaultRepositoryForDefaultPrimaryKey(IServiceCollection services, Type entityType, Type repositoryImplementationType) + { + //IRepository + var repositoryInterfaceWithDefaultPrimaryKey = typeof(IRepository<>).MakeGenericType(entityType); + if (!repositoryInterfaceWithDefaultPrimaryKey.GetTypeInfo().IsAssignableFrom(repositoryImplementationType)) + { + throw new AbpException($"Given repositoryImplementationType ({repositoryImplementationType}) must implement {repositoryInterfaceWithDefaultPrimaryKey}"); + } + + services.TryAddTransient(repositoryInterfaceWithDefaultPrimaryKey, repositoryImplementationType); + + //IQueryableRepository + var queryableRepositoryInterfaceWithDefaultPrimaryKey = typeof(IQueryableRepository<>).MakeGenericType(entityType); + if (queryableRepositoryInterfaceWithDefaultPrimaryKey.GetTypeInfo().IsAssignableFrom(repositoryImplementationType)) { - services.TryAddTransient( - typeof(IRepository<>).MakeGenericType(entityType), - repositoryImplementationType - ); - - services.TryAddTransient( //TODO: May not support IQueryableRepository - typeof(IQueryableRepository<>).MakeGenericType(entityType), - repositoryImplementationType - ); + services.TryAddTransient(queryableRepositoryInterfaceWithDefaultPrimaryKey, repositoryImplementationType); } }