Improve data access layer select method Pattern

后端 未结 7 904
眼角桃花
眼角桃花 2020-12-05 05:46

Lately I find myself writing data access layer select methods where the code all takes this general form:

public static DataTable GetSomeData( ... arguments)         


        
7条回答
  •  情深已故
    2020-12-05 06:28

    One pattern I've enjoyed looks like this as far as client code goes:

            DataTable data = null;
            using (StoredProcedure proc = new StoredProcedure("MyProcName","[Connection]"))
            {
                proc.AddParameter("@LoginName", loginName);
                data = proc.ExecuteDataTable();
            }
    

    I usually make connection optional, and I will code in a way to pull it from ConnectionStrings config section or treat it as the actual connection string. This lets me reuse the dal in one off scenario's and is in part a habbit from the COM+ days when I stored the connection string using the object construction property.

    I like this because it's easy to read and hides all the ADO code from me.

提交回复
热议问题