【问题标题】:What is the best way to implement a search in the pattern DAL?在模式 DAL 中实现搜索的最佳方法是什么?
【发布时间】:2013-05-25 06:44:48
【问题描述】:

我通常尽可能声明我的 DAL 类覆盖搜索方法,如下例所示:

public class clsAsd {
    private AdConn oConn;

    public clsAsd(ref AdConn Connection) {
        oConn = Connection;
    }

    private string sqlSearch(string DocType, string Status, string Aaa) {
        return
            "    AND x.tp_doc = '" + DocType + "'\r\n" +
            "    AND x.co_status IN (" + Status + ")\r\n" +
            "    AND x.aaa = '" + Aaa + "'\r\n";
    }
    public List<clsQwe> search(string DocType, string Status, string Aaa, bool IncDel) {
        return search(sqlSearch(DocType, Status, Aaa), IncDel);
    }

    private string sqlSearch(string DocType, string Status, string Aaa, string Bbb) {
        string sSQL = sqlSearch(DocType, Status, Aaa) + (Bbb != "" ? "    AND x.bbb = '" + Bbb + "'\r\n" : "");
        return sSQL;
    }
    public List<clsQwe> search(string DocType, string Status, string Aaa, string Bbb, bool IncDel) {
        return search(sqlSearch(DocType, Status, Aaa, Bbb), IncDel);
    }

    private List<clsQwe> search(string Where, bool IncDel) {
        string sSQL;

        sSQL = "SELECT\r\n";
        sSQL += "    b.aaa, c.bbb, x.*\r\n";
        sSQL += "FROM asd x, qwe b, zxc c\r\n";
        sSQL += "WHERE x.www = b.www\r\n";
        sSQL += "    AND x.zzz = c.zzz\r\n";
        sSQL += Where;

        if (!IncDel) sSQL += "    AND x.del IS NULL\r\n";

        sSQL += "ORDER BY x.www";

        // Connection + Run SQL

        // List to get results
        List<clsQwe> lstRet = new List<clsQwe>();

        // Row to add in list
        clsQwe oX;

        while (oReader.Read()) {
            oX = new clsQwe();
            // Add all data
            lstRet.Add(oX);
        }

        // Return data
        return lstRet;
    }
}

我的问题是:这是一个好习惯吗?

我应该为每个不同的搜索使用一种方法吗? 示例:

public class clsAsd {
    private AdConn oConn;
    public clsAsd(ref AdConn Connection) { oConn = Connection; }

    public List<clsQwe> search1(string DocType, string Status, string Aaa, bool IncDel) { }
    public List<clsQwe> search2(string DocType, string Status, string Aaa, string Bbb, bool IncDel) { }
    public List<clsQwe> search3(string DocType, string Status, string Aaa, string Bbb, string Ccc, bool IncDel) { }

    private List<clsQwe> search(string Where, bool IncDel) { }
}

如何改进这门课?

我正在考虑只实现一种接收类 (clsQwe) 作为参数的搜索方法。我检查这个类的每个属性并根据填充的属性创建 where 子句。

这种方法有意思吗?

谢谢。

【问题讨论】:

  • 不,不是。您对sql-injection 开放,使用参数化查询。
  • 我知道这一点,并且我在实际实现中使用 Oracle 命令。我的问题是如何最好地实现对我的班级的搜索?谢谢。

标签: .net design-patterns implementation data-access-layer


【解决方案1】:

为了改进直接使用 db 的类,您可以使用ORM。

【讨论】:

    猜你喜欢
    • 2010-12-14
    • 2016-01-19
    • 2016-05-23
    • 2021-04-24
    • 2010-11-02
    • 2011-03-20
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    相关资源
    最近更新 更多