Skip to content
edpollitt edited this page Jul 27, 2015 · 2 revisions

AutoConfig can map arbitrarily complex nested types

<configuration>
  <configSections>
    <section name="complexConfiguration" 
             type="Nerdle.AutoConfig.Section, Nerdle.AutoConfig" />
  </configSections>
  <complexConfiguration foo="foo" bar="bar" baz="baz">
    <someService name="MyService" requestTimeout ="20" enabled="false" />
    <someOtherService name="MyOtherService" requestTimeout="1000" enabled="true">
      <hosts>
        <host name="HOST-01" port="1700" />
        <host name="HOST-02" port="1700" />
        <host name="HOST-03" port="1700" />
      </hosts>
    </someOtherService>
  </complexConfiguration>
</configuration>
public interface IComplexConfiguration
{
    string Foo { get; }
    string Bar { get; }
    string Baz { get; }
    IServiceConfiguration SomeService { get; }
    IExtendedServiceConfiguration SomeOtherService{ get; }
}

public interface IServiceConfiguration
{
    string Name { get; }
    int RequestTimeout { get; }
    bool Enabled { get; }
}

public interface IExtendedServiceConfiguration : IServiceConfiguration
{
    IEnumerable<IHost> Hosts { get; }
}

public interface IHost
{
    string Name { get; }
    int Port { get; }
}
[Test]
public void Mapping_nested_complex_types()
{
    var config = AutoConfig.Map<IComplexConfiguration>();

    config.Should().NotBeNull();
    config.Foo.Should().Be("foo");
    config.Bar.Should().Be("bar");
    config.Baz.Should().Be("baz");

    config.SomeService.Should().NotBeNull();
    config.SomeService.Name.Should().Be("MyService");
    config.SomeService.RequestTimeout.Should().Be(20);
    config.SomeService.Enabled.Should().BeFalse();

    config.SomeOtherService.Should().NotBeNull();
    config.SomeOtherService.Name.Should().Be("MyOtherService");
    config.SomeOtherService.RequestTimeout.Should().Be(1000);
    config.SomeOtherService.Enabled.Should().BeTrue();

    config.SomeOtherService.Hosts.Should().NotBeNull();
    config.SomeOtherService.Hosts.Should().HaveCount(3);
    config.SomeOtherService.Hosts.Select(host => host.Name)
          .Should().BeEquivalentTo("HOST-01", "HOST-02", "HOST-03");
    config.SomeOtherService.Hosts.Select(host => host.Port)
          .All(port => port == 1700).Should().BeTrue();
}

Clone this wiki locally