mirror of https://github.com/abpframework/abp.git
23 changed files with 270 additions and 58 deletions
@ -1,11 +1,9 @@ |
|||
using System; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.DependencyInjection |
|||
{ |
|||
public interface IServiceProviderAccessor |
|||
{ |
|||
[NotNull] |
|||
IServiceProvider ServiceProvider { get; } |
|||
} |
|||
} |
|||
@ -1,14 +1,27 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.DependencyInjection |
|||
{ |
|||
public class AbpDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IAbpDbContextRegistrationOptionsBuilder |
|||
{ |
|||
public AbpDbContextRegistrationOptions(Type originalDbContextType) |
|||
: base(originalDbContextType) |
|||
public Dictionary<Type, object> EntityOptions { get; } |
|||
|
|||
public AbpDbContextRegistrationOptions(Type originalDbContextType, IServiceCollection services) |
|||
: base(originalDbContextType, services) |
|||
{ |
|||
EntityOptions = new Dictionary<Type, object>(); |
|||
} |
|||
|
|||
public void Entity<TEntity>(Action<EntityOptions<TEntity>> optionsAction) where TEntity : IEntity |
|||
{ |
|||
Services.Configure<EntityOptions>(options => |
|||
{ |
|||
options.Entity(optionsAction); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.DependencyInjection |
|||
{ |
|||
public class EntityOptions<TEntity> |
|||
where TEntity : IEntity |
|||
{ |
|||
public static EntityOptions<TEntity> Empty { get; } = new EntityOptions<TEntity>(); |
|||
|
|||
public Func<IQueryable<TEntity>, IQueryable<TEntity>> IncludeDetailsFunc { get; set; } |
|||
} |
|||
|
|||
public class EntityOptions |
|||
{ |
|||
private readonly IDictionary<Type, object> _options; |
|||
|
|||
public EntityOptions() |
|||
{ |
|||
_options = new Dictionary<Type, object>(); |
|||
} |
|||
|
|||
public EntityOptions<TEntity> GetOrNull<TEntity>() |
|||
where TEntity : IEntity |
|||
{ |
|||
return _options.GetOrDefault(typeof(TEntity)) as EntityOptions<TEntity>; |
|||
} |
|||
|
|||
public void Entity<TEntity>([NotNull] Action<EntityOptions<TEntity>> optionsAction) |
|||
where TEntity : IEntity |
|||
{ |
|||
Check.NotNull(optionsAction, nameof(optionsAction)); |
|||
|
|||
optionsAction( |
|||
_options.GetOrAdd( |
|||
typeof(TEntity), |
|||
() => new EntityOptions<TEntity>() |
|||
) as EntityOptions<TEntity> |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -1,8 +1,13 @@ |
|||
using System; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.DependencyInjection |
|||
{ |
|||
public interface IAbpDbContextRegistrationOptionsBuilder : ICommonDbContextRegistrationOptionsBuilder |
|||
{ |
|||
void Entity<TEntity>([NotNull] Action<EntityOptions<TEntity>> optionsAction) |
|||
where TEntity : IEntity; |
|||
} |
|||
} |
|||
@ -1,12 +1,13 @@ |
|||
using System; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.MemoryDb.DependencyInjection |
|||
{ |
|||
public class MemoryDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IMemoryDbContextRegistrationOptionsBuilder |
|||
{ |
|||
public MemoryDbContextRegistrationOptions(Type originalDbContextType) |
|||
: base(originalDbContextType) |
|||
public MemoryDbContextRegistrationOptions(Type originalDbContextType, IServiceCollection services) |
|||
: base(originalDbContextType, services) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
@ -1,12 +1,13 @@ |
|||
using System; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.MongoDB.DependencyInjection |
|||
{ |
|||
public class MongoDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IMongoDbContextRegistrationOptionsBuilder |
|||
{ |
|||
public MongoDbContextRegistrationOptions(Type originalDbContextType) |
|||
: base(originalDbContextType) |
|||
public MongoDbContextRegistrationOptions(Type originalDbContextType, IServiceCollection services) |
|||
: base(originalDbContextType, services) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.TestApp.Testing; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.DataFiltering |
|||
{ |
|||
public class SoftDelete_Tests : SoftDelete_Tests<AbpEntityFrameworkCoreTestModule> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.TestApp.Testing; |
|||
|
|||
namespace Volo.Abp.MongoDB.DataFiltering |
|||
{ |
|||
public class SoftDelete_Tests : SoftDelete_Tests<AbpMongoDbTestModule> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,64 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.TestApp.Domain; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.TestApp.Testing |
|||
{ |
|||
public abstract class SoftDelete_Tests<TStartupModule> : TestAppTestBase<TStartupModule> |
|||
where TStartupModule : IAbpModule |
|||
{ |
|||
protected readonly IRepository<Person, Guid> PersonRepository; |
|||
protected readonly IDataFilter DataFilter; |
|||
|
|||
protected SoftDelete_Tests() |
|||
{ |
|||
PersonRepository = GetRequiredService<IRepository<Person, Guid>>(); |
|||
DataFilter = GetRequiredService<IDataFilter>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Cancel_Deletion_For_Soft_Delete_Entities() |
|||
{ |
|||
var douglas = await PersonRepository.GetAsync(TestDataBuilder.UserDouglasId); |
|||
await PersonRepository.DeleteAsync(douglas); |
|||
|
|||
douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); |
|||
douglas.ShouldBeNull(); |
|||
|
|||
using (DataFilter.Disable<ISoftDelete>()) |
|||
{ |
|||
douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); |
|||
douglas.ShouldNotBeNull(); |
|||
douglas.IsDeleted.ShouldBeTrue(); |
|||
douglas.DeletionTime.ShouldNotBeNull(); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Handle_Deletion_On_Update_For_Soft_Delete_Entities() |
|||
{ |
|||
var douglas = await PersonRepository.GetAsync(TestDataBuilder.UserDouglasId); |
|||
douglas.Age = 42; |
|||
douglas.IsDeleted = true; |
|||
|
|||
await PersonRepository.UpdateAsync(douglas); |
|||
|
|||
douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); |
|||
douglas.ShouldBeNull(); |
|||
|
|||
using (DataFilter.Disable<ISoftDelete>()) |
|||
{ |
|||
douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); |
|||
douglas.ShouldNotBeNull(); |
|||
douglas.IsDeleted.ShouldBeTrue(); |
|||
douglas.DeletionTime.ShouldNotBeNull(); |
|||
douglas.Age.ShouldBe(42); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue