zoukankan      html  css  js  c++  java
  • 使用Profile和Provider技术实现 购物车

    一、ASP.NET的Profile属性

      

    1.提供对配置文件(Web.config)属性值和信息的非类型化访问

    2.Web应用程序运行时,ASP.NET创建一个从ProfileBase类动态继承下来的动态生成出来的ProfileCommon类。动态ProfileCommon类包含了你在Web应用程序配置文件中指定的Profile属性所拥有的字段。一个动态ProfileCommon类的实例被设置成了当前HttpContext的Profile属性,并可以在应用程序的各个页面中使用。

    二、Profile作用

    1.存储和使用唯一与用户对应的信息

    2.展现个人化版本的Web应用程序

    3.用户的唯一身份标识在再次访问时识别用户

    =========常用的Web保存数据的方式=========

    1.Session

    只要Session不超时,那么保存在Session的数据就不会丢失

    优点:

    <1>数据私有性

    <2>会话结束,释放资源,节省访问器内存

    缺点:

    <1>易丢失,超时时间很难确定

    <2>Asp.Net1.0将Session保存到数据库方式,持久保存数据,导致网站性能降低

    使用:

    Session["Key"]=Value;//存储
    (Type)Session["Key"]; //读取(强制转换)
    
    

    2.Cookie

    保存到客户端的少量文本数据

    优点:

    <1>可自定义有效期

    <2>不占用服务器磁盘空间

    <3>稳定性比较好

    缺点:

    <1>易丢失

    <2>安全性差

    <3>只能存储文本数据

    使用:

    Response.Cookies["Key"].Value=值;//向Cookie写入数据,String类型
    string value = Request.Cookies["Key"];//读取
    
    

    3.Application

    4.Cache

    5.XML

    6.文件

    7.数据库

      

    =========购物车=========

    一、特点

    1.私有性:每位用户操作自己的购物车

    2.安全性:保障用户支付信息的安全

    3.稳定性:可以支持高负载

    4.持久性:购物车内的物品不能丢失

    二、抽象

    <1> 购物车中的商品类(购物车容器中的实例)

    ===CartItem类的属性===

    ID: 唯一标识 (只读)

    Name: 商品名称 (只读)

    Price: 商品价格 (只读)

    Quantity: 商品数量

    ===CartItem的方法===

    CartItem(int id,string name,decimal Price);
    CartItem(int id,string name,decimal Price,int Quantity);
    
    

    <2>购物车(存放商品的容器)

    ===Cart类的属性===

    Items :商品集合(Hashtable)

    Hashtable Items = new Hashtable();
    
    //返回购物车中商品的集合
    
    public ICollection CartItems
    
    {
    
      Get{return Items.Values;}
    
    }
    
    

    CartItems: 获取全部商品

    Total: 商品总价格

    ===Cart类的方法===

    public void AddItem(int id,string name,decimal price) //增加项
    
    {
    
      //无商品增加. 有商品更改数量,通过检验Hashtable中 键来判断有无商品            
    
        CartItem item = (CartItem)Items[id];
    
      if(item ==null)
    
      {
    
        //增加
    
        Items.Add(id,new CartItem(id,name,price));
    
      }
    
      else
    
      {
    
        items.Quantity++;
    
        items[id] = item;
    
      }
    
    }
    
    public void RemoveItem(int id);//移出项
    
    {
    
      item.Quantity--;
    
      if(item.Quantity ==0)//如果没数量,移除
    
      {
    
        Items.Remove(id);
    
      }
    
      else
    
      {
    
        items[id] =item;
    
      }
    }
    
    

     

      =========Provider提供程序==========

     1.Profile属性

    2.配置层:Web.Config

    3.提供程序层:SqlProfileProvider

    4.数据层:Aspnetdb数据库

      

    //Web.config
    
    <connectionStrings>  
        <add name="profileConnStr" connectionString="server=.\sqlexpress;database=test;uid=sa;pwd=123456"/>  
    </connectionStrings>  
    <system.web>  
        <!-- 匿名用户也可访问profile -->  
        <anonymousIdentification enabled="true"/>  
        <profile enabled="true" automaticSaveEnabled="true" defaultProvider="SqlProvide">  
         <providers>  
            <add name="SqlProvide" connectionStringName="profileConnStr" type="System.Web.Profile.SqlProfileProvider"/>  
          </providers>  
          <properties>  
            <add name="ShoppingCart" type="Cart" allowAnonymous="true" serializeAs="Binary"/>  
          </properties>  
        </profile>  
        ......   
         <!--   
                通过 <authentication> 节可以配置 ASP.NET 用来    
                识别进入用户的   
                安全身份验证模式。    
            -->  
        <authentication mode="Forms" />  
    </system.web> 
    
    //采用硬编码方式进行用户认证:
    
    string name = txtName.Text.Trim();   
    string pwd = txtPassword.Text.Trim();   
      
    if (name == "niunan" && pwd == "123456")   
    {   
        FormsAuthentication.SetAuthCookie(name, false);   
        Response.Redirect("~/product.aspx");   
    }  
    
    
    //匿名用户的购物车数据向实名用户的购物车数据迁移
    
    //即,未登陆时收藏到购物车. 结账时 登陆.将匿名用户的购物车转移到登陆账户中
    //Global.asax文件中加入如下内容:
    
    protected void Profile_MigrateAnonymous(object s, ProfileMigrateEventArgs e)   
    {   
        ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);   
        foreach (CartItem ci in anonProfile.ShoppingCart.GetItems())   
        {   
            Profile.ShoppingCart.AddItem(ci);   
        }   
      
        ProfileManager.DeleteProfile(e.AnonymousID);  // 删除匿名用户的profile   
        AnonymousIdentificationModule.ClearAnonymousIdentifier();  // 清除匿名用户标识   
        Profile.Save();   
    } 
    
    
    
    
    =============Aspnetdb数据库中内容===============
    aspnet_Users基础表
    aspnet_Applications基础表  
    aspnet_Profile  个性化用户配置部分  
    aspnet_PersonalizationPerUser  页面个性化设置部分  
    aspnet_Paths  页面个性化设置部分  
    aspnet_PersonalizationAllUsers  页面个性化配置部分  
    aspnet_Roles  角色管理部分  
    aspnet_UserInRoles  角色管理部分  
    aspnet_Membership  成员资格管理部分
    
    
    =============Asp.net提供程序基类===============
    描述
    MembershipProvider   成员资格提供程序的基类,用来管理用户账户信息  
    ProfileProvider   个性化提供程序的基类,用来永久存储和检索用户的配置信息  
    RoleProvider   角色提供程序的基类,用来管理用户角色信息  
    SessionStateStoreProviderBase   会话状态存储器提供程序的基类,这些提供程序用来把会话状态信息保存在持久性存储介质中,或从中检索会话状态信息  
    SiteMapProvider   管理站点地图信息的基类

    Profile属性的作用

    1.存储和使用唯一与用户对应的信息

    2.展现个人化版本的Web应用程序

    3.用户的唯一身份标识在再次访问时识别用户

    =============Sql ServerProvider对Asp.net提供程序的支持===============
    描述
    SqlMembershipProvider类   成员资格  
    SqlRoleProvider类   角色管理  
    SqlProfileProvider类   个性化配置  
    SqlPersonalizationProvider类   Web部件个性化设置  
    SqlWebEventProvider类   Web事件
  • 相关阅读:
    栈· 用一个栈实现另一个栈的排序
    Array+DP leetcode-11.装更多的水
    string+DP leetcode-4.最长回文子串
    leetcode-3 最长无重复字串
    栈 · 有getMin功能的栈O(1)
    VS OpenCV imread imwrite nameWindow等相关报错问题
    socket字符流循环截取
    jsp:set/getProperty底层实现的探究
    关于C++11右值引用和移动语义的探究
    VS fopen sprinft ... unsafe 问题
  • 原文地址:https://www.cnblogs.com/tweet/p/1671746.html
Copyright © 2011-2022 走看看