Mash.AppSettings
1.2.0
dotnet add package Mash.AppSettings --version 1.2.0
NuGet\Install-Package Mash.AppSettings -Version 1.2.0
<PackageReference Include="Mash.AppSettings" Version="1.2.0" />
paket add Mash.AppSettings --version 1.2.0
#r "nuget: Mash.AppSettings, 1.2.0"
// Install Mash.AppSettings as a Cake Addin #addin nuget:?package=Mash.AppSettings&version=1.2.0 // Install Mash.AppSettings as a Cake Tool #tool nuget:?package=Mash.AppSettings&version=1.2.0
Mash.AppSettings
Tired of littering your code with ConfigurationManager.AppSettings["TheSetting"] and parsing the string the type you need? Let's make loading settings easy with very little code investment.
Just create a data class which holds properties representing the settings you wish to load, and then call Load(). Mash.AppSettings uses reflection on your data class to find public properties with an attribute, finds a setting with that key in your app.config, and then sets the value on that property.
Also, now your unit tests won't have problems if they call code which directly loaded from the app.config. Instead they can set your settings class with any values they want. This makes your code a lot more cohesive, and prevents unnecessary coupling to System.Configuration.
Don't want to store settings in your app.config file? No problem! The loader uses an interface to determine where it gets its settings from so you can replace it later with another implementation. This will prevent code changes of how you load settings from impacting the rest of your code base.
Settings class
Your settings class can look as simple as this:
class MySettings : SingletonSettings<MySettings>
{
[AppSetting]
public int MyIntValue { get; set; }
[AppSetting(Key = "StringSettingOverride")]
public string OverridenSetting { get; set; }
[AppSetting(Optional = true)]
public string OptionalSetting { get; set; }
[AppSetting(SettingType = SettingType.ConnectionString)]
public string SpecificConnectionStringToLoadByKey { get; set; }
[AppSetting(SettingType = SettingType.ConnectionString)]
public IReadOnlyDictionary<string, string> ConnectionStrings { get; set; }
[AppSetting]
public IList<int> ListOfIntegers { get; set; }
}
And you can access your code like this:
var numbers = Settings.Instance.ListOfIntegers;
Or if you want to opt-in all public properties, you can just decorate the class with the AppSetting attribute.
Load your settings by hand
If you wish to load the settings by hand, the initialization could would look like:
var settings = new MySettings();
AppSettingsLoader.Load(AppSettingsFactory.GetAppConfigSettingLoader(), ref settings);
Local debug dev settings
If more than one developer is working on the code base and requires different settings for each person, it is nice to avoid having to edit the default settings source before debugging.
Each developer can have their settings preset in a file with their unique user name, and you'll rewrite your initialization code to specify this. Detecting debug flavor is a good way to do this, but you can also use the presence or value of an environment variable, existing of a file, etc.
Just set the public static property AppSettingsLoader.DevSettings to an instance of an ISettingLoader that knows what file to load.
See the DevSettings readme for more information.
Connection strings
There are two options to load connection strings:
1 A single connection string can be loaded into a named property.
2 All connection strings can be loaded into a dictionary.
Use the AppSetting "SettingType" attribute property and set it to "SettingType.ConnectionString". When loading all connection strings, the property type must be IReadOnlyDictionary<string, string>. In the dictionary, the key will hold the connection string's name, and the value will be connection string.
App.Config
Included is support for loading settings from your app.config or web.config file. See the SampleApp for a working example.
Developer support
Useful information will be traced during loading. Watch your output window for any issues encountered. Any problems loading values will be returned in an aggregate exception, unless your property is decorated as Optional.
Use the DevSettings property on the AppSettingsLoader class to override whatever settings would normally be loaded with development settings. If the DevSettings loader does not have the setting, the usual one will be loaded instead. See Mash.AppSettings.DevSettings for an implementation that reads developer-specific settings from a json file.
What's New
December 6, 2018
Converted project to target .NET Core 2.1.
September 17, 2017
Add a Json setting loader after the introduction appsettings.json in ASP.NET Core projects. Yes, you can just deserialize the json into a class, or use their built in loading, but you may not want to refactor your code.
Note that although the Json setting loader will handle numbers and integers, you should format lists just as you would for the AppConfigSettingLoader, that is as a delimited string (e.g. "1, 2, 3").
December 1, 2016
Reduced .NET version requirement to 4.5 to enable legacy clients, as the library does not require 4.61.
November 23, 2016
Added support for loading development-specific settings by setting the AppSettingsLoader.DevSettings property with a loader of your choice which will optionally supply developer-specific values.
September 29, 2016
Loading a list now handles semi-colons as well as commas. It also trims whitespace around entries.
March 8, 2016
The Optional setting can be applied to the class level and will be inherited by all members, unless otherwise specified on the property.
March 2, 2016
Fixed a null reference exception in the app config setting loader when attempting to load a connection string which does not exist.
Feb 28, 2016
You can now load comma-delimited settings into a property type that supports IList<T> where T is a public type. If you do not initialize the property with an instance, AppSettingsLoader will create a List<T> for you.
Also fixed a bug with the Optional feature released a week ago. The logic was reversed. Sorry about that!
Feb 21, 2016
In order to prevent bugs due to settings that don't exist at the source, an exception will now be thrown. If you do not wish to be notified with an exception, mark the setting as Optional (see example above).
Feb 20, 2016
Added a singleton base class to derive your settings class from. Because the base class creates an instance of your class you must specify the type, and it must have a parameterless constructor.
Initialization went from newing up your class and calling LoadSettings to one line: var settings = YourSettings.Instance.
If you wish to override the default ISettingsLoader, you may do so with the SettingLoader property, just do so before accessing the Instance property.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CSharp (>= 4.7.0)
- System.Configuration.ConfigurationManager (>= 4.7.0)
- System.Text.Json (>= 4.7.1)
-
.NETStandard 2.1
- Microsoft.CSharp (>= 4.7.0)
- System.Configuration.ConfigurationManager (>= 4.7.0)
- System.Text.Json (>= 4.7.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Mash.AppSettings:
Package | Downloads |
---|---|
Mash.AppSettings.DevSettings
Add-on for Mash.AppSettings to override with dev-specific settings |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.2.0 | 7,732 | 3/8/2020 |
1.1.0 | 2,123 | 12/17/2018 |
1.0.6522.31060 | 7,643 | 11/10/2017 |
1.0.6469.32965 | 1,580 | 9/18/2017 |
1.0.6179.29949 | 3,558 | 12/2/2016 |
1.0.6176.28344 | 1,761 | 11/28/2016 |
1.0.6172.24448 | 1,644 | 11/24/2016 |
1.0.6116.31246 | 2,396 | 9/30/2016 |
1.0.5911.39546 | 5,848 | 3/9/2016 |
1.0.5905.1661 | 1,899 | 3/2/2016 |
1.0.5902.24356 | 2,222 | 2/28/2016 |
1.0.5897.40754 | 2,132 | 2/24/2016 |
1.0.5894.36674 | 2,026 | 2/21/2016 |
1.0.5796.39370 | 2,431 | 11/15/2015 |
1.0.5796.38248 | 1,995 | 11/15/2015 |
1.0.5796.34220 | 2,073 | 11/15/2015 |
1.0.5795.34186 | 1,962 | 11/14/2015 |
1.0.5789.22683 | 2,010 | 11/7/2015 |
1.0.5783.34081 | 2,037 | 11/2/2015 |
Target net standard. Replace Newtonsoft.Json with System.Text.Json.