Refactored repository registration.

pull/81/head
Halil İbrahim Kalkan 9 years ago
parent d60ced3e2f
commit b068513f5b

@ -1,5 +1,4 @@
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
namespace Microsoft.Extensions.DependencyInjection
{

@ -1,9 +1,5 @@
using System;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp.Data;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
namespace Microsoft.Extensions.DependencyInjection
@ -23,45 +19,10 @@ namespace Microsoft.Extensions.DependencyInjection
var options = new AbpDbContextRegistrationOptions();
optionsBuilder?.Invoke(options);
AddRepositories<TDbContext>(services, options);
new EfCoreRepositoryRegistrar(options)
.AddRepositories(services, typeof(TDbContext));
return services;
}
private static void AddRepositories<TDbContext>(IServiceCollection services, AbpDbContextRegistrationOptions options)
where TDbContext : AbpDbContext<TDbContext>
{
foreach (var customRepository in options.CustomRepositories)
{
services.AddDefaultRepository(customRepository.Key, customRepository.Value);
}
if (options.RegisterDefaultRepositories)
{
RegisterDefaultRepositories(services, typeof(TDbContext), options);
}
}
private static void RegisterDefaultRepositories(IServiceCollection services, Type dbContextType, AbpDbContextRegistrationOptions options)
{
foreach (var entityType in DbContextHelper.GetEntityTypes(dbContextType))
{
if (!options.ShouldRegisterDefaultRepositoryFor(entityType))
{
continue;
}
RegisterDefaultRepository(services, dbContextType, entityType, options);
}
}
private static void RegisterDefaultRepository(IServiceCollection services, Type dbContextType, Type entityType, AbpDbContextRegistrationOptions options)
{
var repositoryImplementationType = typeof(IEntity).GetTypeInfo().IsAssignableFrom(entityType)
? typeof(EfCoreRepository<,>).MakeGenericType(dbContextType, entityType)
: typeof(EfCoreRepository<,,>).MakeGenericType(dbContextType, entityType, EntityHelper.GetPrimaryKeyType(entityType));
services.AddDefaultRepository(entityType, repositoryImplementationType);
}
}
}

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
namespace Microsoft.Extensions.DependencyInjection
{
public class EfCoreRepositoryRegistrar : RepositoryRegistrarBase<AbpDbContextRegistrationOptions>
{
public EfCoreRepositoryRegistrar(AbpDbContextRegistrationOptions options)
: base(options)
{
}
public override IEnumerable<Type> GetEntityTypes(Type dbContextType)
{
return DbContextHelper.GetEntityTypes(dbContextType);
}
protected override Type GetRepositoryTypeForDefaultPk(Type dbContextType, Type entityType)
{
return typeof(EfCoreRepository<,>).MakeGenericType(dbContextType, entityType);
}
protected override Type GetRepositoryType(Type dbContextType, Type entityType, Type primaryKeyType)
{
return typeof(EfCoreRepository<,,>).MakeGenericType(dbContextType, entityType, primaryKeyType);
}
}
}

@ -1,5 +1,4 @@
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
namespace Microsoft.Extensions.DependencyInjection
{

@ -1,6 +1,4 @@
using System;
using System.Reflection;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories.MongoDB;
using Volo.Abp.MongoDB;
@ -14,47 +12,10 @@ namespace Microsoft.Extensions.DependencyInjection
var options = new MongoDbContextRegistrationOptions();
optionsBuilder?.Invoke(options);
AddRepositories<TMongoDbContext>(services, options);
new MongoDbRepositoryRegistrar(options)
.AddRepositories(services, typeof(TMongoDbContext));
return services;
}
private static void AddRepositories<TMongoDbContext>(IServiceCollection services, MongoDbContextRegistrationOptions options)
where TMongoDbContext : AbpMongoDbContext
{
foreach (var customRepository in options.CustomRepositories)
{
services.AddDefaultRepository(customRepository.Key, customRepository.Value);
}
if (options.RegisterDefaultRepositories)
{
RegisterDefaultRepositories(services, typeof(TMongoDbContext), options);
}
}
private static void RegisterDefaultRepositories(IServiceCollection services, Type dbContextType, MongoDbContextRegistrationOptions options)
{
var mongoDbContext = (AbpMongoDbContext) Activator.CreateInstance(dbContextType);
foreach (var entityType in mongoDbContext.GetEntityCollectionTypes())
{
if (!options.ShouldRegisterDefaultRepositoryFor(entityType))
{
continue;
}
RegisterDefaultRepository(services, dbContextType, entityType, options);
}
}
private static void RegisterDefaultRepository(IServiceCollection services, Type dbContextType, Type entityType, MongoDbContextRegistrationOptions options)
{
var repositoryImplementationType = typeof(IEntity).GetTypeInfo().IsAssignableFrom(entityType)
? typeof(MongoDbRepository<,>).MakeGenericType(dbContextType, entityType)
: typeof(MongoDbRepository<,,>).MakeGenericType(dbContextType, entityType, EntityHelper.GetPrimaryKeyType(entityType));
services.AddDefaultRepository(entityType, repositoryImplementationType);
}
}
}

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Data;
using Volo.Abp.MongoDB;
namespace Volo.Abp.Domain.Repositories.MongoDB
{
public class MongoDbRepositoryRegistrar : RepositoryRegistrarBase<MongoDbContextRegistrationOptions>
{
public MongoDbRepositoryRegistrar(MongoDbContextRegistrationOptions options)
: base(options)
{
}
public override IEnumerable<Type> GetEntityTypes(Type dbContextType)
{
var mongoDbContext = (AbpMongoDbContext)Activator.CreateInstance(dbContextType);
return mongoDbContext.GetEntityCollectionTypes();
}
protected override Type GetRepositoryTypeForDefaultPk(Type dbContextType, Type entityType)
{
return typeof(MongoDbRepository<,>).MakeGenericType(dbContextType, entityType);
}
protected override Type GetRepositoryType(Type dbContextType, Type entityType, Type primaryKeyType)
{
return typeof(MongoDbRepository<,,>).MakeGenericType(dbContextType, entityType, primaryKeyType);
}
}
}

@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using Volo.Abp;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Reflection;
namespace Microsoft.Extensions.DependencyInjection
namespace Volo.Abp.Data
{
public class CommonDbContextRegistrationOptions : ICommonDbContextRegistrationOptionsBuilder
{

@ -1,4 +1,4 @@
namespace Microsoft.Extensions.DependencyInjection
namespace Volo.Abp.Data
{
public interface ICommonDbContextRegistrationOptionsBuilder
{

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Domain.Entities;
namespace Volo.Abp.Data
{
public abstract class RepositoryRegistrarBase<TOptions>
where TOptions: CommonDbContextRegistrationOptions
{
public TOptions Options { get; }
protected RepositoryRegistrarBase(TOptions options)
{
Options = options;
}
public virtual void AddRepositories(IServiceCollection services, Type dbContextType)
{
foreach (var customRepository in Options.CustomRepositories)
{
services.AddDefaultRepository(customRepository.Key, customRepository.Value);
}
if (Options.RegisterDefaultRepositories)
{
RegisterDefaultRepositories(services, dbContextType);
}
}
protected virtual void RegisterDefaultRepositories(IServiceCollection services, Type dbContextType)
{
foreach (var entityType in GetEntityTypes(dbContextType))
{
if (!Options.ShouldRegisterDefaultRepositoryFor(entityType))
{
continue;
}
RegisterDefaultRepository(services, dbContextType, entityType);
}
}
protected void RegisterDefaultRepository(IServiceCollection services, Type dbContextType, Type entityType)
{
var repositoryImplementationType = typeof(IEntity).GetTypeInfo().IsAssignableFrom(entityType)
? GetRepositoryTypeForDefaultPk(dbContextType, entityType)
: GetRepositoryType(dbContextType, entityType, EntityHelper.GetPrimaryKeyType(entityType));
services.AddDefaultRepository(entityType, repositoryImplementationType);
}
public abstract IEnumerable<Type> GetEntityTypes(Type dbContextType);
protected abstract Type GetRepositoryTypeForDefaultPk(Type dbContextType, Type entityType);
protected abstract Type GetRepositoryType(Type dbContextType, Type entityType, Type primaryKeyType);
}
}
Loading…
Cancel
Save