Browse Source

Introduce GenericEventNameAttribute.

pull/598/head
Halil ibrahim Kalkan 8 years ago
parent
commit
7783cd0126
  1. 6
      framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs
  2. 18
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventNameAttribute.cs
  3. 24
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/GenericEventNameAttribute.cs
  4. 9
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventNameProvider.cs
  5. 26
      framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/GenericEventNameAttribute_Tests.cs

6
framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs

@ -132,7 +132,7 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq
if (handlerFactories.Count == 1) //TODO: Multi-threading!
{
var eventName = EventNameAttribute.GetName(eventType);
var eventName = EventNameAttribute.GetNameOrDefault(eventType);
using (var channel = ConnectionPool.Get().CreateModel()) //TODO: Connection name per event!
{
@ -203,7 +203,7 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq
public override Task PublishAsync(Type eventType, object eventData)
{
var eventName = EventNameAttribute.GetName(eventType);
var eventName = EventNameAttribute.GetNameOrDefault(eventType);
var body = Serializer.Serialize(eventData);
using (var channel = ConnectionPool.Get().CreateModel()) //TODO: Connection name per event!
@ -232,7 +232,7 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq
eventType,
type =>
{
var eventName = EventNameAttribute.GetName(type);
var eventName = EventNameAttribute.GetNameOrDefault(type);
EventTypes[eventName] = type;
return new List<IEventHandlerFactory>();
}

18
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventName.cs → framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventNameAttribute.cs

@ -7,19 +7,19 @@ namespace Volo.Abp.EventBus
[AttributeUsage(AttributeTargets.Class)]
public class EventNameAttribute : Attribute, IEventNameProvider
{
public string Name { get; }
public virtual string Name { get; }
public EventNameAttribute([NotNull] string name)
{
Name = Check.NotNullOrWhiteSpace(name, nameof(name));
}
public static string GetName<TEvent>()
public static string GetNameOrDefault<TEvent>()
{
return GetName(typeof(TEvent));
return GetNameOrDefault(typeof(TEvent));
}
public static string GetName([NotNull] Type eventType)
public static string GetNameOrDefault([NotNull] Type eventType)
{
Check.NotNull(eventType, nameof(eventType));
@ -27,13 +27,13 @@ namespace Volo.Abp.EventBus
.GetCustomAttributes(true)
.OfType<IEventNameProvider>()
.FirstOrDefault()
?.Name
?.GetName(eventType)
?? eventType.FullName;
}
}
public interface IEventNameProvider
{
string Name { get; }
public string GetName(Type eventType)
{
return Name;
}
}
}

24
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/GenericEventNameAttribute.cs

@ -0,0 +1,24 @@
using System;
namespace Volo.Abp.EventBus
{
[AttributeUsage(AttributeTargets.Class)]
public class GenericEventNameAttribute : Attribute, IEventNameProvider
{
public string GetName(Type eventType)
{
if (!eventType.IsGenericType)
{
throw new AbpException($"Given type is not generic: {eventType.AssemblyQualifiedName}");
}
var genericArguments = eventType.GetGenericArguments();
if (genericArguments.Length > 1)
{
throw new AbpException($"Given type has more than one generic argument: {eventType.AssemblyQualifiedName}");
}
return EventNameAttribute.GetNameOrDefault(genericArguments[0]);
}
}
}

9
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventNameProvider.cs

@ -0,0 +1,9 @@
using System;
namespace Volo.Abp.EventBus
{
public interface IEventNameProvider
{
string GetName(Type eventType);
}
}

26
framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/GenericEventNameAttribute_Tests.cs

@ -0,0 +1,26 @@
using Shouldly;
using Xunit;
namespace Volo.Abp.EventBus
{
public class GenericEventNameAttribute_Tests
{
[Fact]
public void Should_Properly_Get_EventName()
{
new GenericEventNameAttribute()
.GetName(typeof(MyGenericType<MyInnerType>))
.ShouldBe(typeof(MyInnerType).FullName);
}
public class MyInnerType
{
}
public class MyGenericType<T>
{
}
}
}
Loading…
Cancel
Save