PetShop3.0 Control Flow 部分源代码

AccountController.cs

using System;
using System.Web;
using System.Web.Security;

//PetShop specific references
using PetShop.Model;
using PetShop.BLL;

namespace PetShop.Web.ProcessFlow
{
 /// <summary>
 /// Acount Process Flow, controls navigation for account events
 /// </summary>
 public class AccountController {
  // Navigation constants

  private const string ACCOUNT_KEY = "ACCOUNT_KEY";
  private const string URL_DEFAULT = "default.aspx";
  private const string URL_SIGNIN  = "SignIn.aspx";
  private const string URL_ACCOUNTCREATE = "MyAccount.aspx?action=create";
  private const string URL_ACCOUNTSIGNIN = "MyAccount.aspx?action=signIn";
  private const string URL_ACCOUNTUPDATE = "MyAccount.aspx?action=update";
  
  /// <summary>
  /// Default constructor
  /// </summary>
  public AccountController(){
  }

  /// <summary>
  /// Verify Login process
  /// User passes in a user name and password and will be redirected on if successful
  /// </summary>
  /// <param name="userId">User name the customer is authenticating with</param>
  /// <param name="password">Password the customer is using</param>
  /// <returns>true if the login is successful</returns>
  public bool ProcessLogin(string userId, string password){

   // Use the account business logic layer to login
   Account account = new Account();
   AccountInfo myAccountInfo = account.SignIn(userId, password);

   //If login is successful then store the state in session and redirect
   if (myAccountInfo != null) {
    HttpContext.Current.Session[ACCOUNT_KEY] = myAccountInfo;
    
    // Determine where to redirect the user back too
    // If they came in from the home page, take them to a similar page
    if (FormsAuthentication.GetRedirectUrl(userId, false).EndsWith(URL_DEFAULT)) {

     FormsAuthentication.SetAuthCookie(userId, false);
     HttpContext.Current.Response.Redirect(URL_ACCOUNTSIGNIN, true);

    }else{
     // Take the customer back to where the came from
     FormsAuthentication.SetAuthCookie(userId, false);

     HttpContext.Current.Response.Redirect(FormsAuthentication.GetRedirectUrl(userId, false), true);
    }

    return true;
   
   }else {
    // Login has failed so return false
    return false;
   }
  }

  public bool CreateAccount(AccountInfo newAccountInfo){

   try {
    // Creata a new business logic tier
    Account account = new Account();

    // Call the insert method
    account.Insert(newAccountInfo);

    // Store the data in session state and store the authenticated cookie
    HttpContext.Current.Session[ACCOUNT_KEY] = newAccountInfo;
    FormsAuthentication.SetAuthCookie(newAccountInfo.UserId, false);
    
    //Finally forward to the welcome page
    HttpContext.Current.Response.Redirect(URL_ACCOUNTCREATE, true);
    
   
   }catch {
    return false;
   }

   return true;
  }

  /// <summary>
  /// A method to process an updated account
  /// </summary>
  /// <param name="updatedAccountInfo">Updated account information</param>
  public void UpdateAccount(AccountInfo updatedAccountInfo){

   // Create the business logic tier
   Account account = new Account();
   
   // Call the udpate method
   account.Update(updatedAccountInfo);

   //Store the update info back in session state
   HttpContext.Current.Session[ACCOUNT_KEY] = updatedAccountInfo;

   //Redirect the user to the my account page
   HttpContext.Current.Response.Redirect(URL_ACCOUNTUPDATE, true);
   
  }

  /// <summary>
  /// Retrieves the account information for a customer who has already logged in
  /// The method assume the account information is in session state
  /// If it can't find it the function will direct the user to login
  /// </summary>
  /// <returns>The account info for the currently logged in user</returns>
  public AccountInfo GetAccountInfo(bool required){
   AccountInfo myAccount = (AccountInfo)HttpContext.Current.Session[ACCOUNT_KEY];

   if (myAccount == null){
    if(required){
     HttpContext.Current.Response.Redirect(URL_SIGNIN, true);
     
    }
    return null;
   }else{
    return myAccount;
   }
  }

  /// <summary>
  /// Retrieves favourtie category of a customer if we know who they are
  /// The method assume the account information is in session state
  /// </summary>
  /// <returns>The customers favourite category</returns>
  public string GetFavouriteCategory(){

   AccountInfo myAccount = (AccountInfo)HttpContext.Current.Session[ACCOUNT_KEY];

   if (myAccount != null && myAccount.IsShowFavorites) {
    return myAccount.Category;
   }else{
    return null;
   }
  }

  /// <summary>
  /// Method to log the user out of the application
  /// When the user logs out there session is cleared and their authentication ticket is reset
  /// </summary>
  public void LogOut(){

   // Clear the authentication ticket
   FormsAuthentication.SignOut();
   // Clear the contents of their session
   HttpContext.Current.Session.Clear();
   // Tell the system to drop the session reference so that it does
   // not need to be carried around with the user
   HttpContext.Current.Session.Abandon();
  }
 }
}

CartController.cs

using System;
using System.Web;
using System.Web.Security;

// PetShop specific imports
using PetShop.Model;
using PetShop.BLL;

namespace PetShop.Web.ProcessFlow{

 /// <summary>
 /// Cart Process Flow, controls navigation for cart based events
 /// </summary>
 public class CartController{

  private const string BILLING_KEY = "BILLING_KEY";
  private const string SHIPPING_KEY = "SHIPPING_KEY";
  private const string ACCOUNT_KEY = "ACCOUNT_KEY";
  private const string CART_KEY = "CART_KEY";
  private const string CREDITCARD_KEY = "CREDITCARD_KEY";
  private const string URL_NOCART = "ShoppingCart.aspx";

  public CartController(){}

  /// <summary>
  /// A method to return the current state of the cart
  /// </summary>
  /// <param name="create">Specifies whether a cart should be created if one does not exist</param>
  /// <returns>Cart object</returns>
  public Cart GetCart(bool create){
  
   // Fetch the cart object from session state
   Cart myCart = (Cart)HttpContext.Current.Session[CART_KEY];

   if ( myCart == null ){
    if (create){
     myCart = new Cart();
    }else{   
     HttpContext.Current.Server.Transfer(URL_NOCART);
     return null;
    }
   }
   
   return myCart;
  }

  /// <summary>
  /// A method to return the current state of the cart
  /// </summary>
  /// <param name="create">Specifies whether a cart should be created if one does not exist</param>
  /// <returns>Cart object</returns>
  public void StoreCart(Cart cart){
  
   // Store the cart object in session state
   HttpContext.Current.Session[CART_KEY] = cart;

  }

  /// <summary>
  /// A method to purchase the contents of the cart
  /// </summary>
  /// <returns>Order object with information about the new Order</returns>
  public OrderInfo PurchaseCart(){

   // Fetch the cart from session
   Cart myCart = (Cart)HttpContext.Current.Session[CART_KEY];

   // Make some checks on the cart
   if ( ( myCart == null ) || ( myCart.Count==0 ) ) {

    HttpContext.Current.Server.Transfer(URL_NOCART);
    //HttpContext.Current.Response.Redirect(URL_NOCART, false);
    return null;

   }else{
    
    // Build up the order
    OrderInfo newOrder = new OrderInfo();
    newOrder.UserId = ((AccountInfo)HttpContext.Current.Session[ACCOUNT_KEY]).UserId;
    newOrder.CreditCard = (CreditCardInfo)HttpContext.Current.Session[CREDITCARD_KEY];
    newOrder.BillingAddress = (AddressInfo)HttpContext.Current.Session[BILLING_KEY];
    newOrder.ShippingAddress = (AddressInfo)HttpContext.Current.Session[SHIPPING_KEY];
    
    newOrder.LineItems = (LineItemInfo[])myCart.GetOrderLineItems().ToArray(typeof(LineItemInfo));
        
    newOrder.OrderTotal = myCart.Total;   
    newOrder.Date = DateTime.Now;
    
    // Send the order to the middle tier
    OrderInsert order = new OrderInsert();       
    newOrder.OrderId = order.Insert(newOrder);
    
    // clear the session objects used
    HttpContext.Current.Session[CART_KEY] = null;
    HttpContext.Current.Session[CREDITCARD_KEY] = null;
    HttpContext.Current.Session[BILLING_KEY] = null;
    HttpContext.Current.Session[SHIPPING_KEY] = null;

    return newOrder;
   }
  }

  /// <summary>
  /// A method to store credit card state information
  /// </summary>
  public void StoreCreditCard(CreditCardInfo creditCard){

   HttpContext.Current.Session[CREDITCARD_KEY] = creditCard;
    
  }

  /// <summary>
  /// A method to store the billing address information
  /// </summary>
  public void StoreBillingAddress(AddressInfo billingAddress){

   HttpContext.Current.Session[BILLING_KEY] = billingAddress;
    
  }

  /// <summary>
  /// A method to store the shipping address information
  /// </summary>
  public void StoreShippingAddress(AddressInfo shippingAddress){

   HttpContext.Current.Session[SHIPPING_KEY] = shippingAddress;
    
  }

  /// <summary>
  /// A method to get the billing address information from the state store
  /// </summary>
  public AddressInfo GetBillingAddress(){

   return (AddressInfo)HttpContext.Current.Session[BILLING_KEY];
    
  }

  /// <summary>
  /// A method to get the shipping address information from the state store
  /// </summary>
  public AddressInfo GetShippingAddress(){

   return (AddressInfo)HttpContext.Current.Session[SHIPPING_KEY];
    
  }

  /// <summary>
  /// A method to control the flow when 'continuing' an order
  /// The customer will have entered creditcard information and some address information
  /// </summary>
  public void ContinueOrder(bool useBillingAddress){

   // Set the billing address depending on what the user has selected
   if (useBillingAddress) {
    HttpContext.Current.Session[SHIPPING_KEY] = HttpContext.Current.Session[BILLING_KEY];
   } else {
    //If the user wants to use a different address then take them to the next page
    HttpContext.Current.Response.Redirect("OrderShipping.aspx", true);
   }    
  }

  /// <summary>
  /// A method to control the flow when picking an alternate shipping address
  /// </summary>
  public void SetAlternateShippingAddress(AddressInfo shippingAddress){

   HttpContext.Current.Session[SHIPPING_KEY] = shippingAddress;
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值