views:

364

answers:

1

I'm trying to pass a parameter into a component that requires a System.TimeSpan. I'm only able to get the 'long ticks' ctor to resolve.

Here's a snippet of the config file:

<component id="timeInForce" type="System.TimeSpan, mscorlib">
  <parameters>
    <hours>0</hours>
    <minutes>15</minutes>
    <seconds>0</seconds>
  </parameters>
</component>

<component id="FooSettings" type="Foo.FooSettings, Foo">
    <parameters>
     <tif>${timeInForce}</tif>
    </parameters>
</component>

This is the exception:

Castle.MicroKernel.Handlers.HandlerException : Cant create component 'timeInForce'
as it has dependencies to be satisfied. 
timeInForce is waiting for the following dependencies: 

Keys (components with specific keys)
    - ticks which was not registered.

Passing a tick value for the component parameter works, as in:

<parameters><tif>0</tif></parameters>

but this defeats the purpose.

+3  A: 

What's happening (from what I can see) is that the ticks property is being incorrectly identified as a compulsory parameter (because it belongs to the constructor with the least number of arguments) even though all value types have a default parameter-less constructor.

However the constructor candidate matching the most parameters will still be selected even if you supply additional parameters (i.e. ticks) so you can work around this by just including ticks in the list of parameters:

<component id="timeInForce"" type="System.TimeSpan, mscorlib">
<parameters>
  <ticks>0</ticks>
  <hours>0</hours>
  <minutes>15</minutes>
  <seconds>0</seconds>
</parameters>
</component>

Here is a quick test to verify it works (which passes for against the castle trunk):

string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?> 
<castle>
<components>
<component id=""timeInForce"" type=""System.TimeSpan, mscorlib"">
<parameters>
  <ticks>0</ticks>
  <hours>0</hours>
  <minutes>15</minutes>
  <seconds>0</seconds>
</parameters>
</component>
</components>
</castle>";

WindsorContainer container = new WindsorContainer(
  new XmlInterpreter(new StaticContentResource(xml)));

TimeSpan span = container.Resolve<TimeSpan>("timeInForce");

Assert.AreEqual(new TimeSpan(0, 15, 0), span);

However, what I would suggest rather then the approach your using is to implement your own type converter, as discussed in the castle documentation.

That way you could develop your own shorthand form for a timespan i.e. "15m" or "2h15m" or whatever takes your fancy - making your config a little easier to read and maintain and working round the issues you're currently experiencing.

Bittercoder