Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Last active May 8, 2019 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramonsmits/31a898a0986bfe46db45ae3132072195 to your computer and use it in GitHub Desktop.
Save ramonsmits/31a898a0986bfe46db45ae3132072195 to your computer and use it in GitHub Desktop.
NServiceBus using type attributes in unobtrusive mode
// 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