Skip to content

Instantly share code, notes, and snippets.

@jmarnold
Created July 12, 2011 04:55
Show Gist options
  • Save jmarnold/1077421 to your computer and use it in GitHub Desktop.
Save jmarnold/1077421 to your computer and use it in GitHub Desktop.
Simplistic Stringification
public interface IStringifier
{
bool Matches(StringifyRequest request);
string Stringify(StringifyRequest request);
}
public class Stringifier
{
private readonly IEnumerable<IStringifier> _stringifiers;
public Stringifier(IEnumerable<IStringifier> stringifiers)
{
_stringifiers = stringifiers;
}
public string Stringify(StringifyRequest request)
{
var stringifier = _stringifiers.FirstOrDefault(s => s.Matches(request));
if(stringifier == null) throw new InvalidOperationException("No stringifier found matching request");
return stringifier.Stringify(request);
}
}
public class StringifierDef
{
public Type Type { get; set; }
public IStringifier Value { get; set; }
}
public class StringifierModel
{
private readonly IList<StringifierDef> _stringifiers = new List<StringifierDef>();
public void AddStringifier(IStringifier stringifier)
{
_stringifiers.Add(new StringifierDef { Value = stringifier});
}
public void AddStringifier(Type stringifierType)
{
_stringifiers.Add(new StringifierDef { Type = stringifierType });
}
public IEnumerable<StringifierDef> Stringifiers { get { return _stringifiers; } }
}
public class StringifyRequest
{
public Type RequestType { get; set; }
public object RawValue { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment