Skip to content

Commit c9f410a

Browse files
Merge pull request #30 from Eventuous/event-type-attribute
Event type attribute
2 parents 9f4cb7a + 8e6e52c commit c9f410a

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

src/Eventuous/TypeMap.cs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,62 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
35
using JetBrains.Annotations;
46

57
namespace Eventuous {
68
[PublicAPI]
79
public static class TypeMap {
10+
static readonly List<Assembly> Assemblies = new();
11+
812
static readonly Dictionary<string, Type> ReverseMap = new();
913
static readonly Dictionary<Type, string> Map = new();
1014

1115
public static string GetTypeName<T>() => Map[typeof(T)];
12-
16+
1317
public static string GetTypeName(object o) => Map[o.GetType()];
14-
18+
1519
public static string GetTypeNameByType(Type type) => Map[type];
1620

1721
public static Type GetType(string typeName) => ReverseMap[typeName];
18-
19-
public static bool TryGetType(string typeName, out Type? type) => ReverseMap.TryGetValue(typeName, out type);
2022

21-
public static void AddType<T>(string name) {
22-
ReverseMap[name] = typeof(T);
23-
Map[typeof(T)] = name;
23+
public static bool TryGetType(string typeName, out Type? type) {
24+
return ReverseMap.TryGetValue(typeName, out type);
25+
}
26+
27+
public static void AddType<T>(string name) => AddType(typeof(T), name);
28+
29+
static void AddType(Type type, string name) {
30+
ReverseMap[name] = type;
31+
Map[type] = name;
2432
}
2533

2634
public static bool IsTypeRegistered<T>() => Map.ContainsKey(typeof(T));
35+
36+
public static void RegisterKnownEventTypes(params Assembly[] assemblies) {
37+
foreach (var assembly in assemblies) {
38+
RegisterAssemblyEventTypes(assembly);
39+
}
40+
}
41+
42+
static Type _attributeType = typeof(EventTypeAttribute);
43+
44+
static void RegisterAssemblyEventTypes(Assembly assembly) {
45+
var decoratedTypes = assembly.DefinedTypes.Where(
46+
x => x.IsClass && x.CustomAttributes.Any(a => a.AttributeType == _attributeType)
47+
);
48+
49+
foreach (var type in decoratedTypes) {
50+
var attr = (EventTypeAttribute)Attribute.GetCustomAttribute(type, _attributeType)!;
51+
AddType(type, attr.EventType);
52+
}
53+
}
54+
}
55+
56+
[AttributeUsage(AttributeTargets.Class)]
57+
public class EventTypeAttribute : Attribute {
58+
public string EventType { get; }
59+
60+
public EventTypeAttribute(string eventType) => EventType = eventType;
2761
}
28-
}
62+
}

test/Eventuous.Tests/Model/BookingEvents.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ public record BookingPaymentRegistered(string BookingId, string PaymentId, decim
88

99
public record BookingFullyPaid(string BookingId);
1010

11+
[EventType(BookingCancelledTypeName)]
1112
public record BookingCancelled(string BookingId);
1213

1314
public record BookingImported(string BookingId, string RoomId, LocalDate CheckIn, LocalDate CheckOut);
1415

1516
public static void MapBookingEvents() {
1617
TypeMap.AddType<RoomBooked>("RoomBooked");
1718
TypeMap.AddType<BookingPaymentRegistered>("BookingPaymentRegistered");
18-
TypeMap.AddType<BookingCancelled>("BookingCancelled");
1919
TypeMap.AddType<BookingImported>("BookingImported");
2020
}
21+
22+
public const string BookingCancelledTypeName = "V1.BookingCancelled";
2123
}
2224
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using FluentAssertions;
2+
using Xunit;
3+
using static Eventuous.Tests.Model.BookingEvents;
4+
5+
namespace Eventuous.Tests {
6+
public class TypeRegistrationTests {
7+
[Fact]
8+
public void ShouldResolveDecoratedEvent() {
9+
TypeMap.RegisterKnownEventTypes(typeof(RoomBooked).Assembly);
10+
11+
TypeMap.GetTypeName<BookingCancelled>().Should().Be(BookingCancelledTypeName);
12+
TypeMap.GetType(BookingCancelledTypeName).Should().Be<BookingCancelled>();
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)