Browse Source

Add support for custom entity names in repositories

Introduces the ability to set a custom entity name for repositories by adding a SetCustomEntityName method to IRepository and BasicRepositoryBase. Updates EfCoreRepository to use the custom entity name when accessing DbSet, and extends IEfCoreDbContext with an overload of Set<T> that accepts a name. Test stubs are updated to implement the new method.
pull/23588/head
Halil İbrahim Kalkan 1 year ago
parent
commit
e1ea80495c
  1. 7
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs
  2. 2
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs
  3. 31
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
  4. 3
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContext.cs
  5. 5
      framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs

7
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs

@ -43,6 +43,13 @@ public abstract class BasicRepositoryBase<TEntity> :
public IEntityChangeTrackingProvider EntityChangeTrackingProvider => LazyServiceProvider.LazyGetRequiredService<IEntityChangeTrackingProvider>(); public IEntityChangeTrackingProvider EntityChangeTrackingProvider => LazyServiceProvider.LazyGetRequiredService<IEntityChangeTrackingProvider>();
public bool? IsChangeTrackingEnabled { get; protected set; } public bool? IsChangeTrackingEnabled { get; protected set; }
protected string? CustomEntityName { get; private set; }
public void SetCustomEntityName(string? name)
{
CustomEntityName = name;
}
protected BasicRepositoryBase() protected BasicRepositoryBase()
{ {

2
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs

@ -13,6 +13,8 @@ namespace Volo.Abp.Domain.Repositories;
public interface IRepository public interface IRepository
{ {
bool? IsChangeTrackingEnabled { get; } bool? IsChangeTrackingEnabled { get; }
void SetCustomEntityName(string? name);
} }
public interface IRepository<TEntity> : IReadOnlyRepository<TEntity>, IBasicRepository<TEntity> public interface IRepository<TEntity> : IReadOnlyRepository<TEntity>, IBasicRepository<TEntity>

31
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs

@ -61,7 +61,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
} }
[Obsolete("Use GetDbSetAsync() method.")] [Obsolete("Use GetDbSetAsync() method.")]
public virtual DbSet<TEntity> DbSet => DbContext.Set<TEntity>(); public virtual DbSet<TEntity> DbSet => GetDbSetInternal(DbContext);
Task<DbSet<TEntity>> IEfCoreRepository<TEntity>.GetDbSetAsync() Task<DbSet<TEntity>> IEfCoreRepository<TEntity>.GetDbSetAsync()
{ {
@ -70,7 +70,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
protected async Task<DbSet<TEntity>> GetDbSetAsync() protected async Task<DbSet<TEntity>> GetDbSetAsync()
{ {
return (await GetDbContextAsync()).Set<TEntity>(); return GetDbSetInternal(await GetDbContextAsync());
} }
protected async Task<IDbConnection> GetDbConnectionAsync() protected async Task<IDbConnection> GetDbConnectionAsync()
@ -110,7 +110,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
var savedEntity = (await dbContext.Set<TEntity>().AddAsync(entity, GetCancellationToken(cancellationToken))).Entity; var savedEntity = (await GetDbSetInternal(dbContext).AddAsync(entity, GetCancellationToken(cancellationToken))).Entity;
if (autoSave) if (autoSave)
{ {
@ -120,6 +120,13 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
return savedEntity; return savedEntity;
} }
private DbSet<TEntity> GetDbSetInternal(TDbContext dbContext)
{
return CustomEntityName != null
? dbContext.Set<TEntity>(CustomEntityName)
: dbContext.Set<TEntity>();
}
public async override Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default) public async override Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
var entityArray = entities.ToArray(); var entityArray = entities.ToArray();
@ -147,7 +154,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
return; return;
} }
await dbContext.Set<TEntity>().AddRangeAsync(entityArray, cancellationToken); await GetDbSetInternal(dbContext).AddRangeAsync(entityArray, cancellationToken);
if (autoSave) if (autoSave)
{ {
@ -159,9 +166,10 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
{ {
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
if (dbContext.Set<TEntity>().Local.All(e => e != entity)) var dbSet = GetDbSetInternal(dbContext);
if (dbSet.Local.All(e => e != entity))
{ {
dbContext.Set<TEntity>().Attach(entity); dbSet.Attach(entity);
dbContext.Update(entity); dbContext.Update(entity);
} }
@ -197,7 +205,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
dbContext.Set<TEntity>().UpdateRange(entityArray); GetDbSetInternal(dbContext).UpdateRange(entityArray);
if (autoSave) if (autoSave)
{ {
@ -209,7 +217,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
{ {
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
dbContext.Set<TEntity>().Remove(entity); GetDbSetInternal(dbContext).Remove(entity);
if (autoSave) if (autoSave)
{ {
@ -318,7 +326,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
public async override Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, bool autoSave = false, CancellationToken cancellationToken = default) public async override Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
var dbSet = dbContext.Set<TEntity>(); var dbSet = GetDbSetInternal(dbContext);
var entities = await dbSet var entities = await dbSet
.Where(predicate) .Where(predicate)
@ -335,8 +343,9 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
public async override Task DeleteDirectAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) public async override Task DeleteDirectAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
{ {
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
var dbSet = dbContext.Set<TEntity>(); await GetDbSetInternal(dbContext)
await dbSet.Where(predicate).ExecuteDeleteAsync(GetCancellationToken(cancellationToken)); .Where(predicate)
.ExecuteDeleteAsync(GetCancellationToken(cancellationToken));
} }
public virtual async Task EnsureCollectionLoadedAsync<TProperty>( public virtual async Task EnsureCollectionLoadedAsync<TProperty>(

3
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContext.cs

@ -31,6 +31,9 @@ public interface IEfCoreDbContext : IDisposable, IInfrastructure<IServiceProvide
DbSet<T> Set<T>() DbSet<T> Set<T>()
where T : class; where T : class;
DbSet<T> Set<T>(string name)
where T : class;
DatabaseFacade Database { get; } DatabaseFacade Database { get; }

5
framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs

@ -408,6 +408,11 @@ public class RepositoryRegistration_Tests
public class MyTestAggregateRootWithDefaultPkEmptyRepository : IMyTestAggregateRootWithDefaultPkEmptyRepository public class MyTestAggregateRootWithDefaultPkEmptyRepository : IMyTestAggregateRootWithDefaultPkEmptyRepository
{ {
public bool? IsChangeTrackingEnabled { get; set; } public bool? IsChangeTrackingEnabled { get; set; }
public void SetCustomEntityName(string name)
{
}
} }
public class TestDbContextRegistrationOptions : AbpCommonDbContextRegistrationOptions public class TestDbContextRegistrationOptions : AbpCommonDbContextRegistrationOptions

Loading…
Cancel
Save