-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathAttributes.cs
48 lines (40 loc) · 1.9 KB
/
Attributes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*<FILE_LICENSE>
* Azos (A to Z Application Operating System) Framework
* The A to Z Foundation (a.k.a. Azist) licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
</FILE_LICENSE>*/
using System;
using System.Reflection;
using Azos.Platform;
namespace Azos.Sky.EventHub
{
/// <summary>
/// Decorates EventDocument-derived classes, providing event routing info: Namespace and Queue atoms
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class EventAttribute : Attribute
{
/// <summary>
/// Decorates EventDocument-derived classes, providing event routing info: Namespace and Queue atoms
/// </summary>
public EventAttribute(string ns, string queue, DataLossMode mode)
{
Namespace = Atom.Encode(ns.NonBlank(nameof(ns)));
Queue = Atom.Encode(queue.NonBlank(nameof(queue)));
LossMode = mode;
}
public readonly Atom Namespace;
public readonly Atom Queue;
public readonly DataLossMode LossMode;
/// <summary> returns (Namespace:Queue) tuple </summary>
public Route Route => new Route(Namespace, Queue);
private static readonly FiniteSetLookup<Type, EventAttribute> s_Cache = new FiniteSetLookup<Type, EventAttribute>(
t => t.GetCustomAttribute<EventAttribute>(false));
/// <summary> Retrieves EventAttribute for EventDocument-derivative or null if not decorated</summary>
public static EventAttribute TryGetFor(Type t)
=> s_Cache[t.IsOfType<EventDocument>()];
/// <summary> Retrieves EventAttribute for EventDocument-derivative or throws if not decorated</summary>
public static EventAttribute GetFor(Type t)
=> TryGetFor(t).NonNull("[{0}] on `{1}`".Args(nameof(EventAttribute), t.DisplayNameWithExpandedGenericArgs()));
}
}