Browse Source

Merge pull request #22471 from abpframework/IgnoreEventSelectors

Add `IgnoredEventSelectors` to `AbpDistributedEntityEventOptions` for event filtering.
pull/22589/head
Halil İbrahim Kalkan 10 months ago
committed by GitHub
parent
commit
7799546c45
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 6
      docs/en/framework/infrastructure/event-bus/distributed/index.md
  2. 3
      framework/src/Volo.Abp.Ddd.Domain.Shared/Volo/Abp/Domain/Entities/Events/Distributed/AbpDistributedEntityEventOptions.cs
  3. 10
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs
  4. 7
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs
  5. 7
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs
  6. 42
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs

6
docs/en/framework/infrastructure/event-bus/distributed/index.md

@ -297,10 +297,14 @@ Configure<AbpDistributedEntityEventOptions>(options =>
options.AutoEventSelectors.Add(
type => type.Namespace.StartsWith("MyProject.")
);
//Ignore for a single entity
options.IgnoredEventSelectors.Add<IgnoredProductEntity>();
});
````
* 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 `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.

3
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 IgnoredEventSelectors { get; }
public EtoMappingDictionary EtoMappings { get; set; }
public AbpDistributedEntityEventOptions()
{
AutoEventSelectors = new AutoEntityDistributedEventSelectorList();
IgnoredEventSelectors = new AutoEntityDistributedEventSelectorList();
EtoMappings = new EtoMappingDictionary();
}
}

10
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.IgnoredEventSelectors.IsMatch(entityType) &&
DistributedEntityEventOptions.AutoEventSelectors.IsMatch(entityType);
}
public virtual void PublishEntityUpdatedEvent(object entity)

7
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<AbpDistributedEntityEventOptions>(options =>
{
options.IgnoredEventSelectors.Add<OutgoingEventRecord>();
options.IgnoredEventSelectors.Add<IncomingEventRecord>();
});
}
}

7
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<AbpDistributedEntityEventOptions>(options =>
{
options.IgnoredEventSelectors.Add<OutgoingEventRecord>();
options.IgnoredEventSelectors.Add<IncomingEventRecord>();
});
}
}

42
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<TStartupModule> : TestAppTestBase
where TStartupModule : IAbpModule
{
protected IRepository<Person, Guid> PersonRepository { get; }
protected ICityRepository CityRepository { get; }
protected ILocalEventBus LocalEventBus { get; }
protected IDistributedEventBus DistributedEventBus { get; }
protected EntityChangeEvents_Tests()
{
PersonRepository = GetRequiredService<IRepository<Person, Guid>>();
CityRepository = GetRequiredService<ICityRepository>();
LocalEventBus = GetRequiredService<ILocalEventBus>();
DistributedEventBus = GetRequiredService<IDistributedEventBus>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
services.Configure<AbpDistributedEntityEventOptions>(options =>
{
options.IgnoredEventSelectors.Add<City>();
});
}
[Fact]
public async Task Complex_Event_Test()
{
@ -62,7 +73,7 @@ public abstract class EntityChangeEvents_Tests<TStartupModule> : TestAppTestBase
await uow.CompleteAsync();
}
createdEventTriggered.ShouldBeTrue();
createdEtoTriggered.ShouldBeTrue();
}
@ -113,4 +124,33 @@ public abstract class EntityChangeEvents_Tests<TStartupModule> : 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<IUnitOfWorkManager>().Begin())
{
DistributedEventBus.Subscribe<EntityCreatedEto<City>>(eto =>
{
cityCreatedEtoTriggered = true;
return Task.CompletedTask;
});
DistributedEventBus.Subscribe<EntityCreatedEto<PersonEto>>(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();
}
}

Loading…
Cancel
Save