1. 程式人生 > >petshop4.0 詳解

petshop4.0 詳解

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

publicinterface 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, 
outint totalRecords);
}

因為PetShop 4.0版本分別支援SQL Server和Oracle資料庫,因而它分別定義了兩個不同的PetShopProfileProvider類,實現IPetShopProfileProvider介面,並放在兩個不同的模組SQLProfileDAL和OracleProfileDAL中。具體的實現請參見PetShop 4.0的原始碼。
同樣的,PetShop 4.0為Profile引入了工廠模式,定義了模組ProfileDALFActory,工廠類DataAccess的定義如下:

publicsealedclass DataAccess {

    
privatestaticreadonlystring profilePath = ConfigurationManager.AppSettings["ProfileDAL"];
    
publicstatic 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 {

    
protectedvoid 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的資料來源。

6.4.2  Membership特性

PetShop 4.0並沒有利用Membership的高階功能,而是直接讓Membership特性和ASP.NET 2.0新增的登入控制元件進行繫結。由於.NET Framework 2.0已經定義了針對SQL Server的SqlMembershipProvider,因此對於PetShop 4.0而言,實現Membership比之實現Profile要簡單,僅僅需要為Oracle資料庫定義MembershipProvider即可。在PetShop.Membership模組中,定義了OracleMembershipProvider類,它繼承自System.Web.Security.MembershipProvider抽象類。

OracleMembershipProvider類的實現具有極高的參考價值,如果我們需要定義自己的MembershipProvider類,可以參考該類的實現。
事實上OracleMemberShip類的實現並不複雜,在該類中,主要是針對使用者及使用者安全而實現相關的行為。由於在父類MembershipProvider中,已經定義了相關操作的虛方法,因此我們需要作的是重寫這些虛方法。由於與Membership有關的資訊都是儲存在資料庫中,因而OracleMembershipProvider與SqlMembershipProvider類的主要區別還是在於對資料庫的訪問。對於SQL Server而言,我們利用aspnet_regsql工具為Membership建立了相關的資料表以及儲存過程。也許是因為智慧財產權的原因,Microsoft並沒有為Oracle資料庫提供類似的工具,因而需要我們自己去建立membership的資料表。此外,由於沒有建立Oracle資料庫的儲存過程,因而OracleMembershipProvider類中的實現是直接呼叫SQL語句。以CreateUser()方法為例,剔除那些繁雜的引數判斷與安全性判斷,SqlMembershipProvider類的實現如下:

publicoverride MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
      MembershipUser user1;
      
//前面的程式碼略;
try
      
{
            SqlConnectionHolder holder1 
=null;
            
try
            
{
                  holder1 
= SqlConnectionHelper.GetConnection(this._sqlConnectionString, true);
                  
this.CheckSchemaVersion(holder1.Connection);
                  DateTime time1 
=this.RoundToSeconds(DateTime.UtcNow);
                  SqlCommand command1 
=new SqlCommand("dbo.aspnet_Membership_CreateUser", holder1.Connection);
                  command1.CommandTimeout 
=this.CommandTimeout;
                  command1.CommandType 
= CommandType.StoredProcedure;
                  command1.Parameters.Add(
this.CreateInputParam("@ApplicationName", SqlDbType.NVarChar, this.ApplicationName));
                  command1.Parameters.Add(
this.CreateInputParam("@UserName", SqlDbType.NVarChar, username));
                  command1.Parameters.Add(
this.CreateInputParam("@Password", SqlDbType.NVarChar, text2));
                  command1.Parameters.Add(
this.CreateInputParam("@PasswordSalt", SqlDbType.NVarChar, text1));
                  command1.Parameters.Add(
this.CreateInputParam("@Email", SqlDbType.NVarChar, email));
                  command1.Parameters.Add(
this.CreateInputParam("@PasswordQuestion", SqlDbType.NVarChar, passwordQuestion));
                  command1.Parameters.Add(
this.CreateInputParam("@PasswordAnswer", SqlDbType.NVarChar, text3));
                  command1.Parameters.Add(
this.CreateInputParam("@IsApproved", SqlDbType.Bit, isApproved));
                  command1.Parameters.Add(
this.CreateInputParam("@UniqueEmail", SqlDbType.Int, this.RequiresUniqueEmail ?1 : 0));
                  command1.Parameters.Add(
this.CreateInputParam("@PasswordFormat", SqlDbType.Int, (intthis.PasswordFormat));
                  command1.Parameters.Add(
this.CreateInputParam("@CurrentTimeUtc", SqlDbType.DateTime, time1));
                  SqlParameter parameter1 
=this.CreateInputParam("@UserId", SqlDbType.UniqueIdentifier, providerUserKey);
                  parameter1.Direction 
= ParameterDirection.InputOutput;
                  command1.Parameters.Add(parameter1);
                  parameter1 
=new SqlParameter("@ReturnValue", SqlDbType.Int);
                  parameter1.Direction 
= ParameterDirection.ReturnValue;
                  command1.Parameters.Add(parameter1);
                  command1.ExecuteNonQuery();
                  
int num3 = (parameter1.Value !=null? ((int) parameter1.Value) : -1;
                  
if ((num3 <0|| (num3 >11))
                  
{
                        num3 
=11;
                  }

                  status 
= (MembershipCreateStatus) num3;
                  
if (num3 !=0)
                  
{
                        
returnnull;
                  }

                  providerUserKey 
=new Guid(command1.Parameters["@UserId"].Value.ToString());
                  time1 
= time1.ToLocalTime();
                  user1 
=new MembershipUser(this.Name, username, providerUserKey, email, passwordQuestion, null, isApproved, false, time1, time1, time1, time1, new DateTime(0x6da11));
            }

            
finally
            
{
                  
if (holder1 !=null)
                  
{
                        holder1.Close();
                        holder1 
=null;
                  }

            }

      }

      
catch
      
{
            
throw;
      }

      
return user1;
}

程式碼中,aspnet_Membership_CreateUser為aspnet_regsql工具為membership建立的儲存過程,它的功能就是建立一個使用者。

OracleMembershipProvider類中對CreateUser()方法的定義如下:

publicoverride MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object userId, out MembershipCreateStatus status) {
    
//前面的程式碼略;
 
//Create connection
 OracleConnection connection =new OracleConnection(OracleHelper.ConnectionStringMembership);
 connection.Open();
 OracleTransaction transaction 
= connection.BeginTransaction(IsolationLevel.ReadCommitted);
 
try{
  DateTime dt 
= DateTime.Now;
  
bool isUserNew =true;

  
// Step 1: Check if the user exists in the Users table: create if not    
int uid = GetUserID(transaction, applicationId, username, truefalse, dt, out isUserNew);
  
if(uid ==0// User not created successfully!
   status = MembershipCreateStatus.ProviderError;
   
returnnull;
  }

  
// Step 2: Check if the user exists in the Membership table: Error if yes.
if(IsUserInMembership(transaction, uid)) {
   status 
= MembershipCreateStatus.DuplicateUserName;
   
returnnull;
  }

  
// Step 3: Check if Email is duplicate
if(IsEmailInMembership(transaction, email, applicationId)) {
   status 
= MembershipCreateStatus.DuplicateEmail;
   
returnnull;
  }

  
// Step 4: Create user in Membership table     
int pFormat = (int)passwordFormat;
  
if(!InsertUser(transaction, uid, email, pass, pFormat, salt, """", isApproved, dt)) {
   status 
= MembershipCreateStatus.ProviderError;
   
returnnull;
  }

  
// Step 5: Update activity date if user is not new
if(!isUserNew) {
   
if(!UpdateLastActivityDate(transaction, uid, dt)) {
    status 
= MembershipCreateStatus.ProviderError;
    
returnnull;
   }

  }

  status 
= MembershipCreateStatus.Success;
  
returnnew MembershipUser(this.Name, username, uid, email, passwordQuestion, null, isApproved, false, dt, dt, dt, dt, DateTime.MinValue);
 }

 
catch(Exception) {
  
if(status == MembershipCreateStatus.Success)
   status 
= MembershipCreateStatus.ProviderError;
  
throw;
 }

 
finally{
  
if(status == MembershipCreateStatus.Success)
   transaction.Commit();
  
else
   transaction.Rollback();
  connection.Close();
  connection.Dispose();
 }

}