用DapperExtensions和反射來實現一個通用搜索


前言

  搜索功能是一個很經常使用的功能,固然這個搜索不是指全文檢索,是指網站的後臺管理系統或ERP系統列表的搜索功能。常見作法通常就是在搜索欄上加上幾個經常使用字段來搜索。代碼可能通常這樣實現javascript

StringBuilder sqlStr = new StringBuilder();
if (!string.IsNullOrEmpty(RealName))
{
    sqlStr.Append(" and RealName  = @RealName");
}
if (Age != -1)
{
    sqlStr.Append(" and Age = @Age");
}
if (!string.IsNullOrEmpty(StartTime))
{
    sqlStr.Append(" and CreateTime >= @StartTime");
}
if (!string.IsNullOrEmpty(EndTime))
{
    sqlStr.Append(" and CreateTime <= @EndTime");
}
MySqlParameter[] paras = new MySqlParameter[]{
            new MySqlParameter("@Age", Age),
            new MySqlParameter("@RealName", RealName),
            new MySqlParameter("@StartTime", StartTime),
            new MySqlParameter("@EndTime", EndTime)
        };

 這段代碼若是遇到下面幾個需求,又該如何處理?css

  1. 再加一個查詢字段
  2. RealName須要改爲模糊查詢
  3. Age須要支持範圍查詢

可能大多數程序猿想法,這是新的需求,那麼就直接改代碼,簡單粗暴。而後在前臺加個age範圍文本框,後臺再加個if判斷,realname的=號就直接改爲like,就這樣輕鬆搞定了。但需求老是不斷變化,若是一張表有50個字段,同時須要支持其中40個字段查詢。我想大都數人第一反應:臥槽,神經病!難道就沒有一個通用的辦法來解決這種搜索的問題?我想說固然有,本文接下來就用DapperExtensions和反射來解決這個問題,最終於實現的效果以下圖:html

DapperExtensions介紹

  DapperExtensions是基於Dapper的一個擴展,主要在Dapper基礎上實現了CRUD的操做。它還提供了一個謂詞系統,能夠實現更多複雜的高級查詢功能。還能夠經過ClassMapper來定義實體類和表的映射。java

通用搜索功能實現

1.首先建立一個account表,而後增長一個Account類jquery

public class Account
    {
        public Account()
        {
            Age = -1;
        }
        /// <summary>
        /// 帳戶ID
        /// </summary>
        [Mark("帳戶ID")]
        public int AccountId { get; set; }
        /// <summary>
        /// 姓名
        /// </summary>
        [Mark("姓名")]
        public string RealName { get; set; }
        /// <summary>
        /// 年齡
        /// </summary>
        [Mark("年齡")]
        public int Age { get; set; }
        /// <summary>
        /// 建立時間
        /// </summary>
        [Mark("建立時間")]
        public DateTime CreateTime { get; set; }
    }

2.爲了獲取字段對應的中文名稱,咱們增長一個MarkAttribute類。由於有強大的反射功能,咱們能夠經過反射動態獲取每張表實體類的屬性和中文名稱。git

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
    public class MarkAttribute : Attribute
    {
        public MarkAttribute(string FiledName, string Description = "")
        {
            this.FiledName = FiledName;
            this.Description = Description;
        }

        private string _FiledName;
        public string FiledName
        {
            get { return _FiledName; }
            set { _FiledName = value; }
        }

        private string _Description;
        public string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }
    }

3.通用搜索思路主要是把搜索功能抽象出一個對象,本質上也就列名、操做符、值組成的一個對象集合,這樣就能夠實現多個搜索條件的組合。咱們增長一個Predicate類github

public class Predicate
    {
        /// <summary>
        /// 列名
        /// </summary>
        public string ColumnItem { get; set; }
        /// <summary>
        /// 操做符
        /// </summary>
        public string OperatorItem { get; set; }
        /// <summary>
        ////// </summary>
        public object Value { get; set; }
    }

4.而後經過反射Account類的屬性加載到前臺列名的DropDownList,再增長一個操做符的DropDownListajax

var columnItems = new List<SelectListItem>();
            //經過反射來獲取類的屬性
            //Type t = Assembly.Load("SearchDemo").GetType("SearchDemo.Models.Account");
       Type t = typeof(SearchDemo.Models.Account);   
foreach (PropertyInfo item in t.GetProperties()) { string filedName = (item.GetCustomAttributes(typeof(MarkAttribute), false)[0] as MarkAttribute).FiledName; columnItems.Add(new SelectListItem() { Text = filedName, Value = item.Name }); } ViewBag.columnItems = columnItems; var operatorItems = new List<SelectListItem>() { new SelectListItem() {Text = "等於", Value = "Eq"}, new SelectListItem() {Text = "大於", Value = "Gt"}, new SelectListItem() {Text = "大於或等於", Value = "Ge"}, new SelectListItem() {Text = "小於", Value = "Lt"}, new SelectListItem() {Text = "小於或等於", Value = "Le"}, new SelectListItem() {Text = "模糊", Value = "Like"} }; ViewBag.operatorItems = operatorItems;

 5.前臺界面實現代碼sql

<!DOCTYPE html>
<html>
<head>
    <title>DapperExtensions通用搜索</title>
    <script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        Date.prototype.format = function (format) {
            var o = {
                "M+": this.getMonth() + 1, //month   
                "d+": this.getDate(), //day   
                "h+": this.getHours(), //hour   
                "m+": this.getMinutes(), //minute   
                "s+": this.getSeconds(), //second   
                "q+": Math.floor((this.getMonth() + 3) / 3), //quarter   
                "S": this.getMilliseconds() //millisecond   
            }

            if (/(y+)/.test(format)) {
                format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            }

            for (var k in o) {
                if (new RegExp("(" + k + ")").test(format)) {
                    format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
                }
            }
            return format;
        }  
    </script>
    <style type="text/css">
        ul
        {
            list-style: none;
            padding: 0px;
            margin: 0px;
            width: 590px;
            height: 20px;
            line-height: 20px;
            border: 1px solid #99CC00;
            border-top: 0px;
            font-size: 12px;
        }
        ul li
        {
            display: block;
            width: 25%;
            float: left;
            text-indent: 2em;
        }
        .th
        {
            background: #F1FADE;
            font-weight: bold;
            border-top: 1px solid #99CC00;
        }
    </style>
    <script type="text/javascript">
        var predicates = [];
        var index = 0;
        $(document).ready(function () {
            $("#btnAdd").click(function () {
                var columnItem = $("#columnItems option:selected");
                var operatorItem = $("#operatorItems option:selected");
                var value = $("#value").val();
                if(value == ""){
                    alert("請輸入值");
                    return;
                }
                var predicate = { index: index, columnItem: columnItem.val(), operatorItem: operatorItem.val(), value: value };
                predicates.push(predicate);
                var html = "<ul><li>" + columnItem.text() + "</li><li>" + operatorItem.text() + "</li><li>" + value + "</li><li><a href='javascript:;' onclick='del(this," + index + ")'>刪除</a></li></ul>"
                $("#predicates ul:last").after(html);
                index++;
            })

            $("#btnSearch").click(function () {
                $.ajax({
                    type: "POST",
                    url: "home/search",
                    data: JSON.stringify(predicates),
                    contentType: "application/json",
                    success: function (data) {
                        if (data.Error != null) {
                            alert(data.Error);
                            return;
                        }
                        $("#list .th").nextAll().remove();
                        var html = "";
                        $.each(data, function (index, item) {
                            html += "<ul><li>" + item.AccountId + "</li>";
                            html += "<li>" + item.RealName + "</li>";
                            html += "<li>" + item.Age + "</li>";
                            //轉換日期
                            var dateMilliseconds = parseInt(item.CreateTime.replace(/\D/igm, ""));
                            var date = new Date(dateMilliseconds);
                            html += "<li>" + date.format("yyyy-MM-dd hh:mm:ss") + "</li></ul>";
                        });
                        $("#list .th").after(html);
                    }
                });
            })
        })

        function del(obj,index) {
            obj.parentNode.parentNode.remove();
            for (var i = 0; i < predicates.length; i++) {
                if (predicates[i].index == index) {
                    predicates.splice(i, 1);
                }
            }
        }
    </script>
</head>
<body>
    <div>
        列名:@Html.DropDownList("columnItems")&nbsp;&nbsp;操做符:@Html.DropDownList("operatorItems")&nbsp;&nbsp;值:@Html.TextBox("value")&nbsp;&nbsp;
        <input id="btnAdd" type="button" value="增長" />&nbsp;&nbsp;<input id="btnSearch" type="button" value="搜索" />
    </div>
    <br />
    <div id="predicates">
        <ul class="th">
            <li>列名</li>
            <li>操做符</li>
            <li>值</li>
            <li>操做</li>
        </ul>
    </div>
    <br />
    <div id="list">
        <ul class="th">
            <li>帳戶ID</li>
            <li>姓名</li>
            <li>年齡</li>
            <li>建立時間</li>
        </ul>    
    </div>
</body>
</html>

 6.最後經過DapperExtensions的謂詞和反射實現搜索方法json

        [HttpPost]
        public JsonResult Search(List<Predicate> predicates)
        {
            if (predicates == null)
            {
                return Json(new { Error = "請增長搜索條件" });
            }

            using (var connection = SqlHelper.GetConnection())
            {
                var pga = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
                foreach (var p in predicates)
                {
                    var predicate = Predicates.Field<Account>(GetExpression(p), (Operator)Enum.Parse(typeof(Operator), p.OperatorItem), p.Value);
                    pga.Predicates.Add(predicate);
                }
                var list = connection.GetList<Account>(pga);
                return Json(list);
            }
        }

        private static Expression<Func<Account, object>> GetExpression(Predicate p)
        {
            ParameterExpression parameter = Expression.Parameter(typeof(Account), "p");
            return Expression.Lambda<Func<Account, object>>(Expression.Convert(Expression.Property(parameter, p.ColumnItem), typeof(object)), parameter);
        }

  最終,經過簡單的幾行代碼,在基於DapperExtensions的功能基礎上,咱們最終實現了一個能夠支持多個字段、多個條件、多個操做符的通用查詢功能。本文也只是拋磚引玉,只是提供一種思路,還有更多細節沒有考慮。好比多個條件的組合能夠再增長一個邏輯符來鏈接、多個條件組合嵌套查詢、多表查詢等等。

Dapper地址

DapperExtensions的Wiki

SearchDemo源碼

ps:本人也是第一次在博客園發博客,有些地方可能表達不清楚,請廣大網友多多見諒。若是您有什麼好的建議和意見,也能夠在文章的評論區留言給我,我會及時更正! 我但願之後在博客園多發佈一些文章,和你們作更多的技術交流和分享。若是以爲本文對你有用,請你們多多點推薦或收藏。

相關文章
相關標籤/搜索