Rory Becker - DevExpress CodeRush Blog

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.

Published May 19 2014, 09:46 PM by
Bookmark and Share

Comments

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.

May 19, 2014 3:26 PM

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.

May 20, 2014 2:39 AM

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

May 20, 2014 5:34 AM

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.

May 29, 2014 5:04 AM
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners