Skip to content

Instantly share code, notes, and snippets.

@JeffreyZhao
Created October 18, 2009 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyZhao/212644 to your computer and use it in GitHub Desktop.
Save JeffreyZhao/212644 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleTest.Tdd
{
internal interface IConverter
{
object Convert(List<string> values);
}
internal class KeywordConverter : IConverter
{
public object Convert(List<string> values)
{
return values[0];
}
}
internal class PriceRangeConverter : IConverter
{
public object Convert(List<string> values)
{
return new PriceRange
{
Min = float.Parse(values[0]),
Max = float.Parse(values[1])
};
}
}
internal class ColorConverter : IConverter
{
public object Convert(List<string> values)
{
return null;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleTest.Tdd
{
public class SearchCriteria
{
public PriceRange Price { get; set; }
public string Keywords { get; set; }
public Color Colors { get; set; }
}
[Flags]
public enum Color
{
Red = 1,
Black = 1 << 1,
White = 1 << 2
}
public class PriceRange
{
public float Min { get; set; }
public float Max { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace SimpleTest.Tdd
{
public class SearchCriteriaBinder : IModelBinder
{
public SearchCriteriaBinder()
: this(new Tokenizer(), new SearchCriteriaBuilder()) { }
internal SearchCriteriaBinder(ITokenizer tokenizer, ISearchCriteriaBuilder builder)
{
this.m_tokenizer = tokenizer;
this.m_builder = builder;
}
private readonly ITokenizer m_tokenizer;
private readonly ISearchCriteriaBuilder m_builder;
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var modelName = bindingContext.ModelName;
var rawValue = bindingContext.ValueProvider[modelName].RawValue;
var text = HttpUtility.UrlDecode(rawValue.ToString());
var tokenGroups = this.m_tokenizer.Tokenize(text);
return this.m_builder.Build(tokenGroups);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleTest.Tdd
{
internal interface ISearchCriteriaBuilder
{
SearchCriteria Build(List<string[]> tokenGroups);
}
internal class SearchCriteriaBuilder : ISearchCriteriaBuilder
{
internal SearchCriteriaBuilder() : this(GetConverter)
{
}
internal SearchCriteriaBuilder(Func<string, IConverter> converterGetter)
{
this.m_getConverter = converterGetter;
}
internal static IConverter GetConverter(string field)
{
// 使用if ... else或是字典
return null;
}
private readonly Func<string, IConverter> m_getConverter;
public SearchCriteria Build(List<string[]> tokenGroups)
{
var fieldTokens = tokenGroups.ToDictionary(
g => g[0].ToLowerInvariant(),
g => g.Skip(1).ToList());
var searchCriteria = new SearchCriteria();
List<string> values;
if (fieldTokens.TryGetValue("keywords", out values))
{
searchCriteria.Keywords = (string)this.m_getConverter("keywords").Convert(values);
}
if (fieldTokens.TryGetValue("price", out values))
{
searchCriteria.Price = (PriceRange)this.m_getConverter("price").Convert(values);
}
if (fieldTokens.TryGetValue("color", out values))
{
searchCriteria.Colors = (Color)this.m_getConverter("color").Convert(values);
}
return searchCriteria;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleTest.Tdd
{
internal interface ITokenizer
{
List<string[]> Tokenize(string text);
}
internal class Tokenizer : ITokenizer
{
public List<string[]> Tokenize(string text)
{
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment