mirror of https://github.com/abpframework/abp.git
9 changed files with 129 additions and 87 deletions
@ -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); |
|||
} |
|||
} |
|||
} |
|||
@ -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…
Reference in new issue