The Bitter Coder Tutorials, Binsor Style: Part XI, Factories

Previous posts in the series:

In this post, I will be discussing how to use Binsor to configure the factory support facility in Windsor.  A “facility” is (as Alex states) an add-in to the Windsor container that changes or adds what the container can do.  Facilities are a big part of what makes Windsor so freaking kick-ass, and other facilities are the logging facility, the transaction facility, and (the oft blogged about here) WCF facility.

So, let’s get to the code.  Here’s our interface:

public interface ISmsService

{

void SendMessage(string number, string message);

}

And our SmsService and SmsConfig classes:

public class SmsService : ISmsService

{

private SmsConfig _config;

 

public void SetConfig(SmsConfig config)

{

_config = config;

}

 

public void SendMessage(string number, string message)

{

Console.WriteLine(“SMS Message: {0} sent to {1} with account {2}”, message, number, _config.UserName);

}

}

public class SmsConfig

{

 

private string _userName;

private string _password;

private int _retryAttempts{ get; set;}

 

internal string UserName

{

get { return _userName; }

}

 

internal string Password

{

get { return _password; }

 

}

 

public void SetCredentials(string user, string pwd)

{

_userName = user;

_password = pwd;

}

}

Now, showing what is necessary to instantiate our SmsService class:

SmsService service = new SmsService();

 

SmsService.SmsConfig config = new SmsService.SmsConfig();

config.SetCredentials(“joe”, “secret”);

config.RetryAttempts = 3;

 

service.SetConfig(config);

That is not gonna work with Windsor straight away.  So, we need a factory to take care of this for us:

public class SmsServiceFactory

{

private string _userName;

private string _password;

private int _retryAttempts { get; set; }

 

public SmsServiceFactory(string userName, string password)

{

this._userName = userName;

this._password = password;

_retryAttempts = 3;

}

 

public ISmsService CreateService()

{

SmsService service = new SmsService();

SmsConfig config = new SmsConfig();

config.SetCredentials(_userName,_password);

service.SetConfig(config);

 

return service;

}

}

Right.  Now we have to get our supporting Binsor squared away.  First off, let’s register the facility.  Add the following import statement to the top of your .boo file:

import Castle.Facilities.FactorySupport from Castle.MicroKernel

So, Binsor knows where to get the facility.  Then, the facility itself:

facility FactorySupportFacility

Now we have to add our factory and our component:

component "smsservice.factory", SmsServiceFactory:
    userName="joe"
    password="secret"

component "smsservice.default", ISmsService,SmsService:
    @factoryId=@smsservice.factory
    @factoryCreate="CreateService"

Last, but not least, the program:

private static void Main(string[] args)

{

container = new WindsorContainer().Install(BinsorScript.FromFile(“windsor.boo”));

ISmsService smsService = container.Resolve<ISmsService>();

smsService.SendMessage(“+465556555”, “testing testing…1.2.3”);

Console.Read();

}

Running the console, gives us:

SMS Message: testing testing…1.2.3 sent to +465556555 with account joe

Seems we’re looking at the Decorator Pattern with Windsor next…

Reblog this post [with Zemanta]
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

2 responses to “The Bitter Coder Tutorials, Binsor Style: Part XI, Factories

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: