Project Description

Strongly typed configuration provider for .net applications.

Goal

- Remove hard-coded literals, while working with configuration settings for your project
- Remove hand-made conversion from string to meaningful type
- Provide centralized behavior for non-existing configuration values
- Provide unified way to access configuration properties located across different sources (text file, web config or database)

Download

Available via NuGet: http://nuget.org/List/Packages/Typed.Configuration

Usage scenario

Config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="FooCount" value="98"/>
  </appSettings>
</configuration>
Code
using NUnit.Framework;
using Typed.Configuration.ConfigSource;

namespace Typed.Configuration.Tests
{
    [TestFixture]
    public class ConfigServiceUsage
    {
        // Define interface with configuration properties
        public interface IConfigInterface
        {
            [FieldName("FooCount")]
            int? ItemsPerPage { get; }
            
            [FieldName("NotExistingField")]
            int? NotExistingField { get; }
        }

        [Test]
        public void FullUsageScenarioForStubSource()
        {
            var configurationServiceBuilder = new ConfigurationServiceBuilder();

            // Register configuration interface within service
            configurationServiceBuilder.RegisterConfigInterface<IConfigInterface>(new AppSettingsSource(), new NullValueSource());

            // Create service instance and put it to your IoC container, or store in static vairable
            ConfigurationService configService = configurationServiceBuilder.Build();

            // Get configuration property value
            int? itemsPerPageCount = configService.For<IConfigInterface>().ItemsPerPage;
            Assert.AreEqual(98, itemsPerPageCount);
            int? notExistingValue = configService.For<IConfigInterface>().NotExistingField;
            Assert.IsNull(notExistingValue);
        }
    }
}

Details

Currently supported property types:
- Int?
- Int[]
- String
- String[]

Last edited May 10, 2011 at 8:46 PM by valera_kolupaev, version 10