|
|
|
@ -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<TEntity>
|
|
|
|
var readOnlyRepositoryInterface = typeof(IReadOnlyRepository<>).MakeGenericType(entityType); |
|
|
|
if (readOnlyRepositoryInterface.IsAssignableFrom(repositoryImplementationType)) |
|
|
|
{ |
|
|
|
services.TryAddTransient(readOnlyRepositoryInterface, repositoryImplementationType); |
|
|
|
} |
|
|
|
|
|
|
|
//IBasicRepository<TEntity>
|
|
|
|
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<TEntity, TKey>
|
|
|
|
var basicRepositoryInterfaceWithPk = typeof(IBasicRepository<,>).MakeGenericType(entityType, primaryKeyType); |
|
|
|
if (basicRepositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) |
|
|
|
//IReadOnlyBasicRepository<TEntity, TKey>
|
|
|
|
var readOnlyBasicRepositoryInterfaceWithPk = typeof(IReadOnlyBasicRepository<,>).MakeGenericType(entityType, primaryKeyType); |
|
|
|
if (readOnlyBasicRepositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) |
|
|
|
{ |
|
|
|
services.TryAddTransient(basicRepositoryInterfaceWithPk, repositoryImplementationType); |
|
|
|
services.TryAddTransient(readOnlyBasicRepositoryInterfaceWithPk, repositoryImplementationType); |
|
|
|
|
|
|
|
//IRepository<TEntity, TKey>
|
|
|
|
var repositoryInterfaceWithPk = typeof(IRepository<,>).MakeGenericType(entityType, primaryKeyType); |
|
|
|
if (repositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) |
|
|
|
//IReadOnlyRepository<TEntity, TKey>
|
|
|
|
var readOnlyRepositoryInterfaceWithPk = typeof(IReadOnlyRepository<,>).MakeGenericType(entityType, primaryKeyType); |
|
|
|
if (readOnlyRepositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) |
|
|
|
{ |
|
|
|
services.TryAddTransient(repositoryInterfaceWithPk, repositoryImplementationType); |
|
|
|
services.TryAddTransient(readOnlyRepositoryInterfaceWithPk, repositoryImplementationType); |
|
|
|
} |
|
|
|
|
|
|
|
//IBasicRepository<TEntity, TKey>
|
|
|
|
var basicRepositoryInterfaceWithPk = typeof(IBasicRepository<,>).MakeGenericType(entityType, primaryKeyType); |
|
|
|
if (basicRepositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) |
|
|
|
{ |
|
|
|
services.TryAddTransient(basicRepositoryInterfaceWithPk, repositoryImplementationType); |
|
|
|
|
|
|
|
//IRepository<TEntity, TKey>
|
|
|
|
var repositoryInterfaceWithPk = typeof(IRepository<,>).MakeGenericType(entityType, primaryKeyType); |
|
|
|
if (repositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType)) |
|
|
|
{ |
|
|
|
services.TryAddTransient(repositoryInterfaceWithPk, repositoryImplementationType); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|