Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Replacing EasyNetQ Components

DZone's Guide to

Replacing EasyNetQ Components

· Integration Zone
Free Resource

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

EasyNetQ, my simple .NET API for RabbitMQ, is a library composed of small components. Until today, the code simply wired the components in a messy hard-coded routine. Now it has its own tiny internal IoC container.  When you write:

var bus = RabbitHutch.CreateBus("host=localhost");  

... the static method CreateBus registers the components with the container and then resolves the IBus instance. The really cool thing about this is that it allows you, the user, to replace any of the internal components, including IBus, with your own implementations. An overload of the CreateBus method provides the hook which gives you access to the component registration. The signature looks like this:

public static IBus CreateBus(string connectionString, Action<IServiceRegister> registerServices)  

The IServiceRegister interface provides a single method:

public interface IServiceRegister
{
    IServiceRegister Register<TService>(Func<IServiceProvider, TService> serviceCreator) where TService : class;
} 

So to register your own logger, based on IEasyNetQLogger, you'd write this code:

var logger = new MyLogger(); // MyLogger implements IEasyNetQLogger
var bus = RabbitHutch.CreateBus(connectionString, 
serviceRegister => serviceRegister.Register(serviceProvider => logger)); 

The Register method's argument, Func<IServiceProvider, TService>, is a function that's run when CreateBus pulls together the components to make an IBus instance. IServiceProvider looks like this:

public interface IServiceProvider
{
    TService Resolve<TService>() where TService : class;
} 

This allows you to access other services that EasyNetQ provides. If for example you wanted to replace the default serializer with your own implementation of ISerializer, and you wanted to construct it with a reference to the internal logger, you could do this:

var bus = RabbitHutch.CreateBus(connectionString, serviceRegister => serviceRegister.Register(
serviceProvider => new MySerializer(serviceProvider.Resolve<IEasyNetQLogger>()))); 

There’s nothing to stop you registering your own interfaces with the container that you can then use with your implementations of EasyNetQ’s service interfaces.

To see the complete list of components that make up the IBus instance, and how they are assembled, take a look at the ComponentRegistration class.

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}