Pages

September 11, 2013

Configuration Pattern

Easy configuration pattern with typed, stateful, and easy to use manage settings.

Settings are represented by a custom type. Since the type is owned by the user it can be fully tailored with little constraint. For example, in WPF it can support INotifyPropertyChanged and be completely bound to your view. It can have in memory default values, be inherited, and even encapsulated logic.

public class MySettings {
 int Host { get; set;}
 int Port { get; set;}
}

The settings library itself is a static container that manages the saving (serialization) and loading (deserialization) of your in-memory settings object. It also provides a consistent way of accessing the object as well as maintaining its lifetime.

public static class Config<T> where T : new(){
 private T _instance = new T(); //Can be made lazy
 
 public T Instance { get { return _instance; } }

  //Instance is created via configurator which 
  //implements the deserialization logic
  public static void Load(IConfigurator conf, string location){
  _instance= conf.Load(location);
 }

 //Instance is persisted via configurator which 
 //implements the serialization logic
 public static void Save(string location){
  conf.Save(Instance , location);
 }
}

A simple interface allows you to create your own configurators that define how serialization and deserialization will be performed. The configurators can be fed to the static config class, or the static config class can be written with pre-canned configurators.

interface IConfigurator
{
 T Load<T>(string location);
 void Save<T>(T settings, string location);
}


Using the configuration class is simple. Since the actual settings object is manged by the encapsulating static config class, all you need to do is just call it. Accessing the settings properties without loading will always yield the default memory values. Calling the Load method will update the internal instance of the settings object.

Config<MySettings>.Load(new XmlConfigurator(), "settings.xml");

Once loaded, the Instance property returns a shared instance of in memory settings.

var host = Config<MySettings>.Instance.Host;
var port = Config<MySettings>.Instance.Port;

The settings can be easily manipulated in memory.
Config<MySettings>.Instance.Host = host;
Config<MySettings>.Instance.Port = port;

The settings can also be saved just as easily.
Config<MySettings>.Save(new DBConfigurator(), sqlConnectionString);

September 6, 2013

Zip to Get Element to Element Delta

Often when manipulating time series, you may want to get difference between adjacent elements in a list.

Take a look at the example below:

"Original" is the original time series list of Date/Value, with the Delta row illustrating the expected dela we are trying to calculate.

Linq can handle this in a pretty simple way (this can be pasted into linqpad):

var original = new [] { 
  Tuple.Create(new DateTime(2010, 1, 1), 100),
  Tuple.Create(new DateTime(2010, 2, 1), 50),
  Tuple.Create(new DateTime(2010, 3, 1), 75)
};

var anchor = Tuple.Create(new DateTime(2009, 12, 1), 0);

var B = (new []{ anchor }).Concat(original);
var A = original;

var deltas = A.Zip(B, (a, b) => Tuple.Create(a.Item1, a.Item2 - b.Item2));

deltas.Dump(1);

Note that if you don't really care about the first diff, you can simply Skip and Zip like so:
var B = new [] { 
  Tuple.Create(new DateTime(2010, 1, 1), 100),
  Tuple.Create(new DateTime(2010, 2, 1), 50),
  Tuple.Create(new DateTime(2010, 3, 1), 75)
};

var A = B.Skip(1);

var deltas = A.Zip(B, (a, b) => Tuple.Create(a.Item1, a.Item2 - b.Item2));

deltas.Dump(1);