QueryBuilder: A Simple Way to Read and Write Query Strings

QueryBuilder: A Simple Way to Read and Write Query Strings

http://www.codeproject.com/Articles/875164/QueryBuilder-A-Simple-Way-to-Read-and-Write-Query

代码下载地址:http://pan.baidu.com/s/1ntuWEpV

namespace TestApplication.Query
{
    public class QueryParameter
    {
        public string Name { get; set; }
        public string Value { get; set; }
        internal bool IsFirst { get; set; }

        public QueryParameter(string name, string value)
        {
            this.Name = name;
            this.Value = value;
        }

        public QueryParameter(string name)
            : this(name, "")
        { }

        public static QueryParameter Create(string parameter)
        {
            string trimmed = parameter.Replace("&", "").Replace("?", "");
            if (trimmed.Contains("="))
            {
                string[] split = trimmed.Split('=');
                return new QueryParameter(split[0], split[1]);
            }
            return new QueryParameter(trimmed);
        }

        private string Flatten()
        {
            return (this.IsFirst ? '?' : '&') + this.Name +
                   (string.IsNullOrEmpty(this.Value) ? "" : "=") + this.Value;
        }

        public override string ToString() { return this.Flatten(); }

        //...

    }
}

using System.Collections.Generic;
using System.Linq;

namespace TestApplication.Query
{
    public class QueryBuilder
    {
        private List<QueryParameter> _parameters;
        public QueryBuilder()
        {
            _parameters = new List<QueryParameter>();
        }

        public QueryParameter this[int index]
        {
            get { return _parameters[index]; }
        }

        public string this[string name]
        {
            get
            {
                return HasParameter(name) ?
                    _parameters.Find(x => x.Name == name).Value : null;
            }
        }

        public bool HasParameter(string name)
        {
            return _parameters.Exists(p => p.Name == name);
        }

        #region method

        public void AddParameter(QueryParameter parameter)
        {
            _parameters.Add(parameter);
            _parameters.Last().IsFirst = (_parameters.Count == 1);
        }
        public void AddParameter(string name, string value)
        {
            AddParameter(new QueryParameter(name, value));
        }
        public void RemoveParameter(QueryParameter parameter)
        {
            _parameters.Remove(parameter);
        }
        public void RemoveParameter(string name)
        {
            QueryParameter param = _parameters.Find(x => x.Name == name);
            _parameters.Remove(param);
        }

        public static QueryBuilder Create(string queryString)
        {
            QueryBuilder toReturn = new QueryBuilder();
            if (queryString.Contains('&'))
            {
                string[] arr = queryString.Split('&');
                foreach (string p in arr)
                {
                    toReturn.AddParameter(QueryParameter.Create(p));
                }
            }
            else
            {
                toReturn.AddParameter(QueryParameter.Create(queryString));
            }
            return toReturn;
        }
        private string CompileQueryString()
        {
            string toReturn = "";
            foreach (QueryParameter p in _parameters)
            {
                toReturn += p.ToString();
            }
            return toReturn;
        }

        public override string ToString() { return CompileQueryString(); }

        #endregion

        //...

    }
}
//测试下

class Program
    {
        static void Main(string[] args)
        {
            Query.QueryBuilder querybuilder = new Query.QueryBuilder();
            querybuilder.AddParameter("name", "jon");
            querybuilder.AddParameter("bb", "123");
            querybuilder.AddParameter("aa", "mike");
            querybuilder.AddParameter("cc", "hello");
            Console.WriteLine(querybuilder.ToString());
            Console.ReadKey();
        }

        //..

    }
?name=jon&bb=123&aa=mike&cc=hello



你可能感兴趣的:(QueryBuilder)