GoFigure 2.0 Documentation to-be-released soon...

 



GoFigure currently supports: .NET 3.5-4.0, Silverlight 3-5, Windows Phone 7.0-7.1, and jQuery

Some code samples to satisfy your appetite:

Porting existing .NET config settings to GoFigure:

If we had these settings in our .config file...

<appSettings>
	<add key="appSetting1" value="appSetting1Value"/>
</appSettings>

<connectionStrings>
	<add name="connectionString1" connectionString="connectionString1Value"/>
</connectionStrings>

We could simply start using this...

bool appSettingExists = GoFigure.DefaultConfig.HasAppSetting("appSetting1");
string appSettingValue = GoFigure.DefaultConfig.GetAppSetting("appSetting1");
bool connectionStringExists = GoFigure.DefaultConfig.HasConnectionString("connectionString1");
string connectionStringValue = GoFigure.DefaultConfig.GetConnectionString("connectionString1");

Instead of this...

bool appSettingExists = ConfigurationManager.AppSettings["appSetting1"] != null;
string appSettingValue = ConfigurationManager.AppSettings["appSetting1"];
bool connectionStringExists = ConfigurationManager.ConnectionStrings["connectionString1"] != null;
string connectionStringValue = connectionStringExists ? ConfigurationManager.ConnectionStrings["connectionString1"].ConnectionString : null;

Why? With GoFigure, the code no longer knows where it gets these settings and connection strings. Today, you might use the local .NET config file for development, but tomorrow you may want to get some or all of this information from somewhere else! Without GoFigure, you will have to change code in multiple places, re-compile, re-test, and re-release your application. With GoFigure, it *can* be as simple as swapping the configuration provider in the .NET config file.

jQuery Sample:

 

// with direct callbacks specific to the setting retrieved

// note that we are initiating and using GoFigure in the same call (e.g. "$.goFigure().....")
$.goFigure().getSetting(
	'mySettingKey', 
	// onSuccess
	function(json) {
		if (json) alert('Setting value is: ' + json);
		else alert('No setting found.');
	},
	// onError
	function (err) {
	alert('Error: ' + (err.responseText ? err.responseText : err.toString()));
});

// if there were multiple calls for settings, you could improve the performance slightly by storing GoFigure in a variable

var $goFigure = $.goFigure();

$goFigure.getSetting(
	'mySettingKey', 
	// onSuccess
	function(json) {
		if (json) alert('Setting value is: ' + json);
		else alert('No setting found.');
	},
	// onError
	function (err) {
	alert('Error: ' + (err.responseText ? err.responseText : err.toString()));
});

$goFigure.getSetting(
	'mySettingKeyTwo', 
	// onSuccess
	function(json) {
		if (json) alert('Setting #2 value is: ' + json);
		else alert('No setting #2 found.');
	},
	// onError
	function (err) {
	alert('Error #2: ' + (err.responseText ? err.responseText : err.toString()));
});

// or, with global callback handlers for all settings
// note that you will need to manage which setting was requested for type of request
// this is best used if only one request for settings is made

// note that we are storing the initiated GoFigure object in a variable and then using it
var $goFigure = $.goFigure({
	serviceUrl: '/gofigure/',
	success: function (json) {
		if (json) alert('Setting value is: ' + json);
		else alert('No setting found.');
	},
	error: function (err) {
		alert('Error: ' + (err.responseText ? err.responseText : err.toString()));
	}
});

$goFigure.getSetting('mySettingKey'); // see 'success' method above

 

Why? Web applications are getting more complicated and sometimes retrieving configuration information (simple or complex) is required. Abstracting the source of this information can be as simple as an AJAX request to the server. With GoFigure, writing the service is done for you with the added benefit that even the server's source of this information can be abstracted to anything from a physical configuration file to another service or database!

Synchronous WCF Client Example:

 

var client = GoFigureServices.CreateChannel<IGoFigureXmlService>("myChannelKey");
var settingValue = client.Get(input);

 

Asynchronous WCF Client Example:

 

var channel = GoFigureServices.CreateChannel<IGoFigureXmlService>(channelKey);

channel.BeginGet("mySettingKey",
                 asyncResult =>
                 {
                     var settingValue = channel.EndGet(asyncResult);
                     
                     // do something with the value, like...
                     // Dispatcher.BeginInvoke(() =>
                     // {
                     //     // do something with the UI
                     // });
                 }, null);

 

Complicated C# Sample (demonstrated in a NUnit test):

using System.Collections.Generic;
using NUnit.Framework;

namespace GoFigure.Tests.DogFood
{
	[TestFixture]
	public class DogFoodTestFixture
	{
		[TestCase]
		public void BigTest()
		{
			// declare config setting value entities for test
			var anonType = new { Name = "Anonymous type declaration" };
			var person = new Person { Name = "Person name", ResidentialAddress = new Address { Street = "555 E Test Way" } };
			var nullValue = (object)null;

			#region Declare Configuration Providers

			// Declare multiple providers for configuration settings...

			// support for out-of-the-box .NET configuration sections (<appSettings />, <connectionStrings />, <applicationSettings />, and <userSettings />)
			var app = new GoFigureLocalAppSettingsProvider();
			var conn = new GoFigureLocalConnectionStringsProvider();
			var appAndUser = new GoFigureLocalApplicationAndUserSettingsProvider(Properties.Settings.Default);

			// custom, in-memory provider
			var dict = new GoFigureDictionaryProvider(
				new Dictionary<string, object>
				{
					{ "dictKey1", "dictKey1Value" },
					{ "dictKey2", anonType },
					{ "dictKey3", person }
				});

			// lookup provider
			var lookup = new GoFigureLookupByPathProvider(
				// route input setting keys to other providers
				settingPath =>
				{
					if (settingPath.StartsWith("memory/")) return dict;
					if (settingPath.StartsWith("connectionStrings/")) return conn;
					if (settingPath.StartsWith("appSettings/")) return app;
					if (settingPath.StartsWith("user/") || settingPath.StartsWith("application/")) return appAndUser;
					return null;
				},
				// map input setting keys to provider-specific keys
				settingPath => settingPath.Substring(settingPath.LastIndexOf("/") + 1));

			#endregion

			// register configuration provider
			GoFigure.LocateUsing(lookup);

			#region Test Assertions

			Assert.IsTrue(GoFigure.Has("memory/dictKey1"), "dictKey1");
			Assert.AreEqual("dictKey1Value", GoFigure.Get("memory/dictKey1", string.Empty), "dictKey1Value");
			Assert.AreSame(anonType, GoFigure.Get("memory/dictKey2", nullValue), "dictKey2Value");
			Assert.AreSame(person, GoFigure.Get("memory/dictKey3", nullValue), "dictKey3Value");
			Assert.IsFalse(GoFigure.Has("memory/nope"), "dict nope");

			Assert.IsTrue(GoFigure.Has("appSettings/appSetting1"), "appSetting1");
			Assert.AreEqual("appSetting1Value", GoFigure.Get("appSettings/appSetting1", string.Empty), "appSetting1Value");
			Assert.AreEqual("appSetting2Value", GoFigure.Get("appSettings/appSetting2", string.Empty), "appSetting2Value");
			Assert.AreEqual("appSetting3Value", GoFigure.Get("appSettings/appSetting3", string.Empty), "appSetting3Value");
			Assert.AreEqual("appSetting4Value", GoFigure.Get("appSettings/appSetting4", string.Empty), "appSetting4Value");
			Assert.IsFalse(GoFigure.Has("appSettings/nope"), "app nope");

			Assert.IsTrue(GoFigure.Has("connectionStrings/connectionString1"), "connectionString1");
			Assert.AreEqual("connectionString1Value", GoFigure.Get("connectionStrings/connectionString1", string.Empty), "connectionString1Value");
			Assert.AreEqual("connectionString2Value", GoFigure.Get("connectionStrings/connectionString2", string.Empty), "connectionString2Value");
			Assert.AreEqual("connectionString3Value", GoFigure.Get("connectionStrings/connectionString3", string.Empty), "connectionString3Value");
			Assert.IsFalse(GoFigure.Has("connectionStrings/nope"), "conn nope");

			Assert.IsTrue(GoFigure.Has("user/userSetting1"), "userSetting1");
			Assert.IsFalse(GoFigure.Has("user/nope"), "user nope");

			Assert.IsTrue(GoFigure.Has("application/applicationSetting1"), "applicationSetting1");
			Assert.IsFalse(GoFigure.Has("application/nope"), "application nope");

			Assert.Throws<GoFigureProviderNotFoundException>(() => GoFigure.Get("no provider", string.Empty), "no provider");

			#endregion
		}

		#region Config setting value entities for test

		private class Person
		{
			public string Name { get; set; }

			public Address ResidentialAddress { get; set; }
		}

		private class Address
		{
			public string Street { get; set; }
		}

		#endregion
	}
}

Last edited Feb 14, 2012 at 1:42 AM by iseric, version 6

Comments

No comments yet.