日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

petshop4.0 詳解之七(PetShop表示層設(shè)計(jì))

系統(tǒng) 1714 0

6.4 ASP.NET 2.0新特性

由于PetShop 4.0是基于.NET Framework 2.0平臺(tái)開發(fā)的電子商務(wù)系統(tǒng),因而它在表示層也引入了許多ASP.NET 2.0的新特性,例如MemberShip、Profile、Master Page、登錄控件等特性。接下來,我將結(jié)合PetShop 4.0的設(shè)計(jì)分別介紹它們的實(shí)現(xiàn)。

6.4.1 Profile特性

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

Profile的出現(xiàn)解決了如上的煩惱,它可以將用戶的個(gè)人化信息保存在指定的數(shù)據(jù)庫(kù)中。ASP.NET 2.0的Profile功能默認(rèn)支持Access數(shù)據(jù)庫(kù)和SQL Server數(shù)據(jù)庫(kù),如果需要支持其他數(shù)據(jù)庫(kù),可以編寫相關(guān)的ProfileProvider類。Profile對(duì)象是強(qiáng)類型的,我們可以為用戶信息建立屬性,以PetShop 4.0為例,它建立了ShoppingCart、WishList和AccountInfo屬性。

由于Profile功能需要訪問數(shù)據(jù)庫(kù),因而在數(shù)據(jù)訪問層(DAL)定義了和Product等數(shù)據(jù)表相似的模塊結(jié)構(gòu)。首先定義了一個(gè)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);
}

因?yàn)镻etShop 4.0版本分別支持SQL Server和Oracle數(shù)據(jù)庫(kù),因而它分別定義了兩個(gè)不同的PetShopProfileProvider類,實(shí)現(xiàn)IPetShopProfileProvider接口,并放在兩個(gè)不同的模塊SQLProfileDAL和OracleProfileDAL中。具體的實(shí)現(xiàn)請(qǐng)參見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);
}

}

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

定義了PetShop.Profile.PetShopProfileProvider類后,才可以在web.config配置文件中配置如下的配置節(jié):

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

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

而鍵值為ProfileDAL的值,正是Profile的工廠類PetShop.ProfileDALFactory.DataAccess在利用反射技術(shù)創(chuàng)建IPetShopProfileProvider類型對(duì)象時(shí)獲取的。

在表示層中,可以利用頁面的Profile屬性訪問用戶的個(gè)性化屬性,例如在ShoppingCart頁面的codebehind代碼ShoppingCart.aspx.cs中,調(diào)用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屬性的值從何而來?實(shí)際上,在我們?yōu)閣eb.config配置文件中對(duì)Profile進(jìn)行配置后,啟動(dòng)Web應(yīng)用程序,ASP.NET會(huì)根據(jù)該配置文件中的相關(guān)配置創(chuàng)建一個(gè)ProfileCommon類的實(shí)例。該類繼承自System.Web.Profile.ProfileBase類。然后調(diào)用從父類繼承來的GetPropertyValue和SetPropertyValue方法,檢索和設(shè)置配置文件的屬性值。然后,ASP.NET將創(chuàng)建好的ProfileCommon實(shí)例設(shè)置為頁面的Profile屬性值。因而,我們可以通過智能感知獲取Profile的ShoppingCart屬性,同時(shí)也可以利用ProfileCommon繼承自ProfileBase類的Save()方法,根據(jù)屬性值更新Profile的數(shù)據(jù)源。

6.4.2 Membership特性

PetShop 4.0并沒有利用Membership的高級(jí)功能,而是直接讓Membership特性和ASP.NET 2.0新增的登錄控件進(jìn)行綁定。由于.NET Framework 2.0已經(jīng)定義了針對(duì)SQL Server的SqlMembershipProvider,因此對(duì)于PetShop 4.0而言,實(shí)現(xiàn)Membership比之實(shí)現(xiàn)Profile要簡(jiǎn)單,僅僅需要為Oracle數(shù)據(jù)庫(kù)定義MembershipProvider即可。在PetShop.Membership模塊中,定義了OracleMembershipProvider類,它繼承自System.Web.Security.MembershipProvider抽象類。

OracleMembershipProvider類的實(shí)現(xiàn)具有極高的參考價(jià)值,如果我們需要定義自己的MembershipProvider類,可以參考該類的實(shí)現(xiàn)。
事實(shí)上OracleMemberShip類的實(shí)現(xiàn)并不復(fù)雜,在該類中,主要是針對(duì)用戶及用戶安全而實(shí)現(xiàn)相關(guān)的行為。由于在父類MembershipProvider中,已經(jīng)定義了相關(guān)操作的虛方法,因此我們需要作的是重寫這些虛方法。由于與Membership有關(guān)的信息都是存儲(chǔ)在數(shù)據(jù)庫(kù)中,因而OracleMembershipProvider與SqlMembershipProvider類的主要區(qū)別還是在于對(duì)數(shù)據(jù)庫(kù)的訪問。對(duì)于SQL Server而言,我們利用aspnet_regsql工具為Membership建立了相關(guān)的數(shù)據(jù)表以及存儲(chǔ)過程。也許是因?yàn)橹R(shí)產(chǎn)權(quán)的原因,Microsoft并沒有為Oracle數(shù)據(jù)庫(kù)提供類似的工具,因而需要我們自己去創(chuàng)建membership的數(shù)據(jù)表。此外,由于沒有創(chuàng)建Oracle數(shù)據(jù)庫(kù)的存儲(chǔ)過程,因而OracleMembershipProvider類中的實(shí)現(xiàn)是直接調(diào)用SQL語句。以CreateUser()方法為例,剔除那些繁雜的參數(shù)判斷與安全性判斷,SqlMembershipProvider類的實(shí)現(xiàn)如下:

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創(chuàng)建的存儲(chǔ)過程,它的功能就是創(chuàng)建一個(gè)用戶。

OracleMembershipProvider類中對(duì)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
<
分享到:
評(píng)論

petshop4.0 詳解之七(PetShop表示層設(shè)計(jì))


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 临桂县| 黑山县| 开远市| 湾仔区| 巴东县| 鲁甸县| 清镇市| 株洲县| 遵义县| 大安市| 长治市| 邵阳市| 大名县| 钦州市| 宿州市| 左权县| 华蓥市| 慈利县| 湟中县| 临猗县| 文成县| 江门市| 印江| 临城县| 鹿邑县| 黄梅县| 柞水县| 苏尼特左旗| 甘德县| 固安县| 仙居县| 上栗县| 辉县市| 瑞金市| 陈巴尔虎旗| 吉林市| 延长县| 株洲县| 连云港市| 东台市| 安岳县|