The Bitter Coder Tutorials Binsor Style, Part X: Setter Injection

Previous posts in the series:

So, we’re 10 posts into our arguable plagerism of Alex’s tutorials, and this post’s focus is setter injection.  Setter injection is, basically, exposing a class’s dependencies via properties, as opposed to constructor parameters.  I prefer setter injection only when the number of dependencies is very high, and in most of those cases, it’s probably code smell.  However, we are just here to Binsorify the Bitter Coder Tutorials, so off we go.

We are slightly refactoring the project from Part 9, adding the ability to specify a formatter for our encoded message.  The interface is here:

public interface IMessageFormatter

{

string FormatMessage(string from, string to, string body);

}

Here’s the default formatter:

public class DefaultFormatter:IMessageFormatter

{

public string FormatMessage(string from, string to, string body)

{

return string.Format(“to: {0}\r\nfrom: {1}\r\n\r\n{2}”, to, from, body);

}

}

And the refactored version of last post’s message sender, incorporating our new dependency:

public class SecretMessageSender

{

private readonly IEncoder _encoder;

private readonly string _from;

public SecretMessageSender(IEncoder _encoder, string _from)

{

this._encoder = _encoder;

this._from = _from;

}

public void SendMessage(string to, string body)

{

Console.WriteLine(“to: {0}\r\nfrom: {1}\r\n\r\n{2}”, to, _from, _encoder.Encode(body));

}

}

Looking at the config now, we have:

component “default.formatter”, IMessageFormatter, DefaultFormatter

component “messageformat.sender”, FormatMessageSender:

_from=”ruprict@wordpress.com”

_encoder=@null.encoder

Notice that we don’t specifically set the Formatter in the config, so Windsor will do that for us.  Running the program now will yield the same results as the last post.  Keeping with Alex’s tutorial, let’s make another formatter, this one based on the NVelocity template engine.  NVelocity has changed a good bit since Alex used it in his tutorial, so the code is a bit more complex (thanks for the help on this, Alex):

public class NVelocityMessageFormatter : IMessageFormatter

{

private readonly Template _template;

private readonly VelocityEngine _velocity;

public NVelocityMessageFormatter(string _templateFile)

{

_velocity = new VelocityEngine();

var props = new ExtendedProperties();

props.SetProperty(RuntimeConstants.RESOURCE_LOADER, “file”);

props.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,

Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName));

props.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, “false”);

_velocity.Init(props);

_template = _velocity.GetTemplate(_templateFile);

}

#region IMessageFormatter Members

public string FormatMessage(string from, string to, string body)

{

var context = new VelocityContext();

context.Put(“from”, from);

context.Put(“to”, to);

context.Put(“body”, body);

context.Put(“today”, DateTime.Now);

var writer = new StringWriter();

_template.Merge(context, writer);

return writer.ToString();

}

#endregion

}

Our formatter merges the parameters (from, to, and body) with the template, which looks like:

To:   $to

From: $from

Sent: $today

———————-

$body

———————-

Finally, let’s see how we configure the program to use our fancy new formatter:

component “fancymessage.formatter”, IMessageFormatter, NVelocityMessageFormatter:

_templateFile=”message.vm”

component “messageformat.sender”, FormatMessageSender:

_from=”ruprict@wordpress.com”

_encoder=@null.encoder

Formatter = @fancymessage.formatter

We specify the template file as well as the “Formatter” parameter.  Our output now looks like:

To:   hammett
From: ruprict@wordpress.com
Sent: 8/25/2008 9:49:39 AM
———————-

Howzit going at Microsoft?

———————-
And we’re done.  We covered a decent amount of stuff in this post: setter injection, default dependencies, overriding default dependencies, as well as how to use NVelocity as a template engine.

Next time….factories.

Advertisements

About Ruprict

I am a nerd that is a Nerd Wannabe. I have more kids than should be allowed by law, a lovely wife, and a different sense of humor than most. I work in the field of GIS, where I am still trying to find myself on the map. View all posts by Ruprict

One response to “The Bitter Coder Tutorials Binsor Style, Part X: Setter Injection

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: