Profile在petshop

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  

 

 

 

 

 

 

 

 

posted @   小霖2012  阅读(367)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示