views:

698

answers:

3

I have the following component

public class MyTimer : IMyTimer {
  public MyTimer(TimeSpan timespan){...}
}

Where timespan should be provided by the property ISettings.MyTimerFrequency.

How do I wire this up in windsor container xml? I thought I could do something like this:

 <component id="settings"
    service="MySample.ISettings, MySample"
    type="MySample.Settings, MySample"
    factoryId="settings_dao" factoryCreate="GetSettingsForInstance">
  <parameters><instance_id>1</instance_id></parameters>
 </component>

 <component id="my_timer_frequency"
    type="System.TimeSpan"
    factoryId="settings" factoryCreate="MyTimerFrequency" />

 <component id="my_timer" 
    service="MySample.IMyTimer, MySample"
    type="MySample.MyTimer, MySample">
  <parameters><timespan>${my_timer_frequency}</timespan></parameters>

but I am getting an error because MyTimerFrequency is a property when the factory facility expects a method.

Is there a simple resolution here? Am I approaching the whole thing the wrong way?

EDIT: There is definitely a solution, see my answer below.

A: 

Wouldn't the simplest solution be to add a method which wraps the property?

RKitson
I've got a lot of settings and the idea of having to wrap each one is not very appealing.
George Mauer
+3  A: 

The solution actually came to me in a dream. Keep in mind that properties are not a CLR construct but rather C# syntactic sugar. If you don't believe me just try compiling

public class MyClass {
  public object Item {
    get;
  }
  public object get_Item() {return null;}
}

results in a Error: Type 'TestApp.MyClass' already reserves a member called 'get_Item' with the same parameter types

Since the Xml configuration is pursed at runtime after compilation, we can simply bind to a factoryCreate property by binding to the method that it compiles to so the above example becomes:

<component id="my_timer_frequency"
                        type="System.TimeSpan"
                        factoryId="settings" factoryCreate="get_MyTimerFrequency" />

And voila!

Someone vote this up since I can't mark it as an answer.

George Mauer
Or you could have checked the castle forums, where this was answered some time ago: http://forum.castleproject.org/viewtopic.php?t=4726
Mauricio Scheffer
Properties are actually CLR constructs. The fact the C# compiler chooses to name the getter method get_MyProperty is only an implementation detail. Another compiler could choose a different naming scheme and the property would still exist and work.Check the specification: http://msdn.microsoft.com/en-us/library/65zdfbdt%28VS.71%29.aspx
Antoine Aubry
A: 

Also treated in the castle forums

Mauricio Scheffer