Profile在petshop的应用应该要从对ProfileProvide的重写谈起。
跟往常一样,定义了一个接口IPetShopProfileProvider,那么在这个接口中我们到底要定义些什么功能呢?先看下Profile的属性吧:
<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>
可以看到Profile定义了ShoppingCart、WishList和AccountInfo属性,那么在数据库MSPetShop4Profile中友对应的表cart、Account。
由此可以见我们最主要是要对cart和Account进行操作,当然还有对Profile的进行操作,如最后登录时间等。
比较啰嗦,现在贴出自己不太理解的代码块(方便以后在研究),在PetShopProfileProvider中队IPetShopProfileProvider进行了实现:
public void UpdateActivityDates(string userName, bool activityOnly, string appName) { DateTime activityDate = DateTime.Now; string sqlUpdate; SqlParameter[] parms; if(activityOnly) { sqlUpdate = "UPDATE Profiles Set LastActivityDate = @LastActivityDate WHERE Username = @Username AND ApplicationName = @ApplicationName;"; parms = new SqlParameter[]{ new SqlParameter("@LastActivityDate", SqlDbType.DateTime), new SqlParameter("@Username", SqlDbType.VarChar, 256), new SqlParameter("@ApplicationName", SqlDbType.VarChar, 256)}; parms[0].Value = activityDate; parms[1].Value = userName; parms[2].Value = appName; } else { sqlUpdate = "UPDATE Profiles Set LastActivityDate = @LastActivityDate, LastUpdatedDate = @LastUpdatedDate WHERE Username = @Username AND ApplicationName = @ApplicationName;"; parms = new SqlParameter[]{ new SqlParameter("@LastActivityDate", SqlDbType.DateTime), new SqlParameter("@LastUpdatedDate", SqlDbType.DateTime), new SqlParameter("@Username", SqlDbType.VarChar, 256), new SqlParameter("@ApplicationName", SqlDbType.VarChar, 256)}; parms[0].Value = activityDate; parms[1].Value = activityDate; parms[2].Value = userName; parms[3].Value = appName; } SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringProfile, CommandType.Text, sqlUpdate, parms); }
我不知道为什么事三个参数,也不知道为什么是这三个参数:username、activityOnly、appName。如果说appName是.Net PetShop4.0的话;
activityOnly进行判断。从这个函数的名字UpdateActivityDates可以看出是对最后一次活动的时间进行更新。
接下来是GetUniqueID:
public int GetUniqueID(string userName, bool isAuthenticated, bool ignoreAuthenticationType, string appName) { string sqlSelect = "SELECT UniqueID FROM Profiles WHERE Username = @Username AND ApplicationName = @ApplicationName"; SqlParameter[] parms = { new SqlParameter("@Username", SqlDbType.VarChar, 256), new SqlParameter("@ApplicationName", SqlDbType.VarChar, 256)}; parms[0].Value = userName; parms[1].Value = appName; if(!ignoreAuthenticationType) { sqlSelect += " AND IsAnonymous = @IsAnonymous"; Array.Resize<SqlParameter>(ref parms, parms.Length + 1); parms[2] = new SqlParameter("@IsAnonymous", SqlDbType.Bit); parms[2].Value = !isAuthenticated; } int uniqueID = 0; object retVal = null; retVal = SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringProfile, CommandType.Text, sqlSelect, parms); if(retVal == null) uniqueID = CreateProfileForUser(userName, isAuthenticated, appName); else uniqueID = Convert.ToInt32(retVal); return uniqueID; }
这个函数带有四个参数:string userName, bool isAuthenticated, bool ignoreAuthenticationType, string appName。
我不知道ignareAuthticationType是干嘛用的,但是根据上面的源码可以看出,ignareAuthticationType为false是就执行
parms[2].Value = !isAuthenticated