以前就把petShop这个实例DOWN下来了,这是微软主要针对SUN公司的petStore而发布的一款源代码,好了,不多说.现在来看看此源码的主要内容.
它由多个项目组合而成,大致分为以下几层:
BLL。专门处理前台页面的所需功能.
ConfigTool和Utility主要负处理连库字符加密及其它工作.
DALFactory负责创建对象
就看Product这个类吧
2
3 /// Look up the DAL implementation we should be using
4 string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];
5 string className = path + ".Product";
6 //PetShop.SQLServerDAL.Product
7
8 // Using the evidence given in the config file load the appropriate assembly and class
9 return (PetShop.IDAL.IProduct)Assembly.Load(path).CreateInstance(className);
10 }
path值为PetShop.SQLServerDAL从web.config文件读取
主要负责创建PetShop.SQLServerDAL.Product这个类的实例;
(PetShop.IDAL.IProduct)Assembly.Load(path).CreateInstance(className);
到了这里就可以看IDAL.IProduct这个接口
2
3 /// <summary>
4 /// Method to search products by category name
5 /// </summary>
6 /// <param name="category">Name of the category to search by</param>
7 /// <returns>Interface to an arraylist of search results</returns>
8 IList GetProductsByCategory(string category);
9
10
11 /// <summary>
12 /// Method to search products by a set of keyword
13 /// </summary>
14 /// <param name="keywords">An array of keywords to search by</param>
15 /// <returns>Interface to an arraylist of search results</returns>
16 IList GetProductsBySearch(string[] keywords);
17 }
声明了两个方法.接口有了。当然要找实现接口的这个类.个人理解接口与类之间只是定义一种规则.接口内声明的方法,实现这个接口的类必须去实现接口里面的方法.假如在大型项目中有多人开发.可以有专门的人写好接口里面的方法.然后另外的人去写实现接口的类.可以做到分工明确.开发效率也会高点。好了.找到了实现接口的这个类.
2
3 //Static constants
4 private const string SQL_SELECT_PRODUCTS = "SELECT ProductId, Name, Descn FROM Product";
5 private const string SQL_SELECT_PRODUCTS_BY_CATEGORY = "SELECT ProductId, Name FROM Product WHERE Category = @Category";
6 private const string SQL_SELECT_PRODUCTS_BY_SEARCH1 = "SELECT ProductId, Name, Descn FROM Product WHERE ((";
7 private const string SQL_SELECT_PRODUCTS_BY_SEARCH2 = "LOWER(Name) LIKE '%' + {0} + '%' OR LOWER(Category) LIKE '%' + {0} + '%'";
8 private const string SQL_SELECT_PRODUCTS_BY_SEARCH3 = ") OR (";
9 private const string SQL_SELECT_PRODUCTS_BY_SEARCH4 = "))";
10 private const string PARM_CATEGORY = "@Category";
11 private const string PARM_KEYWORD = "@Keyword";
12
13 /// <summary>
14 /// Query for products by category
15 /// </summary>
16 /// <param name="category">category name</param>
17 /// <returns></returns>
18 public IList GetProductsByCategory(string category) {
19
20 IList productsByCategory = new ArrayList();
21
22 SqlParameter parm = new SqlParameter(PARM_CATEGORY, SqlDbType.Char, 10);
23 parm.Value = category;
24
25 //Execute a query to read the products
26 using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, SQL_SELECT_PRODUCTS_BY_CATEGORY, parm)) {
27 while (rdr.Read()){
28 ProductInfo product = new ProductInfo(rdr.GetString(0), rdr.GetString(1), null);
29 productsByCategory.Add(product);
30 }
31 }
32
33 return productsByCategory;
34 }
35
36 /// <summary>
37 /// Query for products by keywords.
38 /// The results will include any product where the keyword appears in the category name or product name
39 /// </summary>
40 /// <param name="keywords">string array of keywords</param>
41 /// <returns></returns>
42 public IList GetProductsBySearch(string[] keywords) {
43
44 IList productsBySearch = new ArrayList();
45
46 int numKeywords = keywords.Length;
47
48 //Create a new query string
49 StringBuilder sql = new StringBuilder(SQL_SELECT_PRODUCTS_BY_SEARCH1);
50
51 //Add each keyword to the query
52 for (int i = 0; i < numKeywords; i++) {
53 sql.Append(string.Format(SQL_SELECT_PRODUCTS_BY_SEARCH2, PARM_KEYWORD + i));
54 sql.Append(i + 1 < numKeywords ? SQL_SELECT_PRODUCTS_BY_SEARCH3 : SQL_SELECT_PRODUCTS_BY_SEARCH4);
55 }
56
57 string sqlProductsBySearch = sql.ToString();
58 SqlParameter[] parms = SQLHelper.GetCachedParameters(sqlProductsBySearch);
59
60 // If the parameters are null build a new set
61 if (parms == null) {
62 parms = new SqlParameter[numKeywords];
63
64 for (int i = 0; i < numKeywords; i++)
65 parms[i] = new SqlParameter(PARM_KEYWORD + i, SqlDbType.VarChar, 80);
66
67 SQLHelper.CacheParameters(sqlProductsBySearch, parms);
68 }
69
70 // Bind the new parameters
71 for (int i = 0; i < numKeywords; i++)
72 parms[i].Value = keywords[i];
73
74 //Finally execute the query
75 using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, sqlProductsBySearch, parms)) {
76 while (rdr.Read()){
77 ProductInfo product = new ProductInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2));
78 productsBySearch.Add(product);
79 }
80 }
81
82 return productsBySearch;
83 }
84 }
说实话,当初第一眼看里面的代码觉得眼花头昏,咋会这么乱呢.仔细一看.嗯.好处还是蛮多多的.仔细一分析,然后再对比以前自己写的东西.禁不住老脸都红了一下.呵呵。
2 private const string SQL_SELECT_PRODUCTS_BY_CATEGORY = "SELECT ProductId, Name FROM Product WHERE Category = @Category";
3 private const string SQL_SELECT_PRODUCTS_BY_SEARCH1 = "SELECT ProductId, Name, Descn FROM Product WHERE ((";
4 private const string SQL_SELECT_PRODUCTS_BY_SEARCH2 = "LOWER(Name) LIKE '%' + {0} + '%' OR LOWER(Category) LIKE '%' + {0} + '%'";
5 private const string SQL_SELECT_PRODUCTS_BY_SEARCH3 = ") OR (";
6 private const string SQL_SELECT_PRODUCTS_BY_SEARCH4 = "))";
7 private const string PARM_CATEGORY = "@Category";
8 private const string PARM_KEYWORD = "@Keyword";
上面这一段声明了对数据库操作所需的SQL语句.第一次编译后,下次直接调用.然后这个类实现了IDAL.IProduct这个类的方法.
Model这层主要是实现了实体层。
可能是语文小学的时候没学好的.表达得连我自己都觉得乱了呢.
web.config里面的
2 <add key="SQLConnString2" value="AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAiwM8Jc97B06fnNOg4m/cVAQAAAACAAAAAAADZgAAqAAAABAAAADBe+0s4RDV1kFfEajzSk6yAAAAAASAAACgAAAAEAAAAO5mVnnO8ixwTntVMZz1OyhIAAAAy+dStcznTM4kq705oDiKVp1TkUw4mozYbuvp+BNXr4bN3rPzVbvbjgeYNE9RHa69rxUppvtb/YDRbj1oV2hsu5tX9dtIHik/FAAAAOpCMs+ujLIaxSalUd+bwkpNNBe6" />
3 <add key="SQLConnString3" value="AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAiwM8Jc97B06fnNOg4m/cVAQAAAACAAAAAAADZgAAqAAAABAAAACnoyDL679IUCTKmdAuovLtAAAAAASAAACgAAAAEAAAAMuza8eq1dAanascPjy/tr5YAAAAcJK4ehxVfY931JX9iG3TkPCYDqbKbUZ0Pp/OFNqJBNr313ije2WrKdqfPXWcERENx57wQ/IjSAnljOUzNdWAW2HPVERq6eMsbsz+l0P8xFJEMjOTEpNXbBQAAAB7TXfLAKGq+4CZ5ISo8Yngg6xczw==" />
4 <add key="OraConnString1" value="" />
5 <add key="OraConnString2" value="" />
6 <add key="OraConnString3" value="" />
7 <add key="WebDAL" value="PetShop.SQLServerDAL" />
8 <add key="OrdersDAL" value="PetShop.SQLServerDAL" />
9 <add key="Event Log Source" value=".NET Pet Shop" />
主要是存放连库字符串.和Factory所需的信息.刚刚不是说了下面这一段的么
string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];
5 string className = path + ".Product";
6 //PetShop.SQLServerDAL.Product
7
8 // Using the evidence given in the config file load the appropriate assembly and class
9 return (PetShop.IDAL.IProduct)Assembly.Load(path).CreateInstance(className);
只要把WebDAL改成OracleDAL就可以实现针对ORACLE数据库的操作.实现了多数据.娘的.真好.为啥我以前就没有想到呢.所以说书看得多.不如分析实例.边想边写.
还有差不多它的返回结果都IList类型.主要是用DataReader执行后的结果返回个另外的对象.这样做的话可以节省资源,小巧.不像自己以前动不动返回一个DataSet,要是应用到大项目中.不把服务器卡死才怪呢.