Browse Source

Refactor and fix ServiceCollectionRepositoryExtensions

pull/81/head
Halil İbrahim Kalkan 10 years ago
parent
commit
9a5707d167
  1. 61
      src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs

61
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<TEntity, TPrimaryKey>
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<TEntity, TPrimaryKey>
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<TEntity>
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<TEntity>
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);
}
}

Loading…
Cancel
Save