Skip to content

Commit

Permalink
Rename RepositoryInterceptor and so on to ChangeTrackingInterceptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
maliming committed Sep 3, 2023
1 parent d625654 commit 9356884
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Volo.Abp.Auditing;
using Volo.Abp.Caching;
using Volo.Abp.Data;
using Volo.Abp.Domain.ChangeTracking;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus;
using Volo.Abp.ExceptionHandling;
Expand Down Expand Up @@ -30,6 +31,6 @@ public class AbpDddDomainModule : AbpModule
public override void PreConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddConventionalRegistrar(new AbpRepositoryConventionalRegistrar());
context.Services.OnRegistered(RepositoryInterceptorRegistrar.RegisterIfNeeded);
context.Services.OnRegistered(ChangeTrackingInterceptorRegistrar.RegisterIfNeeded);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Volo.Abp.Domain.Repositories;

namespace Volo.Abp.Domain.Repositories;
namespace Volo.Abp.Domain.ChangeTracking;

public static class RepositoryHelper
public static class ChangeTrackingHelper
{
public static bool IsEntityChangeTrackingType(TypeInfo implementationType)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.DynamicProxy;

namespace Volo.Abp.Domain.Repositories;
namespace Volo.Abp.Domain.ChangeTracking;

public class RepositoryInterceptor : AbpInterceptor, ITransientDependency
public class ChangeTrackingInterceptor : AbpInterceptor, ITransientDependency
{
private readonly IEntityChangeTrackingProvider _entityChangeTrackingProvider;

public RepositoryInterceptor(IEntityChangeTrackingProvider entityChangeTrackingProvider)
public ChangeTrackingInterceptor(IEntityChangeTrackingProvider entityChangeTrackingProvider)
{
_entityChangeTrackingProvider = entityChangeTrackingProvider;
}

public async override Task InterceptAsync(IAbpMethodInvocation invocation)
{
if (!RepositoryHelper.IsEntityChangeTrackingMethod(invocation.Method, out var changeTrackingAttribute))
if (!ChangeTrackingHelper.IsEntityChangeTrackingMethod(invocation.Method, out var changeTrackingAttribute))
{
await invocation.ProceedAsync();
return;

Check warning on line 22 in framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/ChangeTracking/ChangeTrackingInterceptor.cs

View check run for this annotation

Codecov / codecov/patch

framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/ChangeTracking/ChangeTrackingInterceptor.cs#L20-L22

Added lines #L20 - L22 were not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
using Volo.Abp.DependencyInjection;
using Volo.Abp.DynamicProxy;

namespace Volo.Abp.Domain.Repositories;
namespace Volo.Abp.Domain.ChangeTracking;

public class RepositoryInterceptorRegistrar
public class ChangeTrackingInterceptorRegistrar
{
public static void RegisterIfNeeded(IOnServiceRegistredContext context)
{
if (ShouldIntercept(context.ImplementationType))
{
context.Interceptors.TryAdd<RepositoryInterceptor>();
context.Interceptors.TryAdd<ChangeTrackingInterceptor>();
}
}

private static bool ShouldIntercept(Type type)
{
return !DynamicProxyIgnoreTypes.Contains(type) && RepositoryHelper.IsEntityChangeTrackingType(type.GetTypeInfo());
return !DynamicProxyIgnoreTypes.Contains(type) && ChangeTrackingHelper.IsEntityChangeTrackingType(type.GetTypeInfo());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;

namespace Volo.Abp.Domain.Repositories;
namespace Volo.Abp.Domain.ChangeTracking;

/// <summary>
/// Ensures that the change tracking in enabled for the given method or class.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Interface)]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class DisableEntityChangeTrackingAttribute : EntityChangeTrackingAttribute
{
public DisableEntityChangeTrackingAttribute()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;

namespace Volo.Abp.Domain.Repositories;
namespace Volo.Abp.Domain.ChangeTracking;

/// <summary>
/// Ensures that the change tracking in enabled for the given method or class.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Interface)]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class EnableEntityChangeTrackingAttribute : EntityChangeTrackingAttribute
{
public EnableEntityChangeTrackingAttribute()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;

namespace Volo.Abp.Domain.Repositories;
namespace Volo.Abp.Domain.ChangeTracking;

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Interface)]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public abstract class EntityChangeTrackingAttribute : Attribute
{
public virtual bool IsEnabled { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.ChangeTracking;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.TestApp.Testing;
using Xunit;

namespace Volo.Abp.EntityFrameworkCore.Repositories;
namespace Volo.Abp.EntityFrameworkCore.ChangeTracking;

public class RepositoryInterceptor_Tests : TestAppTestBase<AbpEntityFrameworkCoreTestModule>
public class ChangeTrackingInterceptor_Tests : TestAppTestBase<AbpEntityFrameworkCoreTestModule>
{
[Fact]
public async Task ReadOnly_Repository_Should_Not_Track_Entities()
Expand Down Expand Up @@ -65,18 +66,6 @@ await WithUnitOfWorkAsync(async () =>
db.ChangeTracker.Entries().Count().ShouldBe(1); // Track one entity from GetPeoplesAsync
});

Check warning on line 67 in framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/ChangeTracking/ChangeTrackingInterceptor_Tests.cs

View check run for this annotation

Codecov / codecov/patch

framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/ChangeTracking/ChangeTrackingInterceptor_Tests.cs#L57-L67

Added lines #L57 - L67 were not covered by tests

await WithUnitOfWorkAsync(async () =>
{
var service = GetRequiredService<MyServiceEnableEntityChangeTrackingOnInterface>();
var db = await repository.GetDbContextAsync();
db.ChangeTracker.Entries().Count().ShouldBe(0);
var list = await service.GetPeoplesAsync();
list.Count.ShouldBeGreaterThan(0);
db.ChangeTracker.Entries().Count().ShouldBe(1); // Track one entity from GetPeoplesAsync
});

await WithUnitOfWorkAsync(async () =>
{
var service = GetRequiredService<MyServiceChangeTrackingByEntityChangeTrackingProvider>();
Expand Down Expand Up @@ -163,38 +152,6 @@ public virtual async Task<Person> GetPeopleAsync()
}

Check warning on line 152 in framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/ChangeTracking/ChangeTrackingInterceptor_Tests.cs

View check run for this annotation

Codecov / codecov/patch

framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/ChangeTracking/ChangeTrackingInterceptor_Tests.cs#L149-L152

Added lines #L149 - L152 were not covered by tests
}


[EnableEntityChangeTracking]
public interface IMyServiceEnableEntityChangeTrackingOnInterface
{

}

public class MyServiceEnableEntityChangeTrackingOnInterface : IMyServiceEnableEntityChangeTrackingOnInterface, ITransientDependency
{
private readonly IRepository<Person, Guid> _repository;

public MyServiceEnableEntityChangeTrackingOnInterface(IRepository<Person, Guid> repository)
{
_repository = repository;
}

public virtual async Task<List<Person>> GetPeoplesAsync()
{
var p1 = await GetPeopleAsync();
var p2 = await _repository.FindAsync(x => x.Name == "people2");

return new List<Person> {p1, p2};
}

[DisableEntityChangeTracking]
public virtual async Task<Person> GetPeopleAsync()
{
var p1 = await _repository.FindAsync(x => x.Name == "people1");
return p1;
}
}

public class MyServiceChangeTrackingByEntityChangeTrackingProvider : ITransientDependency
{
private readonly IRepository<Person, Guid> _repository;
Expand Down

0 comments on commit 9356884

Please sign in to comment.