-
Notifications
You must be signed in to change notification settings - Fork 23
Configuration Conventions
In addition to supporting an explicit configuration model, RabbitBus also provides a convention-based configuration model which allows you to define conventions for the registration of messages as well as the auto-subscription of message handlers.
Custom conventions are configured through the use of an overloaded BusBuilder.Configure()
method which accepts an IAutoConfigurationModel
. For convenience, an AutoConfigurationModelBuilder
type is supplied which provides a fluent interface for the configuration of IAutoConfigurationModel
instances.
To configure RabbitBus to automatically register message types for publication, the AutoConfigurationModelBuilder
type is used to denote the assemblies to scan along with the IPublishConfigurationConvention
instances to use. The following example scans the current executing assembly for messages which are matched by a MyPublishConfigurationConvention
type:
_bus = new BusBuilder()
.Configure(new AutoConfigurationModelBuilder()
.WithAssembly(Assembly.GetExecutingAssembly())
.WithPublishConfigurationConvention(new MyPublishConfigurationConvention())
.Build())
.Build();
The MyPublishConfigurationConvention
type in the above example might be implemented as follows:
public class TestPublishConfigurationConvention : PublishConfigurationConventionBase
{
public override bool ShouldRegister(Type type)
{
return type.Namespace.EndsWith("Messages");
}
public override string GetExchangeName(Type type)
{
return type.Name + "-exchange";
}
}
In this case, the convention registers all types within namespaces ending in "Messages" for publication and uses the name of each message type (e.g. "StatusUpdate") with a "-exchange" suffix as the name of the exchange. All other settings are taken from a provided PublishConfigurationConventionBase
type which supplies sensible defaults.
To configure RabbitBus to automatically register message types for consumption, the AutoConfigurationModelBuilder
type is used to denote the assemblies to scan along with the IConsumeConfigurationConvention
instances to use. The following example scans the current assembly for messages which are matched by a MyConsumeConfigurationConvention
type:
_bus = new BusBuilder()
.Configure(new AutoConfigurationModelBuilder()
.WithAssembly(Assembly.GetExecutingAssembly())
.WithConsumeConfigurationConvention(new MyConsumeConfigurationConvention())
.Build())
.Build();
The MyConsumeConfigurationConvention type in the above example might be implemented as follows:
public class MyConsumeConfigurationConvention : ConsumeConfigurationConventionBase
{
public override bool ShouldRegister(Type type)
{
return type.Namespace.EndsWith("Messages");
}
public override string GetExchangeName(Type type)
{
return type.Name + "-exchange";
}
public override string GetQueueName(Type type)
{
return type.Name + "-queue";
}
}
In this case, the convention registers all types within namespaces ending in "Messages" for consumption and uses the name of each message type (e.g. "StatusUpdate") with a suffix of "-exchange" and "-queue" as the name of the exchange and queue respectively. All other settings are taken from a provided ConsumeConfigurationConventionBase
type which supplies sensible defaults.
To configure RabbitBus to automatically register subscription handlers for published message types, the AutoConfigurationModelBuilder
type is used to denote the assemblies to scan along with the ISubscriptionConvention
instances to use. The following example registers handlers from the calling assembly using the RabbitBus.Configuration.MessageHandlerSubscriptionConvention
provided by RabbitBus:
_bus = new BusBuilder()
.Configure(new AutoConfigurationModelBuilder()
.WithCallingAssembly()
.WithSubscriptionConvention(new MessageHandlerSubscrptionConvention())
.Build())
.Build();
In the example above, the MessageHandlerSubscrptionConvention
type is used to automatically registers types implementing RabbitBus.IMessageHandler<TMessage>
.
The following is an example handler for a StatusUpdate
message
public class StatusUpdateHandler : IMessageHandler<StatusUpdate>
{
public void Handle(IMessageContext<StatusUpdate> messageContext)
{
// handle message
}
}