In a video below I present my quick way (one move) for making web.config (or app.config) appSettings "type safe" in Visual Studio project.
(use gear icon in video to switch to HD resolution)
Minimal setup and usage:
- Include .tt file in a root of your Visual Studio project
- Build your project
- Access your appSettings while coding by typing: AppSettingsProxy_ Generated.Instance. ...
Advanced setup (also explained in video):
-
Include .tt file in your Visual Studio project. It doesn't really need to be in root and also you are free to rename it.
-
Open it and if necessary update line: string relativePath = @"Web.config" to point to your .config file
-
Locate Dictionary variable few lines below and for each of your appSetting specify
SettingTargetType_String,
SettingTargetType_ListOfString,
SettingTargetType_Int,
SettingTargetType_NullableInt or
SettingTargetType_ListOfInt
type. All settings that you don't put here will be generated like string type by default and accessible in code but will not be probed at startup.
-
Optionally, if you want additional types see implementation for existing ones and implement new. In essence you will need to copy/paste get-er method.
-
Right-click on .tt file in solution explorer and click Run Custom Tool to re-generate settings.
-
Access your appSettings while coding by typing: AppSettingsProxy_ Generated.Instance. ...
But what is the real-world use case for this?
Well, you find your self on a project with dozens of appSettings and you want to quickly start accessing them in a type-safe manner and you also want to be sure that some of them will not be missing one day while application/web site is running in production.
License
This product is free. It is under MIT license.
Download
Other Projects
See my
other projects.
Comments