diff --git a/.gitignore b/.gitignore index 2bf9f9b..ea23d3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ *.suo [Bb]in/ [Oo]bj/ -TestResults/ \ No newline at end of file +TestResults/ +_ReSharper.CoderMike.Autofac.EasySettings/ +CoderMike.Autofac.EasySettings.sln.DotSettings.user diff --git a/CoderMike.Autofac.EasySettings.Tests/CoderMike.Autofac.EasySettings.Tests.csproj b/CoderMike.Autofac.EasySettings.Tests/CoderMike.Autofac.EasySettings.Tests.csproj index 2ec0147..571a35b 100644 --- a/CoderMike.Autofac.EasySettings.Tests/CoderMike.Autofac.EasySettings.Tests.csproj +++ b/CoderMike.Autofac.EasySettings.Tests/CoderMike.Autofac.EasySettings.Tests.csproj @@ -1,79 +1,80 @@ - - - - Debug - AnyCPU - - - 2.0 - {F8E8B9A5-B69F-4754-85F7-19192DA90712} - Library - Properties - CoderMike.Autofac.EasySettings.Tests - CoderMike.Autofac.EasySettings.Tests - v4.0 - 512 - {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\packages\Autofac.2.5.2.830\lib\NET40\Autofac.dll - - - ..\packages\Autofac.2.5.2.830\lib\NET40\Autofac.Configuration.dll - - - - - - 3.5 - - - - - False - - - - - - - - - - - - {DB3BD92D-8EEF-4FE5-AA18-2FD52303DC18} - CoderMike.Autofac.EasySettings - - - - - - - + + + + Debug + AnyCPU + + + 2.0 + {F8E8B9A5-B69F-4754-85F7-19192DA90712} + Library + Properties + CoderMike.Autofac.EasySettings.Tests + CoderMike.Autofac.EasySettings.Tests + v4.0 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Autofac.2.5.2.830\lib\NET40\Autofac.dll + + + ..\packages\Autofac.2.5.2.830\lib\NET40\Autofac.Configuration.dll + + + + + + 3.5 + + + + + False + + + + + + + + + + + + + {DB3BD92D-8EEF-4FE5-AA18-2FD52303DC18} + CoderMike.Autofac.EasySettings + + + + + + + + --> \ No newline at end of file diff --git a/CoderMike.Autofac.EasySettings.Tests/Scenarios.cs b/CoderMike.Autofac.EasySettings.Tests/Scenarios.cs index 6c7ebc9..999e09e 100644 --- a/CoderMike.Autofac.EasySettings.Tests/Scenarios.cs +++ b/CoderMike.Autofac.EasySettings.Tests/Scenarios.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Collections.Specialized; using System.Text; using System.Collections.Generic; using System.Linq; @@ -31,6 +32,23 @@ public void TestMethod1() Assert.AreEqual("SomeKey", component.AkismetSettings.ApiKey); Assert.IsTrue(component.BlogSettings.EnableComments); Assert.IsTrue(component.BlogSettings.EnableHistory); + } + + [TestMethod] + public void MultipleSettingsReaders() + { + var builder = new ContainerBuilder(); + builder.RegisterModule(new EasySettingsModule(ConfigurationManager.AppSettings)); + //extra settings provider + var secretItems = new NameValueCollection {{"Blog:SuperSecretItem", "SuperSecretValue"}}; + builder.RegisterInstance(new SimpleSettingsReader(secretItems)).As(); + + builder.RegisterType(); + + var container = builder.Build(); + var component = container.Resolve(); + + Assert.AreEqual("SuperSecretValue", component.BlogSettings.SuperSecretItem); } class FakeComponent @@ -61,8 +79,9 @@ class AkismetSettings class BlogSettings { - public bool EnableComments { get; private set; } - public bool EnableHistory { get; private set; } + public bool EnableComments { get; private set; } + public bool EnableHistory { get; private set; } + public string SuperSecretItem { get; private set; } } } } diff --git a/CoderMike.Autofac.EasySettings.Tests/SettingsSourceFixture.cs b/CoderMike.Autofac.EasySettings.Tests/SettingsSourceFixture.cs index 8735bce..93d192e 100644 --- a/CoderMike.Autofac.EasySettings.Tests/SettingsSourceFixture.cs +++ b/CoderMike.Autofac.EasySettings.Tests/SettingsSourceFixture.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Collections.Specialized; using System.Text; using System.Collections.Generic; using System.Linq; @@ -13,18 +14,23 @@ public class SettingsSourceFixture [TestMethod] public void SettingsSourceAllowsResolvingOfSettingClasses() { - var builder = new ContainerBuilder(); - var reader = new FakeReader(); - builder.RegisterInstance(reader).As(); + var builder = new ContainerBuilder(); + var reader = new FakeReader(); + var injector = new FakeInjector(); + builder.RegisterInstance(reader).As(); + builder.RegisterInstance(injector).As(); builder.RegisterSource(new SettingsSource()); var container = builder.Build(); - var fakeSettings = new FakeSettings(); - reader.Settings = fakeSettings; + var fakeSettings = new NameValueCollection(); + reader.Collection = fakeSettings; var resolvedSettings = container.Resolve(); - Assert.AreSame(fakeSettings, resolvedSettings); - Assert.IsTrue(reader.WasCalled); + + Assert.IsNotNull(resolvedSettings); + Assert.IsTrue(injector.WasCalled); + Assert.IsNotNull(injector.Settings, "Settings sources were not passed to injector"); + Assert.IsTrue(injector.Settings.Contains(reader)); } class FakeSettings @@ -34,15 +40,28 @@ class FakeSettings class FakeReader : ISettingsReader - { - public object Settings { get; set; } + { + public NameValueCollection Collection { get; set; } public bool WasCalled { get; set; } - - public object Read(Type settingsType) - { - WasCalled = true; - return Settings; - } - } + + public NameValueCollection Read() + { + WasCalled = true; + return Collection; + } + } + + private class FakeInjector : ISettingsInjector + { + public bool WasCalled { get; set; } + public IEnumerable Settings { get; set; } + + public void Inject(object o, IEnumerable settings) + { + WasCalled = true; + Settings = settings; + } + + } } } diff --git a/CoderMike.Autofac.EasySettings.Tests/SimpleSettingsInjectorFixture.cs b/CoderMike.Autofac.EasySettings.Tests/SimpleSettingsInjectorFixture.cs new file mode 100644 index 0000000..b49e9fe --- /dev/null +++ b/CoderMike.Autofac.EasySettings.Tests/SimpleSettingsInjectorFixture.cs @@ -0,0 +1,144 @@ +using System; +using System.Collections.Specialized; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace CoderMike.Autofac.EasySettings.Tests +{ + [TestClass] + public class SimpleSettingsInjectorFixture + { + [TestMethod] + public void CanReadAStringValue() + { + var settingsSource = new NameValueCollection(); + settingsSource["My:Name"] = "Mike"; + var reader = new SimpleSettingsReader(settingsSource); + var injector = new SimpleSettingsInjector(); + var settings = new MySettings(); + injector.Inject(settings, reader); + Assert.AreEqual("Mike", settings.Name); + } + + // TODO: Does this need a custom exception class? + [TestMethod, ExpectedException(typeof(Exception))] + public void FailsIfSettingsHaveNoCorrespondingProperty() + { + SimpleSettingsReader reader = null; + SimpleSettingsInjector injector = null; + try + { + var settingsSource = new NameValueCollection(); + settingsSource["My:Name"] = "Mike"; + settingsSource["My:Blog"] = "http://codermike.com"; + reader = new SimpleSettingsReader(settingsSource); + injector = new SimpleSettingsInjector(); + } + catch (Exception ex) + { + Assert.Inconclusive("Unexpected Exception occurred: {0}", ex); + } + var settings = new MySettings(); + injector.Inject(settings, reader); + Assert.Fail("Should have thrown"); + } + + [TestMethod] + public void CanReadTypesOtherThanString() + { + var settingsSource = new NameValueCollection(); + settingsSource["Smtp:Server"] = "fake-server"; + settingsSource["Smtp:Port"] = "12345"; + settingsSource["Smtp:UseSSL"] = "True"; + var injector = new SimpleSettingsInjector(); + var reader = new SimpleSettingsReader(settingsSource); + var settings = new SmtpSettings(); + injector.Inject(settings, reader); + + + Assert.AreEqual("fake-server", settings.Server); + Assert.AreEqual(12345, settings.Port); + Assert.IsTrue(settings.UseSSL); + } + + [TestMethod, ExpectedException(typeof(FormatException))] + public void UnconvertableSettingResultsInFormatException() + { + var settingsSource = new NameValueCollection(); + settingsSource["Smtp:Port"] = "abcdef"; + var reader = new SimpleSettingsReader(settingsSource); + var injector = new SimpleSettingsInjector(); + var settings = new SmtpSettings(); + + injector.Inject(settings, reader); + Assert.Fail("Should have thrown"); + } + + [TestMethod] + public void CanReadManySettingsFromASingleSource() + { + var settingsSource = new NameValueCollection(); + settingsSource["Smtp:Server"] = "test-server"; + settingsSource["My:Name"] = "Mike"; + var reader = new SimpleSettingsReader(settingsSource); + var injector = new SimpleSettingsInjector(); + var smtpSettings = new SmtpSettings(); + var mySettings = new MySettings(); + + injector.Inject(smtpSettings, reader); + injector.Inject(mySettings, reader); + + Assert.AreEqual("test-server", smtpSettings.Server); + Assert.AreEqual("Mike", mySettings.Name); + } + + [TestMethod] + public void CanSetPrivateProperties() + { + var settingsSource = new NameValueCollection(); + settingsSource["Foo:Bar"] = "Baz"; + var reader = new SimpleSettingsReader(settingsSource); + var injector = new SimpleSettingsInjector(); + var settings = new FooSettings(); + + injector.Inject(settings, reader); + Assert.AreEqual("Baz", settings.Bar); + } + + [TestMethod, ExpectedException(typeof(ArgumentException))] + public void AttemptToSetTheUnsettableResultsInAnException() + { + var settingsSource = new NameValueCollection(); + settingsSource["Test:Unsettable"] = "Bang"; + var reader = new SimpleSettingsReader(settingsSource); + var injector = new SimpleSettingsInjector(); + var settings = new TestSettings(); + + injector.Inject(settings, reader); + } + + class MySettings + { + public string Name { get; set; } + } + + class SmtpSettings + { + public string Server { get; set; } + public int Port { get; set; } + public bool UseSSL { get; set; } + } + + class FooSettings + { + public string Bar { get; private set; } + } + + class TestSettings + { + public string Unsettable + { + get { return "Calculated Value"; } + } + } + } +} \ No newline at end of file diff --git a/CoderMike.Autofac.EasySettings.Tests/SimpleSettingsReaderFixture.cs b/CoderMike.Autofac.EasySettings.Tests/SimpleSettingsReaderFixture.cs index 5a89b98..0dcb51c 100644 --- a/CoderMike.Autofac.EasySettings.Tests/SimpleSettingsReaderFixture.cs +++ b/CoderMike.Autofac.EasySettings.Tests/SimpleSettingsReaderFixture.cs @@ -15,141 +15,16 @@ public void RequiresASettingsSource() { var reader = new SimpleSettingsReader(null); } - - [TestMethod] - public void CannotGetNullSettings() - { - var exceptionWasThrown = false; - var reader = new SimpleSettingsReader(new NameValueCollection()); - try - { - reader.Read(null); - } - catch (ArgumentNullException) - { - exceptionWasThrown = true; - } - Assert.IsTrue(exceptionWasThrown, "Call should have thrown an exception"); - } - + [TestMethod] - public void ShouldGetAnObjectEvenIfNoSettingsAreFound() + public void ShouldGetAnCollectionEvenIfNoSettingsAreFound() { var reader = new SimpleSettingsReader(new NameValueCollection()); - object settings = reader.Read(typeof(MySettings)); + object settings = reader.Read(); Assert.IsNotNull(settings, "Didn't get a settings object at all"); - Assert.IsNotNull(settings as MySettings, "Didn't get a settings object of the correct type"); - } - - [TestMethod] - public void CanReadAStringValue() - { - var settingsSource = new NameValueCollection(); - settingsSource["My:Name"] = "Mike"; - var reader = new SimpleSettingsReader(settingsSource); - var settings = reader.Read(); - Assert.AreEqual("Mike", settings.Name); - } - - // TODO: Does this need a custom exception class? - [TestMethod, ExpectedException(typeof(Exception))] - public void FailsIfSettingsHaveNoCorrespondingProperty() - { - SimpleSettingsReader reader = null; - try - { - var settingsSource = new NameValueCollection(); - settingsSource["My:Name"] = "Mike"; - settingsSource["My:Blog"] = "http://codermike.com"; - reader = new SimpleSettingsReader(settingsSource); - } - catch (Exception ex) - { - Assert.Inconclusive("Unexpected Exception occurred: {0}", ex); - } - var settings = reader.Read(); - Assert.Fail("Should have thrown"); - } - - [TestMethod] - public void CanReadTypesOtherThanString() - { - var settingsSource = new NameValueCollection(); - settingsSource["Smtp:Server"] = "fake-server"; - settingsSource["Smtp:Port"] = "12345"; - settingsSource["Smtp:UseSSL"] = "True"; - var reader = new SimpleSettingsReader(settingsSource); - var settings = reader.Read(); - Assert.AreEqual("fake-server", settings.Server); - Assert.AreEqual(12345, settings.Port); - Assert.IsTrue(settings.UseSSL); - } - - [TestMethod, ExpectedException(typeof(FormatException))] - public void UnconvertableSettingResultsInFormatException() - { - var settingsSource = new NameValueCollection(); - settingsSource["Smtp:Port"] = "abcdef"; - var reader = new SimpleSettingsReader(settingsSource); - var settings = reader.Read(); - Assert.Fail("Should have thrown"); - } - - [TestMethod] - public void CanReadManySettingsFromASingleSource() - { - var settingsSource = new NameValueCollection(); - settingsSource["Smtp:Server"] = "test-server"; - settingsSource["My:Name"] = "Mike"; - var reader = new SimpleSettingsReader(settingsSource); - var smtpSettings = reader.Read(); - var mySettings = reader.Read(); - Assert.AreEqual("test-server", smtpSettings.Server); - Assert.AreEqual("Mike", mySettings.Name); - } - - [TestMethod] - public void CanSetPrivateProperties() - { - var settingsSource = new NameValueCollection(); - settingsSource["Foo:Bar"] = "Baz"; - var reader = new SimpleSettingsReader(settingsSource); - var settings = reader.Read(); - Assert.AreEqual("Baz", settings.Bar); - } - - [TestMethod, ExpectedException(typeof(ArgumentException))] - public void AttemptToSetTheUnsettableResultsInAnException() - { - var settingsSource = new NameValueCollection(); - settingsSource["Test:Unsettable"] = "Bang"; - var reader = new SimpleSettingsReader(settingsSource); - var settings = reader.Read(); - } - - class MySettings - { - public string Name { get; set; } - } - - class SmtpSettings - { - public string Server { get; set; } - public int Port { get; set; } - public bool UseSSL { get; set; } - } - - class FooSettings - { - public string Bar { get; private set; } - } - - class TestSettings - { - public string Unsettable - { - get { return "Calculated Value"; } - } + Assert.IsNotNull(settings as NameValueCollection, "Didn't get a settings object of the correct type"); } + + } } diff --git a/CoderMike.Autofac.EasySettings.Tests/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/CoderMike.Autofac.EasySettings.Tests/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache index e636c91..6dd48dc 100644 Binary files a/CoderMike.Autofac.EasySettings.Tests/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache and b/CoderMike.Autofac.EasySettings.Tests/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/CoderMike.Autofac.EasySettings/CoderMike.Autofac.EasySettings.csproj b/CoderMike.Autofac.EasySettings/CoderMike.Autofac.EasySettings.csproj index 8493246..cf3b972 100644 --- a/CoderMike.Autofac.EasySettings/CoderMike.Autofac.EasySettings.csproj +++ b/CoderMike.Autofac.EasySettings/CoderMike.Autofac.EasySettings.csproj @@ -1,67 +1,69 @@ - - - - Debug - AnyCPU - 8.0.30703 - 2.0 - {DB3BD92D-8EEF-4FE5-AA18-2FD52303DC18} - Library - Properties - CoderMike.Autofac.EasySettings - CoderMike.Autofac.EasySettings - v4.0 - 512 - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\packages\Autofac.2.5.2.830\lib\NET40\Autofac.dll - - - ..\packages\Autofac.2.5.2.830\lib\NET40\Autofac.Configuration.dll - - - - - - - - - - - - - - - - - - - - - + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {DB3BD92D-8EEF-4FE5-AA18-2FD52303DC18} + Library + Properties + CoderMike.Autofac.EasySettings + CoderMike.Autofac.EasySettings + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Autofac.2.5.2.830\lib\NET40\Autofac.dll + + + ..\packages\Autofac.2.5.2.830\lib\NET40\Autofac.Configuration.dll + + + + + + + + + + + + + + + + + + + + + + + + --> \ No newline at end of file diff --git a/CoderMike.Autofac.EasySettings/EasySettingsModule.cs b/CoderMike.Autofac.EasySettings/EasySettingsModule.cs index b4d0482..0885f61 100644 --- a/CoderMike.Autofac.EasySettings/EasySettingsModule.cs +++ b/CoderMike.Autofac.EasySettings/EasySettingsModule.cs @@ -11,17 +11,29 @@ public class EasySettingsModule : Module { private readonly NameValueCollection _settings; + public EasySettingsModule() + { + + } + public EasySettingsModule(NameValueCollection settings) { _settings = settings; } - protected override void Load(ContainerBuilder builder) - { - builder.RegisterType() - .As() - .WithParameter(TypedParameter.From(_settings)); - + protected override void Load(ContainerBuilder builder) + { + builder.RegisterType() + .As() + .SingleInstance(); + + if (_settings != null) + { + builder.RegisterType() + .As() + .WithParameter(TypedParameter.From(_settings)); + } + builder.RegisterSource(new SettingsSource()); } } diff --git a/CoderMike.Autofac.EasySettings/ISettingsInjector.cs b/CoderMike.Autofac.EasySettings/ISettingsInjector.cs new file mode 100644 index 0000000..b33d22c --- /dev/null +++ b/CoderMike.Autofac.EasySettings/ISettingsInjector.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace CoderMike.Autofac.EasySettings +{ + public interface ISettingsInjector + { + void Inject(object o, IEnumerable settings); + } +} \ No newline at end of file diff --git a/CoderMike.Autofac.EasySettings/ISettingsReader.cs b/CoderMike.Autofac.EasySettings/ISettingsReader.cs index 226acb2..473bb3c 100644 --- a/CoderMike.Autofac.EasySettings/ISettingsReader.cs +++ b/CoderMike.Autofac.EasySettings/ISettingsReader.cs @@ -1,12 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace CoderMike.Autofac.EasySettings -{ - public interface ISettingsReader - { - object Read(Type settingsType); - } -} +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Linq; +using System.Text; + +namespace CoderMike.Autofac.EasySettings +{ + public interface ISettingsReader + { + NameValueCollection Read(); + } +} diff --git a/CoderMike.Autofac.EasySettings/SettingsInjectorExtensions.cs b/CoderMike.Autofac.EasySettings/SettingsInjectorExtensions.cs new file mode 100644 index 0000000..33dce1a --- /dev/null +++ b/CoderMike.Autofac.EasySettings/SettingsInjectorExtensions.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CoderMike.Autofac.EasySettings +{ + public static class SettingsInjectorExtensions + { + public static void Inject(this ISettingsInjector injector, object instance, params ISettingsReader[] args) + { + injector.Inject(instance, args); + } + } +} diff --git a/CoderMike.Autofac.EasySettings/SettingsReaderExtensions.cs b/CoderMike.Autofac.EasySettings/SettingsReaderExtensions.cs deleted file mode 100644 index 062feb5..0000000 --- a/CoderMike.Autofac.EasySettings/SettingsReaderExtensions.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace CoderMike.Autofac.EasySettings -{ - public static class SettingsReaderExtensions - { - public static T Read(this ISettingsReader reader) - { - return (T)reader.Read(typeof(T)); - } - } -} diff --git a/CoderMike.Autofac.EasySettings/SettingsSource.cs b/CoderMike.Autofac.EasySettings/SettingsSource.cs index 8e00b0d..9d25917 100644 --- a/CoderMike.Autofac.EasySettings/SettingsSource.cs +++ b/CoderMike.Autofac.EasySettings/SettingsSource.cs @@ -16,7 +16,12 @@ public IEnumerable RegistrationsFor(Service service, Fun if (typedService != null && typedService.ServiceType.IsClass && typedService.ServiceType.Name.EndsWith("Settings")) { yield return RegistrationBuilder.ForDelegate( - (c, p) => c.Resolve().Read(typedService.ServiceType) + (c, p) => + { + var instance = Activator.CreateInstance(typedService.ServiceType); + c.Resolve().Inject(instance, c.Resolve>()); + return instance; + } ).As(typedService.ServiceType) .CreateRegistration(); } diff --git a/CoderMike.Autofac.EasySettings/SimpleSettingsInjector.cs b/CoderMike.Autofac.EasySettings/SimpleSettingsInjector.cs new file mode 100644 index 0000000..6d4aaed --- /dev/null +++ b/CoderMike.Autofac.EasySettings/SimpleSettingsInjector.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace CoderMike.Autofac.EasySettings +{ + public class SimpleSettingsInjector : ISettingsInjector + { + public void Inject(object settingsObj, IEnumerable settings) + { + if (settingsObj == null) + throw new ArgumentNullException("instance"); + + var settingsType = settingsObj.GetType(); + var settingsPrefix = settingsType.Name.Replace("Settings", "") + ":"; + + foreach (var settingsReader in settings) + { + var settingsSource = settingsReader.Read(); + if (settingsSource != null) + { + foreach (var key in settingsSource.AllKeys.Where(x => x.StartsWith(settingsPrefix))) + { + var propertyName = key.Substring(settingsPrefix.Length); + var property = settingsType.GetProperty(propertyName); + if (property == null) + throw new Exception(String.Format("Settings class {0} has no property called {1}", settingsType.Name, propertyName)); + + var settingValue = Convert.ChangeType(settingsSource[key], property.PropertyType); + property.SetValue(settingsObj, settingValue, null); + } + } + } + } + } +} \ No newline at end of file diff --git a/CoderMike.Autofac.EasySettings/SimpleSettingsReader.cs b/CoderMike.Autofac.EasySettings/SimpleSettingsReader.cs index 730466a..efe01df 100644 --- a/CoderMike.Autofac.EasySettings/SimpleSettingsReader.cs +++ b/CoderMike.Autofac.EasySettings/SimpleSettingsReader.cs @@ -17,23 +17,11 @@ public SimpleSettingsReader(NameValueCollection settingsSource) _settingsSource = settingsSource; } - public object Read(Type settingsType) - { - if (settingsType == null) - throw new ArgumentNullException("settingsType"); - var settingsObj = Activator.CreateInstance(settingsType); - var settingsPrefix = settingsType.Name.Replace("Settings", "") + ":"; - foreach (var key in _settingsSource.AllKeys.Where(x => x.StartsWith(settingsPrefix))) - { - var propertyName = key.Substring(settingsPrefix.Length); - var property = settingsType.GetProperty(propertyName); - if (property == null) - throw new Exception(String.Format("Settings class {0} has no property called {1}", settingsType.Name, propertyName)); - - var settingValue = Convert.ChangeType(_settingsSource[key], property.PropertyType); - property.SetValue(settingsObj, settingValue, null); - } - return settingsObj; - } + + + public NameValueCollection Read() + { + return _settingsSource; + } } } diff --git a/CoderMike.Autofac.EasySettings/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/CoderMike.Autofac.EasySettings/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache index 3ec5698..b2b016c 100644 Binary files a/CoderMike.Autofac.EasySettings/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache and b/CoderMike.Autofac.EasySettings/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ