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:

  1. Include .tt file in a root of your Visual Studio project
  2. Build your project
  3. Access your appSettings while coding by typing: AppSettingsProxy_ Generated.Instance. ...

Advanced setup (also explained in video):

  1. 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.
  2. Open it and if necessary update line: string relativePath = @"Web.config" to point to your .config file
  3. 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.
  4. 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.
  5. Right-click on .tt file in solution explorer and click Run Custom Tool to re-generate settings.
  6. 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

Name
E-Mail@
(optional; never shown publicly; for notifications from this thread)
Comment
Anti Bot Image