• Stars
    star
    259
  • Rank 157,011 (Top 4 %)
  • Language
    C#
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Formo allows you to use your configuration file as a dynamic object. Turn your web.config or application settings into a rich, dynamic object.

Formo

Formo allows you to use your configuration file as a dynamic object. Turn your web.config or application settings into a rich, dynamic object.

How to use it

Given you have a few of the following settings in your app.config file, you can new up a Configuration object and call those settings directly through a property.

The settings

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

The code

dynamic config = new Configuration();
var retryAttempts1 = config.RetryAttempts;                 // returns 5 as a string
var retryAttempts2 = config.RetryAttempts(10);             // returns 5 if found in config, else 10
var retryAttempts3 = config.RetryAttempts(userInput, 10);  // returns 5 if it exists in config, else userInput if not null, else 10

Both of the values userInput and 10 will be ignored if the value has already been set in your file.

The Configuration class also has the ability to call dynamic methods with type arguments. (I know, right?!) This lets you call your property and cast it to the type of your choice.

dynamic config = new Configuration();
var appBuildDate = config.ApplicationBuildDate<DateTime>();

Specifying Culture

If you have dates in your settings file that need to be bound to a specific culture, you can do this on creation of the Configuration class.

dynamic config = new Configuration(new CultureInfo("de"));

Property Binding

You can also use Formo to bind settings values to properties on an object:

given:

<appSettings>
    <add key="SessionTimeout" value="20" />
    <add key="WebsiteSettingsSiteTitle" value="Cat Facts" />
</appSettings>

and...

public class WebsiteSettings
{
    public int SessionTimeout { get; set; }
    public string SiteTitle { get; set; }
}

then...

dynamic config = new Configuration();
var settings = config.Bind<WebsiteSettings>();

resulting in...

settings.SessionTimeout = 20;
settings.SiteTitle = "Cat Facts";

Configuration Section

You can use Formo on Configuration Sections

<configuration>
    <configSections>
        <section name="customSection" type="System.Configuration.NameValueSectionHandler"/>
    </configSections>
    <customSection>
        <add key="ApplicationBuildDate" value="11/4/1999 6:23 AM" />
    </customSection>
    <appSettings>
    </appSettings>
</configuration>

This still works from the previous example:

dynamic config = new Configuration("customSection");
var appBuildDate = config.ApplicationBuildDate<DateTime>();

Remark the name of the section to load on the Configuration creation. So far the only suported sections are based on System.Configuration.NameValueSectionHandler.

The Property Binding feature also works on sections.

Connection Strings

You can also access connection strings by name like so:

dynamic config = new Configuration();
var devConnection = config.ConnectionString.Development;
var prodConnection = config.ConnectionString.Production;

Given that there are connection strings in the configuration that matches the following config:

<?xml version="1.0"?>
<configuration>
  <!-- stuff -->
  <connectionStrings>
    <add connectionString="<some dev connection...>" name="Development"/>
    <add connectionString="<the production connection...>" name="Production"/>
  </connectionStrings>
  <!-- more stuff -->
</configuration>

Connection Strings With Property Bindings

when using Formo to automatically bind settings values to properties on an object as described here, connection strings are automatically bound if the object contains properties of type "ConnectionStringSettings" :

given:

<appSettings>
    <add key="SessionTimeout" value="20" />
    <add key="WebsiteSettingsSiteTitle" value="Cat Facts" />
</appSettings>
  <!-- stuff -->
  <connectionStrings>
    <add connectionString="<some dev connection...>" name="Development"/>
    <add connectionString="<the production connection...>" name="Production"/>
  </connectionStrings>
  <!-- more stuff -->

and...

public class WebsiteSettings
{
    public int SessionTimeout { get; set; }
    public string SiteTitle { get; set; }

    public ConnectionStringSettings Development { get; set; }
    public ConnectionStringSettings Production { get; set; }
}

then...

dynamic config = new Configuration();
var settings = config.Bind<WebsiteSettings>();

resulting in...

settings.SessionTimeout = 20;
settings.SiteTitle = "Cat Facts";
settings.Development = "<some dev connection...>";
settings.Production = "<the production connection...>";

Access to Any Key/Value

Sometimes, you may need to access a value that contains special characters in the key and cannot be referenced like a property of function. When this is the case, you can still get the value by the string representation, like so:

dynamic config = new Configuration();
var specialValue = config.Get("Some:Value!");

Installation

To install Formo, please use NuGet (Formo NuGet Page):

PM> Install-Package Formo

Enhancements / Feedback

Use the issues link to get in touch with me about any improvements that could be made, or any bugs you encounter.

Contributors

More Repositories

1

chromelogger

chrome extension for server side console logging with .NET
C#
38
star
2

Quiche

A tasty little treat that turns your objects into query strings. (a.k.a. Objects baked into a URLs)
C#
29
star
3

TempusReader

A small .NET library for reading plain English text and converting it to a TimeSpan
C#
26
star
4

Freddie

Freddie (the MailChimp mascot) is a .NET API wrapper for MailChimp that keeps things simple. Written in C#.
C#
8
star
5

mesa

Turn a csv file into a simple, dumb table in SQL Server
F#
5
star
6

atxc

C#
3
star
7

chrismissal.github.io

HTML
2
star
8

Basher

Let's see if I can write a Dota 2 replay parser...
C#
2
star
9

gitbirthday

http://gitbirthday.com/
2
star
10

aspnetwebstack

C#
2
star
11

libgit2sharp

.NET bindings for libgit2
C#
1
star
12

DotNetKoans

A set of Koans to teach the DotNet language. Based off of EdgeCase's RubyKoans
C#
1
star
13

watdat

A Chrome extension to quickly Google an image.
JavaScript
1
star
14

hubot-resumator

Get information from the Resumator API.
CoffeeScript
1
star
15

bootstrap-slider-nuget

Automatically download and publish bootstrap-slider to NuGet
F#
1
star
16

QuickRebaser

An app that's built for quick and easy interactive rebasing in git
C#
1
star
17

linq2ga

A Linq to Google Analytics Provider. linq2ga is a fork of http://linqtogoogleanalytic.codeplex.com
C#
1
star
18

git-fundamentals

A introduction to distributed version control using Git.
1
star
19

autopin

Automatically pin all Chrome tabs
JavaScript
1
star
20

MadeUpStats

A repository for the code that runs MadeUpStats.com
JavaScript
1
star
21

MediatR.CommandLine

Create Command Line Interfaces with MediatR
1
star