摘要:继续前面的话题,上次给出了个不甚明确的大图画,结果引来众多高手指教,不亦乐乎!这次我想明确一下、具体化一下,就是下面这样一个命题(C#示意性代码):
如果有:
[TableMapping("Book")]public class BookInfo{ public BookInfo(int bookID, string title, string publisher); public int BookID; public string Title; public string Publisher;}
怎样将这段代码:
public BookInfo[] SelectByPublisher(string Publisher){ SqlCommand command = new SqlCommand(); command.CommandType = CommandType.Text; command.CommandText = "SELECT BookID, Title, Publisher FROM Book WHERE Publisher=@Publisher"; command.Parameters.Add("@Publisher", Publisher); using (SqlConnection connection = new SqlConnection(connectionString)) { command.Connection = connection; connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (!reader.HasRows) return new BookInfo[0]; ArrayList result = new ArrayList(); while (reader.Read()) { result.Add(new BookInfo(reader[0], reader[1], reader[2])); } return (BookInfo[])result.ToArray(typeof(BookInfo)); }}
用这样的统一方式来轻巧的实现:
public BookInfo[] SelectByPublisher(string Publisher){ return (BookInfo[])ElegantORM.Execute(Publisher);}
显然,我们需要实现ElegantORM(暂且不论这个codename起的多烂啦:)这个helper。怎么实现最合理?它应该能够支持前文所述的几种常见数据访问组件接口方法的signature,且不仅依赖于使用custom attributes的映射元数据(即可扩展到支持外部映射配置文件等等)……
BTW: 在本文下我们不讨论三层结构相关的话题(不过欢迎你把这方面的见解继续发到我的前一个随笔中:),只针对这个具体命题的设计和实现,我很想听到大家的见解并分享自己的心得。:)...[
阅读全文]