[轉]ASP.NET 2.0新特性在PetShop4中的應用1

ASP.NET 2.0新特性

由於PetShop 4.0是基於.NET Framework 2.0平臺開發的電子商務系統,因而它在表示層也引入了許多ASP.NET 2.0的新特性,例如MemberShip、Profile、Master Page、登錄控件等特性。接下來,我將結合PetShop 4.0的設計分別介紹它們的實現。

6.4.1    Profile特性

Profile提供的功能是針對用戶的個性化服務。在ASP.NET 1.x版本時,我們可以利用Session、Cookie等方法來存儲用戶的狀態信息。然而Session對象是具有生存期的,一旦生存期結束,該對象保留的值就會失效。Cookie將用戶信息保存在客戶端,它具有一定的安全隱患,一些重要的信息不能存儲在Cookie中。一旦客戶端禁止使用 Cookie,則該功能就將失去應用的作用。

Profile的出現解決了如上的煩惱,它可以將用戶的個人化信息保存在指定的數據庫中。ASP.NET 2.0的Profile功能默認支持Access數據庫和SQL Server數據庫,如果需要支持其他數據庫,可以編寫相關的ProfileProvider類。Profile對象是強類型的,我們可以爲用戶信息建立屬性,以PetShop 4.0爲例,它建立了ShoppingCart、WishList和AccountInfo屬性。

由於Profile功能需要訪問數據庫,因而在數據訪問層(DAL)定義了和Product等數據表相似的模塊結構。首先定義了一個IProfileDAL接口模塊,包含了接口IPetShopProfileProvider:

public interface IPetShopProfileProvider
{
AddressInfo GetAccountInfo(
string userName, string appName);   
void SetAccountInfo(int uniqueID, AddressInfo addressInfo);
IList
<CartItemInfo> GetCartItems(string userName, string appName,
bool isShoppingCart);
void SetCartItems(int uniqueID, ICollection<CartItemInfo> cartItems,
bool isShoppingCart);
void UpdateActivityDates(string userName, bool activityOnly, string appName);
int GetUniqueID(string userName, bool isAuthenticated, bool ignoreAuthenticationType,
string appName);
int CreateProfileForUser(string userName, bool isAuthenticated, string appName);
IList
<string> GetInactiveProfiles(int authenticationOption,
DateTime userInactiveSinceDate,
string appName);
bool DeleteProfile(string userName, string appName);   
IList
<CustomProfileInfo> GetProfileInfo(int authenticationOption,
string usernameToMatch, DateTime userInactiveSinceDate, string appName,
out int totalRecords);
}

因爲PetShop 4.0版本分別支持SQL Server和Oracle數據庫,因而它分別定義了兩個不同的PetShopProfileProvider類,實現 IPetShopProfileProvider接口,並放在兩個不同的模塊SQLProfileDAL和OracleProfileDAL中。具體的實現請參見PetShop 4.0的源代碼。
同樣的,PetShop 4.0爲Profile引入了工廠模式,定義了模塊ProfileDALFActory,工廠類DataAccess的定義如下:

public sealed class DataAccess {

    
private static readonly string profilePath = ConfigurationManager.AppSettings["ProfileDAL"];
    
public static PetShop.IProfileDAL.IPetShopProfileProvider CreatePetShopProfileProvider() {
string className = profilePath + ".PetShopProfileProvider";
return (PetShop.IProfileDAL.IPetShopProfileProvider)Assembly.Load(profilePath).CreateInstance(className);
      }

}

在業務邏輯層(BLL)中,單獨定義了模塊Profile,它添加了對BLL、IProfileDAL和ProfileDALFactory模塊的程序集。在該模塊中,定義了密封類PetShopProfileProvider,它繼承自 System.Web.Profile.ProfileProvider類,該類作爲Profile的Provider基類,用於在自定義配置文件中實現相關的配置文件服務。在PetShopProfileProvider類中,重寫了父類ProfileProvider中的一些方法,例如 Initialize()、GetPropertyValues()、SetPropertyValues()、DeleteProfiles()等方法。此外,還爲ShoppingCart、WishList、AccountInfo屬性提供了Get和Set方法。至於Provider的具體實現,則調用工廠類DataAccess創建的具體類型對象,如下所示:
private static readonly IPetShopProfileProvider dal = DataAccess.CreatePetShopProfileProvider();

定義了PetShop.Profile.PetShopProfileProvider類後,纔可以在web.config配置文件中配置如下的配置節:

<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>

在配置文件中,針對ShoppingCart、WishList和AccountInfo(它們的類型分別爲PetShop.BLL.Cart、 PetShop.BLL.Cart、PetShop.Model.AddressInfo)屬性分別定義了ShoppingCartProvider、 WishListProvider、AccountInfoProvider,它們的類型均爲 PetShop.Profile.PetShopProfileProvider類型。至於Profile的信息究竟是存儲在何種類型的數據庫中,則由以下的配置節決定:
<add key="ProfileDAL" value="PetShop.SQLProfileDAL"/>

而鍵值爲ProfileDAL的值,正是Profile的工廠類PetShop.ProfileDALFactory.DataAccess在利用反射技術創建IPetShopProfileProvider類型對象時獲取的。

在表示層中,可以利用頁面的Profile屬性訪問用戶的個性化屬性,例如在ShoppingCart頁面的codebehind代碼ShoppingCart.aspx.cs中,調用Profile的ShoppingCart屬性:

public partial class ShoppingCart : System.Web.UI.Page {

    
protected void Page_PreInit(object sender, EventArgs e) {
        
if (!IsPostBack) {
            
string itemId = Request.QueryString["addItem"];
            
if (!string.IsNullOrEmpty(itemId)) {
                  Profile.ShoppingCart.Add(itemId);
                  Profile.Save();
                
// Redirect to prevent duplictations in the cart if user hits "Refresh"
                  Response.Redirect("~/ShoppingCart.aspx", true);
              }

          }

      }

}

在上述的代碼中,Profile屬性的值從何而來?實際上,在我們爲web.config配置文件中對Profile進行配置後,啓動Web應用程序,ASP.NET會根據該配置文件中的相關配置創建一個ProfileCommon類的實例。該類繼承自 System.Web.Profile.ProfileBase類。然後調用從父類繼承來的GetPropertyValue和 SetPropertyValue方法,檢索和設置配置文件的屬性值。然後,ASP.NET將創建好的ProfileCommon實例設置爲頁面的 Profile屬性值。因而,我們可以通過智能感知獲取Profile的ShoppingCart屬性,同時也可以利用ProfileCommon繼承自 ProfileBase類的Save()方法,根據屬性值更新Profile的數據源。

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章