Created
July 12, 2011 15:34
-
-
Save jmarnold/1078219 to your computer and use it in GitHub Desktop.
Stringification Registry
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 ConfigureStringifierExpression | |
{ | |
private readonly Func<StringifyRequest, bool> _predicate; | |
private readonly StringifierModel _model; | |
public ConfigureStringifierExpression(Func<StringifyRequest, bool> predicate, StringifierModel model) | |
{ | |
_predicate = predicate; | |
_model = model; | |
} | |
public void ConvertUsing(Func<StringifyRequest, string> converter) | |
{ | |
_model.AddStringifier(new LambdaStringifier(_predicate, converter)); | |
} | |
} |
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 LambdaStringifier : IStringifier | |
{ | |
private readonly Func<StringifyRequest, bool> _predicate; | |
private readonly Func<StringifyRequest, string> _converter; | |
public LambdaStringifier(Func<StringifyRequest, bool> predicate, Func<StringifyRequest, string> converter) | |
{ | |
_predicate = predicate; | |
_converter = converter; | |
} | |
public bool Matches(StringifyRequest request) | |
{ | |
return _predicate(request); | |
} | |
public string Stringify(StringifyRequest request) | |
{ | |
return _converter(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 MyStringifierRegistry : StringifierRegistry | |
{ | |
public MyStringifierRegistry() | |
{ | |
IfRequestMatches(r => r.RequestType == typeof(DateTime)) | |
.ConvertUsing(r => ((DateTime)r.RawValue).ToString("MM/dd/yyyy HH:mm:ss")); | |
} | |
} |
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 StringifierRegistry | |
{ | |
private readonly StringifierModel _model = new StringifierModel(); | |
public StringifierRegistry() | |
{ | |
AddStringifier<DefaultStringifier>(); | |
} | |
public void AddStringifier<TStringifier>() | |
where TStringifier : IStringifier | |
{ | |
_model.AddStringifier(typeof(TStringifier)); | |
} | |
public void AddStringifier(IStringifier stringifier) | |
{ | |
_model.AddStringifier(stringifier); | |
} | |
public ConfigureStringifierExpression IfRequestMatches(Func<StringifyRequest, bool> predicate) | |
{ | |
return new ConfigureStringifierExpression(predicate, _model); | |
} | |
public StringifierModel BuildModel() | |
{ | |
return _model; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment