Skip to content

Instantly share code, notes, and snippets.

@fboiton
Created January 20, 2011 17:22
Show Gist options
  • Save fboiton/788219 to your computer and use it in GitHub Desktop.
Save fboiton/788219 to your computer and use it in GitHub Desktop.
Custom Dictionary to get formatted values based on specific values and rules
using System;
using System.Collections.Generic;
namespace CustomDictionary {
public interface IGenericDictionarySB<T, TK> {
T this[TK key] { get; set; }
}
public class SuperBidParameters : IGenericDictionarySB<string,string> {
private string _ageDriver;
public string this[string key] {
get { return GetFormattedProperty(key); }
set { StoreProperty(key, value); }
}
private string GetFormattedProperty(string key) {
string val = null;
switch (key)
{
case "age_driver":
val = _ageDriver;
break;
}
return string.IsNullOrEmpty(val) ? string.Empty : GetProperty(key, val);
}
private static string GetProperty(string key, string val) {
IMatcherFormatter matcherFormatter = MatcherFormatterFactory.GetFormatter(key);
return matcherFormatter.Format(val);
}
private void StoreProperty(string key, string value) {
switch (key) {
case "age_driver" :
_ageDriver = value;
break;
}
}
}
public static class MatcherFormatterFactory {
public static IMatcherFormatter GetFormatter(string key) {
switch (key)
{
case "age_driver":
return new AgeDriverFormatter();
}
}
}
public interface IMatcherFormatter {
string Format(string value);
}
public class AgeDriverFormatter : IMatcherFormatter {
private IEnumerable<string> _availableRanges;
public AgeDriverFormatter() {
_availableRanges = LoadRanges("age_driver");
}
private IEnumerable<string> LoadRanges(string key) {
return key.Equals("age_driver") ? new []{"20-30"} : null;
}
public string Format(string value) {
if (_availableRanges == null)
throw new InvalidOperationException("There is no rules to apply format");
return value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment