Last active
May 8, 2019 10:07
-
-
Save ramonsmits/31a898a0986bfe46db45ae3132072195 to your computer and use it in GitHub Desktop.
NServiceBus using type attributes in unobtrusive mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: endpointConfiguration.Conventions().Apply(); | |
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface)] | |
public class MessageAttribute : Attribute | |
{ | |
} | |
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface)] | |
public sealed class EventAttribute : MessageAttribute | |
{ | |
} | |
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface)] | |
public sealed class CommandAttribute : MessageAttribute | |
{ | |
} | |
[AttributeUsage(AttributeTargets.Property)] | |
public sealed class DataBusPropertyAttribute : MessageAttribute | |
{ | |
} | |
public static class Conventions | |
{ | |
static bool IsMessage(Type t) => t != null && t.IsDefined(typeof(MessageAttribute), true); | |
static bool IsEvent(Type t) => t != null && t.IsDefined(typeof(EventAttribute), true); | |
static bool IsCommand(Type t) => t != null && t.IsDefined(typeof(CommandAttribute), true); | |
static bool IsDataBusProperty(PropertyInfo pi) => pi != null && Attribute.IsDefined(pi, typeof(DataBusPropertyAttribute)); | |
public static void Apply(this ConventionsBuilder instance) | |
{ | |
instance.DefiningEventsAs(IsEvent); | |
instance.DefiningCommandsAs(IsCommand); | |
instance.DefiningMessagesAs(IsMessage); | |
instance.DefiningDataBusPropertiesAs(IsDataBusProperty); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment