Introducing Formo – Dynamic Configuration

In the off-chance that I have to maintain your code in the future, I feel I should gamble and share this project with you. I created it, I use it, and I like it.

Something bothers me about seeing strings inside square brackets. It has always felt dirty and error-prone. Since C# is now being sucked into the universe of dynamic, I thought that was a good opportunity to use dynamic to avoid these strings. By no means does Formo fix all these issues, but it helps in a couple ways and makes me feel better when I need to reference configuration.

Given a configuration file:

<appSettings>
    <add key="RetryAttempts" value="5" />
    <add key="ApplicationBuildDate" value="11/4/1999 6:23 AM" />
</appSettings>

Formo can be used to:

dynamic config = new Configuration();

// returns 5 as a string (see app.config file)
var retryAttempts1 = config.RetryAttempts;

// returns 5 if found in config, else 10
var retryAttempts2 = config.RetryAttempts(10);

// returns 5 if it exists in config, else userInput if not null, else 10
var retryAttempts3 = config.RetryAttempts(userInput, 10);

As well as:

dynamic config = new Configuration();

// returns value as Strongly-Typed DateTime (config value: 11/4/1999 6:23AM)
var appBuildDate = config.ApplicationBuildDate<DateTime>();

Formo is lightweight, free to use, and has no dependencies. Use it now with NuGet:

Install-Package Formo

This covers the majority of use-cases I’ve come across. If you have improvements to Formo, or just ideas in general, please submit them as issues on the GitHub issues page.

Have an idea for a logo?! I’d love a pull request!

EDIT: Great logo from Tim G. Thomashttp://dribbble.com/shots/953675-Formo

Related:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Chris Missal

Oh hey, I'm a Senior Consultant for Headspring in Austin, TX. I've been working in software professionally since 2006 and I really, really love it. I'm mostly in the Microsoft world, but enjoy building computer things of all sorts (to be vague). When I'm not slinging code, I'm probably out and about slinging discs, bowling balls, or good beer with great friends.
This entry was posted in .NET, C#, Open Source and tagged , . Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://twitter.com/dougwilson Doug Wilson

    That’s so beautiful it brought tears to my eyes!

  • Rémi BOURGAREL

    What the difference between dynamic and magic strings ?

    • Mike

       Square brackets and quotes

      • Rémi BOURGAREL

        so ? the problem with magic stirng is not the color in visual studio or the notation, its problem is that it’s evaluated at runtime instead of compile time and you got the same problem with dynamics

        • http://chrismissal.lostechies.com Chris Missal

          You’re completely correct. I wasn’t trying to solve that problem. This is *slightly* better IMO because of things like intellisense, fallback values, etc.

          • Askafif

            where is intellisense for dynamics?

  • stimul8d

    By Zeus’s beard, that’s a hot cup of tea!

  • http://realfiction.net Frank Quednau

    Oh, looks like you built DynamicAppSettings anew :) check it out, it has some additional features, too, like nesting calls or indeed leveraging .NET Type conversion capabilities to get casts to other types

    http://nuget.org/packages/dynamicappsettings

    • http://chrismissal.lostechies.com Chris Missal

      Very cool! Great minds think alike, huh? ;)

      I didn’t show it, but when Configuration is created, the ctor can take in TypeConverters - https://github.com/ChrisMissal/Formo/blob/master/src/Formo/Configuration.cs#L14

  • joshuaflanagan

    Another approach that allows you to lean on your compiler and avoid the magic strings:
    http://lostechies.com/joshuaflanagan/2009/07/13/how-we-handle-application-configuration/

    • http://chrismissal.lostechies.com Chris Missal

      I used to use a similar approach, but I didn’t like having to add properties. That’s when I started looking into this method. #hellaLazy

      I’m sure I’ll have a different preference in another two years :)

  • afifmohammed

    I have a similar implementation in configoo, except that I didn’t go the dynamics route as I didn’t see it any different to having strings. What I did add was support for unit testing and support to plug in any look up source. Also key values can be resolved using predicates.
    https://github.com/afifmohammed/configoo