Aliasing a Plugin

Hello Guys,

I currently inject plugins into my handlers and services using annotations, something like:

property name=“MailService” inject=“coldbox:myPlugin:PostBox”;

In this example, I’m injecting my postmarkapp.com mail plugin, for sending emails.

But let’s say down the line I want to switch my email provider, back to standard SMTP, or over to Amazon SES, I’d have to go through all my handlers, service etc, changing the references over to the new plugin.

Is there any way for me to alias this plugin? so I can just inject it as ‘MailService’ and have the reference to which plugin I’m using defined in a single location?

Thanks,

Robert

One option is to inject a generically named plugin like:

property name="MailService" inject="coldbox:myPlugin:myMailService";

Then your "myMailService" plugin could be whatever implementation
you're using at the moment or the generic plugin can extend from the
plugin you want to use. So you'd have the generic plugin extend from
"postbox" and when you want to change to something else, you'd have it
extend from the amazon ses plugin.

- Gabriel

Robert,

I think you could use a binder mapping to do something like this:

map(“MailService”).toDSL(“coldbox:myPlugin:PostBox”);

then, you could inject=“id:MailService” into your handlers and whatever…

If your MailService implementation ever does change then all you would need to do is map(“MailService”).toDSL(“coldbox:myPlugin:MyAwesomeMailPlugin”) in the binder and all the handlers and services should pick it up.

As far as I know this should work… unless somebody else sees issue with that approach

HTH.
-Adam

Thanks for your suggestions guys.

@Adam - where should I put this config setting? I’ve seen other discussions with similar things, but the wirebox docs seem to talk about it from a non-coldbox perspective is initing the framework yourself.

Can I add a wirebox.cfc in my config folder and have it picked up automatically?

Thanks,

Robert

Robert,

I’m new to wirebox myself, but what i did was create a config/Wirebox.cfc in my application and it worked.

Here is the examples for your case:

component extends=“coldbox.system.ioc.config.Binder” output=“false”
{
function configure(){
map(“MailService”).toDSL(“coldbox:myPlugin:PostBox”);
}
}

Then, in coldbox.cfc

wirebox = {
enabled = true,
// binder=“mapping.config.WireBox”, (“config/Wirebox.cfc” used by default)
};

Then, for your property:

property name=“MailService” inject;

HTH.
Adam

Perfect Adam, thank you!

Robert

I would do this approach that Dorioo has suggested, but I would do it is a
factory pattern.

Regards,
Andrew Scott
http://www.andyscott.id.au/

Thanks Amdrew,

That's also a nice approach to things.

Stick to aliasing at the binder level and combine it with a interface pattern. So you can replace later on an have the same method signatures for true swap capabilities.

Thanks Luis,

That's the approach I ended up taking. Thanks for your advice.

Robert