mirror of https://github.com/abpframework/abp.git
9 changed files with 203 additions and 39 deletions
@ -0,0 +1,32 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities.Events.Distributed; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace Volo.Abp.EventBus.Distributed |
|||
{ |
|||
public class MySimpleDistributedSingleInstanceEventHandler : IDistributedEventHandler<MySimpleEventData>, IDistributedEventHandler<EntityCreatedEto<MySimpleEventData>>, ITransientDependency |
|||
{ |
|||
private readonly ICurrentTenant _currentTenant; |
|||
|
|||
public MySimpleDistributedSingleInstanceEventHandler(ICurrentTenant currentTenant) |
|||
{ |
|||
_currentTenant = currentTenant; |
|||
} |
|||
|
|||
public static Guid? TenantId { get; set; } |
|||
|
|||
public Task HandleEventAsync(MySimpleEventData eventData) |
|||
{ |
|||
TenantId = _currentTenant.Id; |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public Task HandleEventAsync(EntityCreatedEto<MySimpleEventData> eventData) |
|||
{ |
|||
TenantId = _currentTenant.Id; |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,64 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.Domain.Entities.Events; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.EventBus.Local |
|||
{ |
|||
public class EventBus_MultiTenancy_Test : EventBusTestBase |
|||
{ |
|||
[Fact] |
|||
public async Task Should_Change_TenantId_If_EventData_Is_MultiTenant() |
|||
{ |
|||
var tenantId = Guid.NewGuid(); |
|||
var handler = new MyEventHandler(GetRequiredService<ICurrentTenant>()); |
|||
|
|||
LocalEventBus.Subscribe<EntityChangedEventData<MyEntity>>(handler); |
|||
|
|||
await LocalEventBus.PublishAsync(new EntityCreatedEventData<MyEntity>(new MyEntity(tenantId))); |
|||
|
|||
handler.TenantId.ShouldBe(tenantId); |
|||
} |
|||
|
|||
public class MyEntity : Entity, IMultiTenant |
|||
{ |
|||
public override object[] GetKeys() |
|||
{ |
|||
return new object[0]; |
|||
} |
|||
|
|||
public MyEntity() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public MyEntity(Guid? tenantId) |
|||
{ |
|||
TenantId = tenantId; |
|||
} |
|||
|
|||
public Guid? TenantId { get; } |
|||
} |
|||
|
|||
public class MyEventHandler : ILocalEventHandler<EntityChangedEventData<MyEntity>> |
|||
{ |
|||
private readonly ICurrentTenant _currentTenant; |
|||
|
|||
public MyEventHandler(ICurrentTenant currentTenant) |
|||
{ |
|||
_currentTenant = currentTenant; |
|||
} |
|||
|
|||
public Guid? TenantId { get; set; } |
|||
|
|||
public Task HandleEventAsync(EntityChangedEventData<MyEntity> eventData) |
|||
{ |
|||
TenantId = _currentTenant.Id; |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,12 +1,18 @@ |
|||
using System; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace Volo.Abp.EventBus |
|||
{ |
|||
public class MySimpleEventData |
|||
public class MySimpleEventData : IMultiTenant |
|||
{ |
|||
public int Value { get; set; } |
|||
|
|||
public MySimpleEventData(int value) |
|||
public Guid? TenantId { get; } |
|||
|
|||
public MySimpleEventData(int value, Guid? tenantId = null) |
|||
{ |
|||
Value = value; |
|||
TenantId = tenantId; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue