Skip to content

Commit

Permalink
Resolved #426: Enable and Disable filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Mar 28, 2015
1 parent c0b8116 commit 95a856f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Abp.EntityFramework/EntityFramework/AbpDbContext.cs
Expand Up @@ -101,7 +101,7 @@ protected AbpDbContext(DbConnection existingConnection, DbCompiledModel model, b
EntityChangedEventHelper = NullEntityChangedEventHelper.Instance;
}

public void Initialize()
public virtual void Initialize()
{
Database.Initialize(false);
}
Expand Down
23 changes: 15 additions & 8 deletions src/Abp.EntityFramework/EntityFramework/Uow/EfUnitOfWork.cs
Expand Up @@ -16,7 +16,6 @@ namespace Abp.EntityFramework.Uow
public class EfUnitOfWork : UnitOfWorkBase, ITransientDependency
{
private readonly IDictionary<Type, DbContext> _activeDbContexts;
private readonly List<string> _disabledFilters;
private readonly IIocResolver _iocResolver;
private TransactionScope _transaction;

Expand All @@ -27,7 +26,6 @@ public EfUnitOfWork(IIocResolver iocResolver)
{
_iocResolver = iocResolver;
_activeDbContexts = new Dictionary<Type, DbContext>();
_disabledFilters = new List<string>();
}

protected override void BeginUow()
Expand Down Expand Up @@ -85,15 +83,19 @@ protected override async Task CompleteUowAsync()

public override void DisableFilter(string filterName)
{
if (_disabledFilters.Contains(filterName))
base.DisableFilter(filterName);
foreach (var activeDbContext in _activeDbContexts.Values)
{
return;
activeDbContext.DisableFilter(filterName);
}
}

_disabledFilters.Add(filterName);
public override void EnableFilter(string filterName)
{
base.EnableFilter(filterName);
foreach (var activeDbContext in _activeDbContexts.Values)
{
activeDbContext.DisableFilter(filterName);
activeDbContext.EnableFilter(filterName);
}
}

Expand All @@ -105,9 +107,14 @@ internal TDbContext GetOrCreateDbContext<TDbContext>()
{
dbContext = _iocResolver.Resolve<TDbContext>();

foreach (var disabledFilter in _disabledFilters)
foreach (var filterName in _disabledFilters)
{
dbContext.DisableFilter(filterName);
}

foreach (var filterName in _enabledFilters)
{
dbContext.DisableFilter(disabledFilter);
dbContext.EnableFilter(filterName);
}

_activeDbContexts[typeof(TDbContext)] = dbContext;
Expand Down
8 changes: 8 additions & 0 deletions src/Abp/Domain/Uow/IActiveUnitOfWork.cs
Expand Up @@ -55,8 +55,16 @@ public interface IActiveUnitOfWork

/// <summary>
/// Disables a data filter.
/// Does nothing if it's already disabled.
/// </summary>
/// <param name="filterName">Name of the filter. <see cref="AbpDataFilters"/> for standard filters.</param>
void DisableFilter(string filterName);

/// <summary>
/// Enables a data filter.
/// Does nothing if it's already disabled.
/// </summary>
/// <param name="filterName"></param>
void EnableFilter(string filterName);
}
}
34 changes: 34 additions & 0 deletions src/Abp/Domain/Uow/UnitOfWorkBase.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Abp.Extensions;

Expand Down Expand Up @@ -46,6 +47,18 @@ public abstract class UnitOfWorkBase : IUnitOfWork
/// </summary>
private Exception _exception;

protected readonly List<string> _disabledFilters;
protected readonly List<string> _enabledFilters;

/// <summary>
/// Constructor.
/// </summary>
protected UnitOfWorkBase()
{
_disabledFilters = new List<string>();
_enabledFilters = new List<string>();
}

/// <inheritdoc/>
public void Begin(UnitOfWorkOptions options)
{
Expand All @@ -67,7 +80,28 @@ public void Begin(UnitOfWorkOptions options)

public virtual void DisableFilter(string filterName)
{
if (!_disabledFilters.Contains(filterName))
{
_disabledFilters.Add(filterName);
}

if (_enabledFilters.Contains(filterName))
{
_enabledFilters.Remove(filterName);
}
}

public virtual void EnableFilter(string filterName)
{
if (_disabledFilters.Contains(filterName))
{
_disabledFilters.Remove(filterName);
}

if (!_enabledFilters.Contains(filterName))
{
_enabledFilters.Add(filterName);
}
}

/// <inheritdoc/>
Expand Down
@@ -1,5 +1,4 @@
using System.Linq;
using Abp.Domain.Repositories;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.TestBase.SampleApplication.People;
using Shouldly;
Expand Down Expand Up @@ -28,17 +27,18 @@ public void Should_Not_Retrieve_Soft_Deleteds()
}

[Fact]
public void Should_Retrieve_Soft_Deleteds_When_Filter_Disabled()
public void Should_Disable_And_Enable_Filters_For_SoftDelete()
{
var uowManager = Resolve<IUnitOfWorkManager>();
using (var ouw = uowManager.Begin())
{
_personRepository.GetAllList().Count.ShouldBe(1);

uowManager.Current.DisableFilter(AbpDataFilters.SoftDelete);
_personRepository.GetAllList().Count.ShouldBe(2);

var persons = _personRepository.GetAllList().OrderBy(p => p.Name).ToList();
persons.Count.ShouldBe(2);
persons[0].Name.ShouldBe("emre");
persons[1].Name.ShouldBe("halil");
uowManager.Current.EnableFilter(AbpDataFilters.SoftDelete);
_personRepository.GetAllList().Count.ShouldBe(1);

ouw.Complete();
}
Expand Down
Expand Up @@ -52,6 +52,11 @@ public MyDbContext(string nameOrConnectionString)
{
CalledConstructorWithConnectionString = true;
}

public override void Initialize()
{

}
}
}
}

0 comments on commit 95a856f

Please sign in to comment.