From ff3086d9c85d1d1e01c2778af8dbd3d5e5ba9d27 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Thu, 16 Jul 2015 05:28:28 +0200 Subject: [PATCH] Add SettingsRepository. --- Tests/Controller/SettingsRepositoryTest.cs | 60 +++++++++++++++++ Tests/Tests.csproj | 1 + zaaReloaded2/Controller/Settings.cs | 5 ++ zaaReloaded2/Controller/SettingsRepository.cs | 64 +++++++++++++++++++ zaaReloaded2/Properties/Settings.Designer.cs | 11 ++++ zaaReloaded2/Properties/Settings.settings | 14 ++-- zaaReloaded2/app.config | 24 +++++++ zaaReloaded2/zaaReloaded2.csproj | 3 + 8 files changed, 176 insertions(+), 6 deletions(-) create mode 100755 Tests/Controller/SettingsRepositoryTest.cs create mode 100755 zaaReloaded2/Controller/SettingsRepository.cs create mode 100755 zaaReloaded2/app.config diff --git a/Tests/Controller/SettingsRepositoryTest.cs b/Tests/Controller/SettingsRepositoryTest.cs new file mode 100755 index 0000000..7fdc722 --- /dev/null +++ b/Tests/Controller/SettingsRepositoryTest.cs @@ -0,0 +1,60 @@ +/* SettingsRepositoryTest.cs + * part of zaaReloaded2 + * + * Copyright 2015 Daniel Kraus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using zaaReloaded2.Controller; + +namespace Tests.Controller +{ + [TestFixture] + class SettingsRepositoryTest + { + SettingsRepository _savedSettings; + + [SetUp] + public void SetUp() + { + _savedSettings = zaaReloaded2.Properties.Settings.Default.SettingsRepository; + } + + [TearDown] + public void TearDown() + { + zaaReloaded2.Properties.Settings.Default.SettingsRepository = _savedSettings; + zaaReloaded2.Properties.Settings.Default.Save(); + } + + [Test] + public void PersistSettingsRespository() + { + SettingsRepository sr = new SettingsRepository(); + Settings s = new Settings(); + string testName = "test"; + s.Name = testName; + sr.Settings.Add(s); + sr.Store(); + sr = null; + sr = SettingsRepository.Load(); + Assert.AreEqual(1, sr.Settings.Count); + Assert.AreEqual(testName, sr.Settings[0].Name); + } + } +} diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 5944e77..482002d 100755 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -66,6 +66,7 @@ + diff --git a/zaaReloaded2/Controller/Settings.cs b/zaaReloaded2/Controller/Settings.cs index e3d947d..7986d98 100755 --- a/zaaReloaded2/Controller/Settings.cs +++ b/zaaReloaded2/Controller/Settings.cs @@ -32,6 +32,11 @@ namespace zaaReloaded2.Controller { #region Properties + /// + /// Gets or sets the name of these settings. + /// + public string Name { get; set; } + /// /// Gets or sets the reference style. /// diff --git a/zaaReloaded2/Controller/SettingsRepository.cs b/zaaReloaded2/Controller/SettingsRepository.cs new file mode 100755 index 0000000..dd21850 --- /dev/null +++ b/zaaReloaded2/Controller/SettingsRepository.cs @@ -0,0 +1,64 @@ +/* SettingsRepository.cs + * part of zaaReloaded2 + * + * Copyright 2015 Daniel Kraus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Configuration; +using System.Linq; +using System.Text; + +namespace zaaReloaded2.Controller +{ + [Serializable] + public class SettingsRepository : ApplicationSettingsBase + { + #region Persistence + + public static SettingsRepository Load() + { + return + zaaReloaded2.Properties.Settings.Default.SettingsRepository ?? + new SettingsRepository(); + } + + public void Store() + { + zaaReloaded2.Properties.Settings.Default.SettingsRepository = this; + zaaReloaded2.Properties.Settings.Default.Save(); + } + + #endregion + + #region Properties + + [UserScopedSetting()] + [SettingsSerializeAs(SettingsSerializeAs.Xml)] + public IList Settings { get; private set; } + + #endregion + + #region Constructor + + public SettingsRepository() + { + Settings = new List(); + } + + #endregion + } +} diff --git a/zaaReloaded2/Properties/Settings.Designer.cs b/zaaReloaded2/Properties/Settings.Designer.cs index 277a9a0..a97d002 100755 --- a/zaaReloaded2/Properties/Settings.Designer.cs +++ b/zaaReloaded2/Properties/Settings.Designer.cs @@ -22,5 +22,16 @@ namespace zaaReloaded2.Properties { return defaultInstance; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + public global::zaaReloaded2.Controller.SettingsRepository SettingsRepository { + get { + return ((global::zaaReloaded2.Controller.SettingsRepository)(this["SettingsRepository"])); + } + set { + this["SettingsRepository"] = value; + } + } } } diff --git a/zaaReloaded2/Properties/Settings.settings b/zaaReloaded2/Properties/Settings.settings index 3964565..130bf39 100755 --- a/zaaReloaded2/Properties/Settings.settings +++ b/zaaReloaded2/Properties/Settings.settings @@ -1,7 +1,9 @@  - - - - - - + + + + + + + + \ No newline at end of file diff --git a/zaaReloaded2/app.config b/zaaReloaded2/app.config new file mode 100755 index 0000000..40c8b50 --- /dev/null +++ b/zaaReloaded2/app.config @@ -0,0 +1,24 @@ + + + + + + + \ No newline at end of file diff --git a/zaaReloaded2/zaaReloaded2.csproj b/zaaReloaded2/zaaReloaded2.csproj index 86f2fd1..5e29f83 100755 --- a/zaaReloaded2/zaaReloaded2.csproj +++ b/zaaReloaded2/zaaReloaded2.csproj @@ -164,6 +164,7 @@ --> + @@ -203,6 +204,7 @@ True Resources.resx + SettingsSingleFileGenerator @@ -211,6 +213,7 @@ True Settings.settings + True Code