Smark.Data 1.5更新详解
? ? 由于一直忙于Beetle的開發和優化,Smark.Data組件已經有很長一段時間更新。這段時間發現Smark.Data在某些情況下不足,而這些情況又比較普遍所以進行了1.5版的更新。其更新的主要功能包括:查詢可填充到自定義對象中,可直接執行存儲過程和執行存儲過程數據填充到對象中。
查詢可填充到自定義對象中
在使用組件的時候會針對表制定一個數據對象結構,在查詢的時候進行對象填充。但很多時候我們是需要查詢這個對象,但實際上并不需要獲取其所有信息。很多時候查詢只是獲取某幾個成員信息,如果在應用做不到按需查詢那對數據庫的性能將會在著很大的影響。所以在1.5版中添加了這一功能,以下通過簡單應但要相應的功能。
[Table("Employees")]public interface IEmployee{[ID]int EmployeeID { get; set; }[Column]string LastName { get; set; }[Column]string FirstName { get; set; }[Column]string Title { get; set; }[Column]string TitleOfCourtesy { get; set; }[Column]DateTime BirthDate { get; set; }[Column]DateTime HireDate { get; set; }[Column]string Address { get; set; }[Column]string City { get; set; }[Column]string Region { get; set; }[Column]string PostalCode { get; set; }[Column]string Country { get; set; }[Column]string HomePhone { get; set; }[Column]string Extension { get; set; }}以上是一個簡單的雇員信息描述。
Expression exp = new Expression();var items = exp.List<Employee>();以上操作是獲取所有雇員信息,而產生的SQL如下:
Select (EmployeeID) as p_EmployeeID,(LastName) as p_LastName,(FirstName) as p_FirstName, (Title) as p_Title,(TitleOfCourtesy) as p_TitleOfCourtesy,(BirthDate) as p_BirthDate, (HireDate) as p_HireDate,(Address) as p_Address,(City) as p_City,(Region) as p_Region, (PostalCode) as p_PostalCode,(Country) as p_Country,(HomePhone) as p_HomePhone, (Extension) as p_Extension from Employees但有些時候并不想獲取所有信息,如只獲取雇員的簡單聯系信息。在1.5版本前的做法重新描述一個對象,并添加相關映射屬性;在1.5版里就相對簡單很多只需描述信息對象,并不需要描述映射屬性。
public class EmployeeContact{public string LastName { get; set; }public string FirstName { get; set; }public string Address { get; set; }public string City { get; set; }public string Region { get; set; }public string PostalCode { get; set; }public string Country { get; set; }public string HomePhone { get; set; }}在查詢的時候只需要,調用List方法的另一版本即可。
Expression exp = new Expression();var items = exp.List<Employee, EmployeeContact>();以上生成的SQL并不會獲取所有字段,而是根據EmployeeContact和Employee相匹配的屬性進行生成查詢字段。
Select (LastName) as p_LastName,(FirstName) as p_FirstName,(Address) as p_Address, (City) as p_City,(Region) as p_Region,(PostalCode) as p_PostalCode,(Country) as p_Country,(HomePhone) as p_HomePhone from Employees存儲過程執行
本人并不喜歡使用存儲過程,但客觀事實也有不少地方有使用存儲過程的習慣。所以組件也直接支持存儲過程處理,更方便使用存儲過程的使用者。組件對存儲過程的調用也和數據表訪問一樣,通過對象來描述;首先需要調用這樣一個存儲過程
以上存儲過程有一個輸入參數和一個返回參數,那可以這樣定義一個存儲過程映射對象
[Proc]public class CustOrderHist{[PorcParameter]public string CustomerID{get;set;}[PorcParameter(Direction= System.Data.ParameterDirection.ReturnValue)]public int Result{get;set;}}通過一個Proc屬性來描述對象是一個存儲過程描述對象,如果屬性不指定名稱則用對象名稱作為存儲過程名稱調用;通過PorcParameter來描述一個屬性對應的存儲過程參數。這樣描述后就可以進行一個存儲過程執行.
CustOrderHist p = new CustOrderHist();p.CustomerID = "ALFKI";DBContext.ExecProc(p);如果有輸出類型參數,執行完成后組件會自動把輸出參數值填充到對應的屬性上.?當需執行存儲過程返回一個對象列表的時候可以,通過以下方法調用即可:
CustOrderHist p = new CustOrderHist();p.CustomerID = "ALFKI";var items = DBContext.ExecProcToObjects<OrderHist>(p);其屬性對應關系是屬性名和字段一致即可,對象成員不需要添加任何屬性描述。
? ? ? Smark.Data最新源碼可以到http://smark.codeplex.com/獲取
總結
以上是生活随笔為你收集整理的Smark.Data 1.5更新详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件设计师教程 第5版 下载
- 下一篇: 利用MySQL语句批量替换指定wordp