New CodeRush Plugin – Convert String to AppSetting

A customer recently tweeted:

Any chance of a CodeRush plugin to convert a literal to a config (app.config) setting? pretty please

Well when the request is so polite, it’s hard to say no Smile

The Convert String to AppSetting plugin adds a new Refactoring to your standard arsenal.

Simply…

  • Place your caret on the string in question.

    ConvertStringToAppSettingBefore
  • Hit your CodeRush \ Refactor key ( Defaults to Ctrl+` )
  • Choose Convert String to AppSetting from the list of refactorings.

    ConvertStringToAppSettingMenu

CodeRush will…

  • Create a new App.config file if one doesn’t already exist.
  • Add a new setting deriving it’s name from the text of your initial string.

    ConvertStringToAppSettingAppConfig
  • Replace the original string with a call to ConfigurationManager.AppSettings

    ConvertStringToAppSettingAfter
  • Link the key-text of your new setting with that of the call, so that you can easily rename them as one.

So where can I get this wonderful plugin?

This plugin is available in 2 locations

GitHub: From here you can get the current (and historical) source of the plugin as well as the latest installable version.

The Visual Studio Gallery: Installing from the Visual Studio Gallery provides the added bonus of VS being on the lookout for any updates that are shipped on your behalf.

Free DevExpress Products – Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We’ll be happy to follow-up.
Cesar Augusto
Cesar Augusto

Very good.

It would be interesting it had an alternate that checks that the property is present or not, so we can provide a default value.

19 May 2014
Rory Becker (DevExpress)
Rory Becker (DevExpress)

An interesting idea.

If I understand you correctly, you'd be in favour of not creating a setting if you found that there was already one with the same name.

This makes sense, although I'm not certain about *automatically* using an existing setting on the basis of starting string being the same.

Consider how many times you might start with a string which was the same as one already used, but have a different purpose in mind from that of the existing setting.

20 May 2014
Cesar Augusto
Cesar Augusto

Hey Rory.

Nah, I meant something like this:

value = IsSettingPresent("mykey") ? ConfigurationManager.AppSettings["mykey"] : "SomeDefaultValue";

Just sugar.

For example, if the setting is not present in the config file, in case someone forgets to write the setting in the web/app.config file in production. Instead of an error, the system will get a default value, and just run.

Like Windows Registry and Policies work.

Also, cleaner if it'd be encapsulated:

value = GetAppSetting("mykey"); // returns the value or null

value = GetAppSetting("mykey", "defaultResult"); //returns value or the informed defaultResult parameter

20 May 2014
Rory Becker (DevExpress)
Rory Becker (DevExpress)

Ah I see now.

Yes that does make sense.

Additionally one could add an options page which would allow the user to override the code emitted with something more to their company's liking :)

This is the main reason the plugin is published as open source. Feel free to take the code and run with it. I'm totally happy to take pull requests :)

I think Encapsulation is only really possible if the user already has the function in question written and available.

In this case one could use the aforementioned configuration option to call the encapsulated function rather than calling ConfigurationManager.AppSettings directly.

29 May 2014

Please login or register to post comments.