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:
public
interface
IPetShopProfileProvider
{
AddressInfoGetAccountInfo(
string
userName,
string
appName);
void
SetAccountInfo(
int
uniqueID,AddressInfoaddressInfo);
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,
DateTimeuserInactiveSinceDate,
string
appName);
bool
DeleteProfile(
string
userName,
string
appName);
IList
<
CustomProfileInfo
>
GetProfileInfo(
int
authenticationOption,
string
usernameToMatch,DateTimeuserInactiveSinceDate,
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.IPetShopProfileProviderCreatePetShopProfileProvider()
{
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配置文件中配置如下的配置節:
<
profileautomaticSaveEnabled
=
"
false
"
defaultProvider
=
"
ShoppingCartProvider
"
>
<
providers
>
<
addname
=
"
ShoppingCartProvider
"
connectionStringName
=
"
SQLProfileConnString
"
type
=
"
PetShop.Profile.PetShopProfileProvider
"
applicationName
=
"
.NETPetShop4.0
"
/>
<
addname
=
"
WishListProvider
"
connectionStringName
=
"
SQLProfileConnString
"
type
=
"
PetShop.Profile.PetShopProfileProvider
"
applicationName
=
"
.NETPetShop4.0
"
/>
<
addname
=
"
AccountInfoProvider
"
connectionStringName
=
"
SQLProfileConnString
"
type
=
"
PetShop.Profile.PetShopProfileProvider
"
applicationName
=
"
.NETPetShop4.0
"
/>
</
providers
>
<
properties
>
<
addname
=
"
ShoppingCart
"
type
=
"
PetShop.BLL.Cart
"
allowAnonymous
=
"
true
"
provider
=
"
ShoppingCartProvider
"
/>
<
addname
=
"
WishList
"
type
=
"
PetShop.BLL.Cart
"
allowAnonymous
=
"
true
"
provider
=
"
WishListProvider
"
/>
<
addname
=
"
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,EventArgse)
{
if
(
!
IsPostBack)
{
string
itemId
=
Request.QueryString[
"
addItem
"
];
if
(
!
string
.IsNullOrEmpty(itemId))
{
Profile.ShoppingCart.Add(itemId);
Profile.Save();
//
Redirecttopreventduplictationsinthecartifuserhits"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類的實現如下:
public
override
MembershipUserCreateUser(
string
username,
string
password,
string
email,
string
passwordQuestion,
string
passwordAnswer,
bool
isApproved,
object
providerUserKey,
out
MembershipCreateStatusstatus)
{
MembershipUseruser1;
//
前面的代碼略;
try
{
SqlConnectionHolderholder1
=
null
;
try
{
holder1
=
SqlConnectionHelper.GetConnection(
this
._sqlConnectionString,
true
);
this
.CheckSchemaVersion(holder1.Connection);
DateTimetime1
=
this
.RoundToSeconds(DateTime.UtcNow);
SqlCommandcommand1
=
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,(
int
)
this
.PasswordFormat));
command1.Parameters.Add(
this
.CreateInputParam(
"
@CurrentTimeUtc
"
,SqlDbType.DateTime,time1));
SqlParameterparameter1
=
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
)
{
return
null
;
}
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(
0x6da
,
1
,
1
));
}
finally
{
if
(holder1
!=
null
)
{
holder1.Close();
holder1
=
null
;
}
}
}
catch
{
throw
;
}
return
user1;
}
代碼中,aspnet_Membership_CreateUser為aspnet_regsql工具為membership創建的存儲過程,它的功能就是創建一個用戶。
OracleMembershipProvider類中對CreateUser()方法的定義如下:
public
override
MembershipUserCreateUser(
string
username,
string
password,
string
email,
string
passwordQuestion,
string
passwordAnswer,
bool
isApproved,
object
userId,
out
MembershipCreateStatusstatus)
{
//
前面的代碼略;
//
Createconnection
OracleConnectionconnection
=
new
OracleConnection(OracleHelper.ConnectionStringMembership);
connection.Open();
OracleTransactiontransaction
=
connection.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
DateTimedt
=
DateTime.Now;
bool
isUserNew
=
true
;
//
Step1:CheckiftheuserexistsintheUserstable:createifnot
int
uid
=
GetUserID(transaction,applicationId,username,
true
,
false
,dt,
out
isUserNew);
if
(uid
==
0
)
{
//
Usernotcreatedsuccessfully!
status
=
MembershipCreateStatus.ProviderError;
return
null
;
}
//
Step2:CheckiftheuserexistsintheMembershiptable:Errorifyes.
if
(IsUserInMembership(transaction,uid))
{
status
=
MembershipCreateStatus.DuplicateUserName;
return
null
;
}
//
Step3:CheckifEmailisduplicate
if
(IsEmailInMembership(transaction,email,applicationId))
{
status
=
MembershipCreateStatus.DuplicateEmail;
return
null
;
}
//
Step4:CreateuserinMembershiptable
<
分享到:
petshop4.0 詳解之七(PetShop表示層設計)
評論