We are primarily using the fluent registration for all of our
components, but many times I want to be able to change a components
configuration from XML. This works fine provide the configuration is
not defined via fluent otherwise the fluent configuration replaces the
xml configuration.
A very simple contrived example to demonstrate:
public class Greeter {
public string Message { get; private set; }
public Greeter(string message) { Message = message; }
public virtual void Greet(TextWriter tw, string user)
{ tw.WriteLine("{0} {1}", Message, user); }
}
[TestFixture]
public class ConfigTest
{
// This would be in a file but placing here for testing
purposes only
const string config =
@"<castle><components><component id='Greeter'>
<parameters><message>Happy Holidays!</message></
parameters>
</component></components></castle>";
[Test]
public virtual void FluentConfigurationNoConfig()
{
var container = new WindsorContainer();
container.Install(Configuration.FromXml(new
StaticContentResource(config)));
container.Register(Component.For<Greeter>().Named("Greeter"));
var greeter = container.Resolve<Greeter>();
Assert.AreEqual("Happy Holidays!", greeter.Message);
}
[Test]
public virtual void FluentConfigurationWithConfig()
{
var container = new WindsorContainer();
container.Install(Configuration.FromXml(new
StaticContentResource(config)));
container.Register(Component.For<Greeter>().Named("Greeter").DependsOn(Property.ForKey("message").Eq("Greetings!")));
var greeter = container.Resolve<Greeter>();
//Expected string length 10 but was 15. Strings differ at
index 0.
//Expected: "Happy Holidays!"
//But was: "Greetings!"
Assert.AreEqual("Happy Holidays!", greeter.Message);
}
}
If I specify the configuration in xml I have the ability to change it
later, however if I specify the configuration in code then the xml
configuration is not used. Ideally I want the reverse to happen where
the code would specify the default value to use unless the value is
overwritten via the xml configuration.
One option is register a new greeter component with a different name
in the configuration file:
<component id='Greeter-Holiday' type="Example.Greeter">
<parameters><message>Happy Holidays!</message></parameters>
</component>
Since the config is installed before the fluent configuration this
component will become the default component, but it is all or
nothing. Not a big issue here with only one property, but it can
become annoying if there are many properties to specify and really
becomes annoying when you need to need multiple instances and also
have to reconfigure other components to specify a service override.
Is there a better way to go about this?
--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.