Created
July 12, 2011 04:55
-
-
Save jmarnold/1077421 to your computer and use it in GitHub Desktop.
Simplistic Stringification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IStringifier | |
{ | |
bool Matches(StringifyRequest request); | |
string Stringify(StringifyRequest request); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringifierDef | |
{ | |
public Type Type { get; set; } | |
public IStringifier Value { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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