Click here to Skip to main content
15,896,606 members
Articles / Web Development / ASP.NET

Simple and flexible way to access and store configuration settings

Rate me:
Please Sign up or sign in to vote.
4.59/5 (11 votes)
6 Jan 2012CPOL4 min read 30.7K   299   31  
An easy way to manage application configuration with a wide range of options for storage.
using System;
using System.Linq;
using i3Quest.Configuration.Core;

namespace i3Quest.Configuration
{
	public class Person : IConfigurationElement
	{
		public string FirstName { get; set; }
		public string LastName { get; set; }
		public DateTime DateOfBirth { get; set; }
		public PostalAddress MailingAddress { get; set; }

		public void WriteConfiguration(IConfigurationContext context)
		{
			context["FirstName"] = FirstName;
			context["LastName"] = LastName;
			context["DateOfBirth"] = DateOfBirth.ToShortDateString();
			if (MailingAddress != null)
			{
				// get existing or create new context
				var ctx = context.Get("MailingAddress", true).First();
				MailingAddress.WriteConfiguration(ctx);
			}
		}

		public void ReadConfiguration(IConfigurationContext context)
		{
			FirstName = context["FirstName"];
			LastName = context["LastName"];
			DateTime dt;
			DateTime.TryParse(context["DateOfBirth"], out dt);
			DateOfBirth = dt;
			var ctx = context.Get("MailingAddress", false).FirstOrDefault();
			if (ctx != null)
			{
				MailingAddress = new PostalAddress();
				MailingAddress.ReadConfiguration(ctx);
			}
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO i3 Quest Inc
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Organisation (No members)


Comments and Discussions