在DAL模式中实现搜索的最佳方法是什么?

时间:2013-05-29 20:47:09

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

我通常会尽可能地声明我的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子句。

这种方法很有意思?

谢谢。

1 个答案:

答案 0 :(得分:0)

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