Last active
August 29, 2015 13:57
-
-
Save ztsmith/9426076 to your computer and use it in GitHub Desktop.
Castle Windsor XML Config Array Parameters not populating when registering dependencies as conventions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IConfig | |
{ | |
string Name { get; set; } | |
Server[] Servers { get; set; } | |
} | |
public class Config : IConfig | |
{ | |
public string Name { get; set; } | |
public Server[] Servers { get; set; } | |
} | |
[Convertible] | |
public class Server | |
{ | |
public string Name { get; set; } | |
public string Ip { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IUsesConfig | |
{ | |
string GetName(); | |
string GetServerIp(string name); | |
} | |
public class UsesConfig : IUsesConfig | |
{ | |
private readonly IConfig _config; | |
public UsesConfig(IConfig config) | |
{ | |
_config = config; | |
} | |
public string GetName() | |
{ | |
return _config.Name; | |
} | |
public string GetServerIp(string name) | |
{ | |
return _config.Servers.First(s => s.Name == name).Ip; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<configuration> | |
<components> | |
<component id="sample"> | |
<parameters> | |
<Name>value</Name> | |
<Servers> | |
<array> | |
<item> | |
<Name>Database</Name> | |
<IP>3.24.23.33</IP> | |
</item> | |
<item> | |
<Name>Web</Name> | |
<IP>3.22.23.23</IP> | |
</item> | |
</array> | |
</Servers> | |
</parameters> | |
</component> | |
</components> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
public class Tests | |
{ | |
[Test] | |
public void Works_when_registered_on_its_own() | |
{ | |
var container = new WindsorContainer(); | |
container | |
.Install(Configuration.FromXmlFile("sample.config")) | |
.Register(Component.For<IConfig>().ImplementedBy<Config>().Named("sample")); | |
var config = container.Resolve<IConfig>(); | |
Assert.AreEqual(config.Name, "value"); | |
Assert.AreEqual(config.Servers.Length, 2); | |
} | |
[Test] | |
public void Works_when_registered_as_a_dependency_with_conventions() | |
{ | |
var container = new WindsorContainer(); | |
container | |
.Install(Configuration.FromXmlFile("sample.config")) | |
.Register(Component.For<IConfig>().ImplementedBy<Config>().Named("sample")); | |
container.Register(Classes.FromThisAssembly().Pick().WithServiceFirstInterface()); | |
var usesConfig = container.Resolve<IUsesConfig>(); | |
Assert.AreEqual(usesConfig.GetName(), "value"); | |
Assert.AreEqual(usesConfig.GetServerIp("Database"), "3.24.23.33"); // this is where it fails i.e. config.Servers is null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment