PetShop4
简析
跟踪顺序为
1.Web/Controls/ItemsControl.ascx.cs
2./BLL/Item.cs (此处用工厂实现下面的Item)
3./IDAL/IItem.cs
/DALFactory/DataAccess.cs(工厂)
/Web/web.config(path)
/SQLServerDAL/Item.cs(IItem的实现类)
具体代码实现如下(UI->BLL->DAL)
Web/Controls/ItemsControl.ascx.cs
|
using
System;
using
System.Web;
using
System.Web.UI.WebControls;
using
PetShop.BLL;
using
PetShop.CacheDependencyFactory;
namespace
PetShop.Web {
public partial class ItemsControl : System.Web.UI.UserControl {
///<summary>
/// Rebind control
///</summary>
protected void PageChanged(object sender, DataGridPageChangedEventArgs e) {
//reset index
itemsGrid.CurrentPageIndex = e.NewPageIndex;
//get category id
string productKey = Request.QueryString["productId"];
//bind data
Item item = new Item();
itemsGrid.DataSource = item.GetItemsByProduct(productKey);
itemsGrid.DataBind();
}
}
}
|
/BLL/Item.cs
|
using
System.Collections.Generic;
using
PetShop.Model;
using
PetShop.IDAL;
namespace
PetShop.BLL {
///<summary>
/// A business component to manage product items
///</summary>
public class Item {
// Get an instance of the Item DAL using the DALFactory
// Making this static will cache the DAL instance after the initial load
private static readonly IItem dal = PetShop.DALFactory.DataAccess.CreateItem();
///<summary>
/// A method to list items by productId
/// Every item is associated with a parent product
///</summary>
///<param name="productId">The productId to search by</param>
///<returns>A Generic List of ItemInfo</returns>
public IList<ItemInfo> GetItemsByProduct(string productId) {
// Validate input
if(string.IsNullOrEmpty(productId))
return new List<ItemInfo>();
// Use the dal to search by productId
return dal.GetItemsByProduct(productId);
}
///<summary>
/// Search for an item given it's unique identifier
///</summary>
///<param name="itemId">Unique identifier for an item</param>
///<returns>An Item business entity</returns>
public ItemInfo GetItem(string itemId) {
// Validate input
if (string.IsNullOrEmpty(itemId))
return null;
// Use the dal to search by ItemId
|
return dal.GetItem(itemId);
}
}
}
|
/IDAL/IItem.cs
|
using
System;
using
System.Collections.Generic;
//References to PetShop specific libraries
//PetShop busines entity library
using
PetShop.Model;
namespace
PetShop.IDAL {
///<summary>
/// Interface to the Item DAL
///</summary>
public interface IItem{
///<summary>
/// Search items by productId
///</summary>
///<param name="productId">ProductId to search for</param>
///<returns>Interface to Model Collection Generic of the results</returns>
IList<ItemInfo> GetItemsByProduct(string productId);
///<summary>
/// Get information on a specific item
///</summary>
///<param name="itemId">Unique identifier for an item</param>
///<returns>Business Entity representing an item</returns>
ItemInfo GetItem(string itemId);
}
}
|
/DALFactory/DataAccess.cs
|
using
System.Reflection;
using
System.Configuration;
namespace
PetShop.DALFactory {
///<summary>
/// This class is implemented following the Abstract Factory pattern to create the DAL implementation
/// specified from the configuration file
///</summary>
public sealed class DataAccess {
// Look up the DAL implementation we should be using
private static readonly string path = ConfigurationManager.AppSettings["WebDAL"];
private static readonly string orderPath = ConfigurationManager.AppSettings["OrdersDAL"];
private DataAccess() { }
public static PetShop.IDAL.ICategory CreateCategory() {
string className = path + ".Category";
return (PetShop.IDAL.ICategory)Assembly.Load(path).CreateInstance(className);
}
public static PetShop.IDAL.IInventory CreateInventory() {
string className = path + ".Inventory";
return (PetShop.IDAL.IInventory)Assembly.Load(path).CreateInstance(className);
}
public static PetShop.IDAL.IItem CreateItem() {
string className = path + ".Item";
return (PetShop.IDAL.IItem)Assembly.Load(path).CreateInstance(className);
}
public static PetShop.IDAL.IOrder CreateOrder() {
string className = orderPath + ".Order";
return (PetShop.IDAL.IOrder)Assembly.Load(orderPath).CreateInstance(className);
}
public static PetShop.IDAL.IProduct CreateProduct() {
string className = path + ".Product";
return (PetShop.IDAL.IProduct)Assembly.Load(path).CreateInstance(className);
}
|
}
}
|
/Web/web.config
|
<?
xml
version
=
"1.0"?>
<
configuration
xmlns
=
"http://schemas.microsoft.com/.NetConfiguration/v2.0">
<
connectionStrings
configProtectionProvider
=
"RsaProtectedConfigurationProvider">
<
EncryptedData
Type
=
"http://www.w3.org/2001/04/xmlenc#Element"xmlns="http://www.w3.org/2001/04/xmlenc#">
<
EncryptionMethod
Algorithm
=
"http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/>
<
KeyInfo
xmlns
=
"http://www.w3.org/2000/09/xmldsig#">
<
EncryptedKey
xmlns
=
"http://www.w3.org/2001/04/xmlenc#">
<
EncryptionMethod
Algorithm
=
"http://www.w3.org/2001/04/xmlenc#rsa-1_5"/>
<
KeyInfo
xmlns
=
"http://www.w3.org/2000/09/xmldsig#">
<
KeyName
>
Rsa Key</KeyName>
</
KeyInfo
>
<
CipherData
>
<
CipherValue
>
NrTRoEqZ1heJDQKp5vLf/vfn+JwusYDwL7c9QFU3FJTeRikKLKaT5Rsa27NzCuU3N9ClxOXBc8hz8h9xO/2LRczJGqAopjhNF77YCV3n28oAXZEC44ePtUQpDhgq9u5EkLkRrBR8zuXidyhU5g0ORGYXtse05jRsZLiNWRpoAyg=</CipherValue>
</
CipherData
>
</
EncryptedKey
>
</
KeyInfo
>
<
CipherData
>
<
CipherValue
>
RcGv/XHN0pAu9yJ9qQ47L1o/o0M2Fe1S54oDDerxNN/zvG0DDz3jl9rq0vQ4Hlrksr5uOCtDnPuD7MA69Kowd0X7mP6OSNQ4Oc2ZKU9wbbusi9xXcIYvGcc1TPxIUwVT7h+1EzpGfvCDSfWVm0aJWo5Wvf4jJGla9jh1m38nq17NJW1KRlCbaIMDl+0V5keOLrFiAVJR4m4pmQTM90e1NqvJDmIOyWt8vAZHre5+HXk89SIPwFyDhQylLmhRxxqOtvhPLRMYIWGk0F2VjGaslxI1dINxp+zCRZZAQJ1+cKFnQVDnd96xuTk24bpjKMlyR86N1gD1pVfsrgPfbTQInzh1WBEePkHNDGhtte/DerNJlssTbXXiH2IlHvp9CIkgWDXRg9QSdwTLBTwqt/islRPgQgPethFT2pezOW4Bqa5L6TyZ6KW9EJ6wwW5Q1/D/9gTbYQx6cN60cv/RaUSsNzexIIX4jIM5ObSWjdb+06kcm0ZOzgGNtmzY0EL7i902pYSJj7/rOdinVnxisop1YcThacDM4JTMyM/SCEE6dor9/OBP3WL6sdArrzGL3XSgPLgsJpzq4m4efyR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yQhXhYb6T4opj+nuaAiO5SZ1FfcK6vhfoRlrwcoI5l4nmpcpbq3Ta80ZrnPiFI0ZrS/wrhfSPCPE8Z81hpcfrxp377GLNv+IZs9ItzhSrNXnggBmWvMHjtLK6z/VHgJylTfDji89XrJ/19lGDo1DMrIV7VJ8plzVq/sHTSpQJnTHlUa6234ffFOsWxN4jd4CbTXKIJqmWWsrm4r9ZoFIoFPaAiGcvSML8vVFOUPemvGJd9XgVghv5YOvty2efLqT7g/OsEfniVFZqSQ8nQPa+0Ek9YV9otks9+winTyZv8MZI6WOzIM+TtsPicFSzjKKa4FMVzsSSBC6SBP1HcUc0NYMnPIYXEoTyZQzi7lHCUEgvAQfIZtPXus7wkAmyS5WrnBQ5tKefXDoSTeaKJL/OFPRZskOTQ5pOTqxnhSWGF/lips6Do6zuW3Y+craa/xjfD/0+SxWeLJ7zt1E0=</CipherValue>
</
CipherData
>
</
EncryptedData
>
</
connectionStrings
>
<
appSettings
>
<!--
Pet Shop DAL configuration settings. Possible values: PetShop.SQLServerDAL for SqlServer, PetShop.OracleServerDALfor Oracle.
-->
<
add
key
=
"WebDAL"value="PetShop.SQLServerDAL"/>
<
add
key
=
"OrdersDAL"value="PetShop.SQLServerDAL"/>
<
add
key
=
"ProfileDAL"value="PetShop.SQLProfileDAL"/>
<!--
Enable data caching
-->
<
add
key
=
"EnableCaching"value="true"/>
<!--
Cache duration (in hours-whole number only)
-->
<
add
key
=
"CategoryCacheDuration"value="12"/>
<
add
key
=
"ProductCacheDuration"value="12"/>
<
add
key
=
"ItemCacheDuration"value="12"/>
<!--
Cache dependency options. Possible values: PetShop.TableCacheDependency for SQL Server and keep empty for ORACLE
-->
<
add
key
=
"CacheDependencyAssembly"value="PetShop.TableCacheDependency"/>
<!--
CacheDatabaseName should match the name under caching section, when using TableCacheDependency
-->
<
add
key
=
"CacheDatabaseName"value="MSPetShop4"/>
<!--
*TableDependency lists table dependency for each instance separated by comma
-->
<
add
key
=
"CategoryTableDependency"value="Category"/>
<
add
key
=
"ProductTableDependency"value="Product,Category"/>
<
add
key
=
"ItemTableDependency"value="Product,Category,Item"/>
<!--
Order processing options (Asynch/Synch)
-->
<
add
key
=
"OrderStrategyAssembly"value="PetShop.BLL"/>
<
add
key
=
"OrderStrategyClass"value="PetShop.BLL.OrderSynchronous"/>
<!--
Asynchronous Order options
-->
<
add
key
=
"OrderMessaging"value="PetShop.MSMQMessaging"/>
<
add
key
=
"OrderQueuePath"value="FormatName:DIRECT=OS:MachineName/Private$/PSOrders"/>
<!--
Application Error Log
-->
<
add
key
=
"Event Log Source"value=".NET Pet Shop 4.0"/>
</
appSettings
>
<
system.web
>
<
pages
theme
=
"PetShop"styleSheetTheme="PetShop"/>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<
compilation
debug
=
"false">
<
assemblies
>
<
add
assembly
=
"System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<
add
assembly
=
"System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<
add
assembly
=
"Accessibility, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<
add
assembly
=
"System.Messaging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<
authentication
mode
=
"Forms">
<
forms
name
=
"PetShopAuth"loginUrl="SignIn.aspx"protection="None"timeout="60"/>
</
authentication
>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
-->
<
customErrors
defaultRedirect
=
"Error.aspx"mode="RemoteOnly"/>
<
sessionState
mode
=
"Off"/>
<
anonymousIdentification
enabled
=
"true"/>
<
profile
automaticSaveEnabled
=
"false"defaultProvider="ShoppingCartProvider">
<
providers
>
<
add
name
=
"ShoppingCartProvider"connectionStringName="SQLProfileConnString"type="PetShop.Profile.PetShopProfileProvider"applicationName=".NET Pet Shop 4.0"/>
<
add
name
=
"WishListProvider"connectionStringName="SQLProfileConnString"type="PetShop.Profile.PetShopProfileProvider"applicationName=".NET Pet Shop 4.0"/>
<
add
name
=
"AccountInfoProvider"connectionStringName="SQLProfileConnString"type="PetShop.Profile.PetShopProfileProvider"applicationName=".NET Pet Shop 4.0"/>
</
providers
>
<
properties
>
<
add
name
=
"ShoppingCart"type="PetShop.BLL.Cart"allowAnonymous="true"provider="ShoppingCartProvider"/>
<
add
name
=
"WishList"type="PetShop.BLL.Cart"allowAnonymous="true"provider="WishListProvider"/>
<
add
name
=
"AccountInfo"type="PetShop.Model.AddressInfo"allowAnonymous="false"provider="AccountInfoProvider"/>
</
properties
>
</
profile
>
<!--
Membership Provider for SqlServer
-->
<
membership
defaultProvider
=
"SQLMembershipProvider">
<
providers
>
<
add
name
=
"SQLMembershipProvider"type="System.Web.Security.SqlMembershipProvider"connectionStringName="SQLMembershipConnString"applicationName=".NET Pet Shop 4.0"enablePasswordRetrieval="false"enablePasswordReset="true"requiresQuestionAndAnswer="false"requiresUniqueEmail="false"passwordFormat="Hashed"/>
</
providers
>
</
membership
>
<!--
Membership Provider for Oracle
-->
<!--
<membership defaultProvider="OracleMembershipProvider">
<providers>
<clear/>
<add name="OracleMembershipProvider"
type="PetShop.Membership.OracleMembershipProvider"
connectionStringName="OraMembershipConnString"
enablePasswordRetrieval="false"
enablePasswordReset="false"
requiresUniqueEmail="false"
requiresQuestionAndAnswer="false"
minRequiredPasswordLength="7"
minRequiredNonalphanumericCharacters="1"
applicationName=".NET Pet Shop 4.0"
hashAlgorithmType="SHA1"
passwordFormat="Hashed"/>
</providers>
</membership>
-->
<
caching
>
<
sqlCacheDependency
enabled
=
"true"pollTime="10000">
<
databases
>
<
add
name
=
"MSPetShop4"connectionStringName="SQLConnString1"pollTime="10000"/>
</
databases
>
</
sqlCacheDependency
>
</
caching
>
</
system.web
>
<
location
path
=
"UserProfile.aspx">
<
system.web
>
<
authorization
>
<
deny
users
=
"?"/>
</
authorization
>
</
system.web
>
</
location
>
<
location
path
=
"CheckOut.aspx">
<
system.web
>
<
authorization
>
<
deny
users
=
"?"/>
</
authorization
>
</
system.web
>
</
location
>
</
configuration
>
|
/SQLServerDAL/Item.cs
(
IItem
的实现类)
|
using
System;
using
System.Data;
using
System.Data.SqlClient;
using
PetShop.Model;
using
PetShop.IDAL;
using
System.Collections.Generic;
using
PetShop.DBUtility;
namespace
PetShop.SQLServerDAL {
public class Item : IItem {
// Static constants
private const string SQL_SELECT_ITEMS_BY_PRODUCT = "SELECT Item.ItemId, Item.Name, Inventory.Qty, Item.ListPrice, Product.Name, Item.Image, Product.CategoryId, Product.ProductId FROM Item INNER JOIN Product ON Item.ProductId = Product.ProductId INNER JOIN Inventory ON Item.ItemId = Inventory.ItemId WHERE Item.ProductId = @ProductId";
private const string SQL_SELECT_ITEM = "SELECT Item.ItemId, Item.Name, Item.ListPrice, Product.Name, Item.Image, Product.CategoryId, Product.ProductId FROM Item INNER JOIN Product ON Item.ProductId = Product.ProductId WHERE Item.ItemId = @ItemId";
private const string PARM_PRODUCT_ID = "@ProductId";
private const string PARM_ITEM_ID = "@ItemId";
///<summary>
/// Function to get a list of items within a product group
///</summary>
///<param name="productId">Product Id</param>
///<returns>A Generic List of ItemInfo</returns>
public IList<ItemInfo> GetItemsByProduct(string productId) {
IList<ItemInfo> itemsByProduct = new List<ItemInfo>();
SqlParameter parm = new SqlParameter(PARM_PRODUCT_ID, SqlDbType.VarChar, 10);
parm.Value = productId;
//Execute the query against the database
using(SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ITEMS_BY_PRODUCT, parm)) {
// Scroll through the results
while (rdr.Read()) {
ItemInfo item = new ItemInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetInt32(2), rdr.GetDecimal(3), rdr.GetString(4), rdr.GetString(5), rdr.GetString(6), rdr.GetString(7));
//Add each item to the arraylist
itemsByProduct.Add(item);
}
}
return itemsByProduct;
}
///<summary>
/// Get an individual item based on a unique key
///</summary>
///<param name="itemId">unique key</param>
///<returns>Details about the Item</returns>
public ItemInfo GetItem(string itemId) {
//Set up a return value
ItemInfo item = null;
//Create a parameter
SqlParameter parm = new SqlParameter(PARM_ITEM_ID, SqlDbType.VarChar, 10);
//Bind the parameter
parm.Value = itemId;
//Execute the query
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ITEM, parm)) {
if (rdr.Read())
item = new ItemInfo(rdr.GetString(0), rdr.GetString(1), 0, rdr.GetDecimal(2), rdr.GetString(3), rdr.GetString(4), rdr.GetString(5), rdr.GetString(6));
else
item = new ItemInfo();
}
return item;
}
///<summary>
/// Get the SqlCommand used to retrieve a list of items by product
///</summary>
///<param name="id">Product id</param>
///<returns>Sql Command object used to retrieve the data</returns>
public static SqlCommand GetCommand(string id) {
//Create a parameter
SqlParameter parm = new SqlParameter(PARM_PRODUCT_ID, SqlDbType.VarChar, 10);
parm.Value = id;
// Create and return SqlCommand object
SqlCommand command = new SqlCommand(SQL_SELECT_ITEMS_BY_PRODUCT);
command.Parameters.Add(parm);
return command;
}
}
}
|