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]](http://img.zemanta.com/reblog_e.png?x-id=58ac3891-3867-4c08-8335-4d38492ab773)