From 6f5e2e3552b6dd035b3e7358c33c52295d02b7bf Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 27 Mar 2025 11:56:01 +0800 Subject: [PATCH 1/4] Add IgnoreEventSelectors to AbpDistributedEntityEventOptions for event filtering. --- .../Distributed/AbpDistributedEntityEventOptions.cs | 3 +++ .../Domain/Entities/Events/EntityChangeEventHelper.cs | 10 +++------- .../AbpEntityFrameworkCoreModule.cs | 7 +++++++ .../Volo/Abp/MongoDB/AbpMongoDbModule.cs | 7 +++++++ 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/framework/src/Volo.Abp.Ddd.Domain.Shared/Volo/Abp/Domain/Entities/Events/Distributed/AbpDistributedEntityEventOptions.cs b/framework/src/Volo.Abp.Ddd.Domain.Shared/Volo/Abp/Domain/Entities/Events/Distributed/AbpDistributedEntityEventOptions.cs index 19af763c85..9168996bb5 100644 --- a/framework/src/Volo.Abp.Ddd.Domain.Shared/Volo/Abp/Domain/Entities/Events/Distributed/AbpDistributedEntityEventOptions.cs +++ b/framework/src/Volo.Abp.Ddd.Domain.Shared/Volo/Abp/Domain/Entities/Events/Distributed/AbpDistributedEntityEventOptions.cs @@ -4,11 +4,14 @@ public class AbpDistributedEntityEventOptions { public IAutoEntityDistributedEventSelectorList AutoEventSelectors { get; } + public IAutoEntityDistributedEventSelectorList IgnoreEventSelectors { get; } + public EtoMappingDictionary EtoMappings { get; set; } public AbpDistributedEntityEventOptions() { AutoEventSelectors = new AutoEntityDistributedEventSelectorList(); + IgnoreEventSelectors = new AutoEntityDistributedEventSelectorList(); EtoMappings = new EtoMappingDictionary(); } } diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs index 44eb5c058a..980390195a 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs @@ -68,13 +68,9 @@ public class EntityChangeEventHelper : IEntityChangeEventHelper, ITransientDepen private bool ShouldPublishDistributedEventForEntity(object entity) { - return DistributedEntityEventOptions - .AutoEventSelectors - .IsMatch( - ProxyHelper - .UnProxy(entity) - .GetType() - ); + var entityType = ProxyHelper.UnProxy(entity).GetType(); + return !DistributedEntityEventOptions.IgnoreEventSelectors.IsMatch(entityType) && + DistributedEntityEventOptions.AutoEventSelectors.IsMatch(entityType); } public virtual void PublishEntityUpdatedEvent(object entity) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs index c98ef478d4..04c65be897 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.Domain; +using Volo.Abp.Domain.Entities.Events.Distributed; using Volo.Abp.EntityFrameworkCore.DistributedEvents; using Volo.Abp.Modularity; using Volo.Abp.Uow.EntityFrameworkCore; @@ -28,5 +29,11 @@ public class AbpEntityFrameworkCoreModule : AbpModule context.Services.TryAddTransient(typeof(IDbContextProvider<>), typeof(UnitOfWorkDbContextProvider<>)); context.Services.AddTransient(typeof(IDbContextEventOutbox<>), typeof(DbContextEventOutbox<>)); context.Services.AddTransient(typeof(IDbContextEventInbox<>), typeof(DbContextEventInbox<>)); + + Configure(options => + { + options.IgnoreEventSelectors.Add(); + options.IgnoreEventSelectors.Add(); + }); } } diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs index 91d2808010..95733d8c77 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs @@ -4,6 +4,7 @@ using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using Volo.Abp.Domain; +using Volo.Abp.Domain.Entities.Events.Distributed; using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.Modularity; using Volo.Abp.MongoDB.DependencyInjection; @@ -48,5 +49,11 @@ public class AbpMongoDbModule : AbpModule typeof(IMongoDbContextEventInbox<>), typeof(MongoDbContextEventInbox<>) ); + + Configure(options => + { + options.IgnoreEventSelectors.Add(); + options.IgnoreEventSelectors.Add(); + }); } } From 896ac57e8dcca0870f5618fe8f4857e67ae7b43a Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 27 Mar 2025 12:11:41 +0800 Subject: [PATCH 2/4] Update index.md --- .../framework/infrastructure/event-bus/distributed/index.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/en/framework/infrastructure/event-bus/distributed/index.md b/docs/en/framework/infrastructure/event-bus/distributed/index.md index 9ed2121057..a311893d65 100644 --- a/docs/en/framework/infrastructure/event-bus/distributed/index.md +++ b/docs/en/framework/infrastructure/event-bus/distributed/index.md @@ -297,10 +297,14 @@ Configure(options => options.AutoEventSelectors.Add( type => type.Namespace.StartsWith("MyProject.") ); + + //Ignore for a single entity + options.IgnoreEventSelectors.Add(); }); ```` -* The last one provides flexibility to decide if the events should be published for the given entity type. Returns `true` to accept a `Type`. +* The `type.Namespace.StartsWith("MyProject.")` provides flexibility to decide if the events should be published for the given entity type. Returns `true` to accept a `Type`. +* The `IgnoreEventSelectors` is used to ignore the events for the specified entity types. It is useful if you enabled for all entities and want to ignore for some entities. You can add more than one selector. If one of the selectors match for an entity type, then it is selected. From 81aa3ab4a0a36ed0c79891802b0f13ce38c14e74 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 27 Mar 2025 13:51:53 +0800 Subject: [PATCH 3/4] Add test for event triggering with IgnoreEventSelectors configuration --- .../Testing/EntityChangeEvents_Tests.cs | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs index 12b3fa01c0..64724312ea 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; using Shouldly; using Volo.Abp.Domain.Entities.Events; using Volo.Abp.Domain.Entities.Events.Distributed; @@ -17,16 +18,26 @@ public abstract class EntityChangeEvents_Tests : TestAppTestBase where TStartupModule : IAbpModule { protected IRepository PersonRepository { get; } + protected ICityRepository CityRepository { get; } protected ILocalEventBus LocalEventBus { get; } protected IDistributedEventBus DistributedEventBus { get; } protected EntityChangeEvents_Tests() { PersonRepository = GetRequiredService>(); + CityRepository = GetRequiredService(); LocalEventBus = GetRequiredService(); DistributedEventBus = GetRequiredService(); } + protected override void AfterAddApplication(IServiceCollection services) + { + services.Configure(options => + { + options.IgnoreEventSelectors.Add(); + }); + } + [Fact] public async Task Complex_Event_Test() { @@ -62,7 +73,7 @@ public abstract class EntityChangeEvents_Tests : TestAppTestBase await uow.CompleteAsync(); } - + createdEventTriggered.ShouldBeTrue(); createdEtoTriggered.ShouldBeTrue(); } @@ -113,4 +124,33 @@ public abstract class EntityChangeEvents_Tests : TestAppTestBase updateEventCount.ShouldBe(1); updatedAge.ShouldBe(45); } + + [Fact] + public async Task Should_Not_Trigger_Event_If_Ignore_Event_Selector_Is_Configured() + { + var personCreatedEtoTriggered = false; + var cityCreatedEtoTriggered = false; + using (var uow = GetRequiredService().Begin()) + { + DistributedEventBus.Subscribe>(eto => + { + cityCreatedEtoTriggered = true; + return Task.CompletedTask; + }); + + DistributedEventBus.Subscribe>(eto => + { + personCreatedEtoTriggered = true; + return Task.CompletedTask; + }); + + await CityRepository.InsertAsync(new City(Guid.NewGuid(), "Istanbul")); + await PersonRepository.InsertAsync(new Person(Guid.NewGuid(), "John", 15)); + + await uow.CompleteAsync(); + } + + personCreatedEtoTriggered.ShouldBeTrue(); + cityCreatedEtoTriggered.ShouldBeFalse(); + } } From 78f972c41fe3ffae497e23499c77361ff8c33539 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 8 Apr 2025 08:20:16 +0800 Subject: [PATCH 4/4] Rename IgnoreEventSelectors to IgnoredEventSelectors. --- .../framework/infrastructure/event-bus/distributed/index.md | 4 ++-- .../Events/Distributed/AbpDistributedEntityEventOptions.cs | 4 ++-- .../Abp/Domain/Entities/Events/EntityChangeEventHelper.cs | 2 +- .../Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs | 4 ++-- .../src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs | 4 ++-- .../Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/en/framework/infrastructure/event-bus/distributed/index.md b/docs/en/framework/infrastructure/event-bus/distributed/index.md index a311893d65..d56215fa12 100644 --- a/docs/en/framework/infrastructure/event-bus/distributed/index.md +++ b/docs/en/framework/infrastructure/event-bus/distributed/index.md @@ -299,12 +299,12 @@ Configure(options => ); //Ignore for a single entity - options.IgnoreEventSelectors.Add(); + options.IgnoredEventSelectors.Add(); }); ```` * The `type.Namespace.StartsWith("MyProject.")` provides flexibility to decide if the events should be published for the given entity type. Returns `true` to accept a `Type`. -* The `IgnoreEventSelectors` is used to ignore the events for the specified entity types. It is useful if you enabled for all entities and want to ignore for some entities. +* The `IgnoredEventSelectors` is used to ignore the events for the specified entity types. It is useful if you enabled for all entities and want to ignore for some entities. You can add more than one selector. If one of the selectors match for an entity type, then it is selected. diff --git a/framework/src/Volo.Abp.Ddd.Domain.Shared/Volo/Abp/Domain/Entities/Events/Distributed/AbpDistributedEntityEventOptions.cs b/framework/src/Volo.Abp.Ddd.Domain.Shared/Volo/Abp/Domain/Entities/Events/Distributed/AbpDistributedEntityEventOptions.cs index 9168996bb5..8109d945bc 100644 --- a/framework/src/Volo.Abp.Ddd.Domain.Shared/Volo/Abp/Domain/Entities/Events/Distributed/AbpDistributedEntityEventOptions.cs +++ b/framework/src/Volo.Abp.Ddd.Domain.Shared/Volo/Abp/Domain/Entities/Events/Distributed/AbpDistributedEntityEventOptions.cs @@ -4,14 +4,14 @@ public class AbpDistributedEntityEventOptions { public IAutoEntityDistributedEventSelectorList AutoEventSelectors { get; } - public IAutoEntityDistributedEventSelectorList IgnoreEventSelectors { get; } + public IAutoEntityDistributedEventSelectorList IgnoredEventSelectors { get; } public EtoMappingDictionary EtoMappings { get; set; } public AbpDistributedEntityEventOptions() { AutoEventSelectors = new AutoEntityDistributedEventSelectorList(); - IgnoreEventSelectors = new AutoEntityDistributedEventSelectorList(); + IgnoredEventSelectors = new AutoEntityDistributedEventSelectorList(); EtoMappings = new EtoMappingDictionary(); } } diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs index 980390195a..eb16a6910c 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs @@ -69,7 +69,7 @@ public class EntityChangeEventHelper : IEntityChangeEventHelper, ITransientDepen private bool ShouldPublishDistributedEventForEntity(object entity) { var entityType = ProxyHelper.UnProxy(entity).GetType(); - return !DistributedEntityEventOptions.IgnoreEventSelectors.IsMatch(entityType) && + return !DistributedEntityEventOptions.IgnoredEventSelectors.IsMatch(entityType) && DistributedEntityEventOptions.AutoEventSelectors.IsMatch(entityType); } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs index 04c65be897..6af11ebeca 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs @@ -32,8 +32,8 @@ public class AbpEntityFrameworkCoreModule : AbpModule Configure(options => { - options.IgnoreEventSelectors.Add(); - options.IgnoreEventSelectors.Add(); + options.IgnoredEventSelectors.Add(); + options.IgnoredEventSelectors.Add(); }); } } diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs index 95733d8c77..8e8ad345fc 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs @@ -52,8 +52,8 @@ public class AbpMongoDbModule : AbpModule Configure(options => { - options.IgnoreEventSelectors.Add(); - options.IgnoreEventSelectors.Add(); + options.IgnoredEventSelectors.Add(); + options.IgnoredEventSelectors.Add(); }); } } diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs index 64724312ea..36979fdd7c 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs @@ -34,7 +34,7 @@ public abstract class EntityChangeEvents_Tests : TestAppTestBase { services.Configure(options => { - options.IgnoreEventSelectors.Add(); + options.IgnoredEventSelectors.Add(); }); }