PetShop3.0 BLL部分源代码

Account.cs

using System;

//References to PetShop specific libraries
//PetShop busines entity library
using PetShop.Model;

//PetShop DAL interfaces
using PetShop.IDAL;

namespace PetShop.BLL {

 /// <summary>
 /// A business Component used to manage accounts
 /// The PetShop.Model.Account is used in most methods
 /// and is used to store serializable information about an account
 /// </summary>
 public class Account {
  
  /// <summary>
  /// Method to login into the system. The user must supply a username and password
  /// </summary>
  /// <param name="userId">Unique identifier for a user</param>
  /// <param name="password">Password for a user</param>
  /// <returns>If the login is successful it returns information abount the account</returns>
  public AccountInfo SignIn(string userId, string password) {

   // Validate input
   if ((userId.Trim() == string.Empty) || (password.Trim() == string.Empty))
    return null;

   // Get an instance of the account DAL using the DALFactory
   IAccount dal = PetShop.DALFactory.Account.Create();

   // Try to sign in with the given credentials
   AccountInfo account = dal.SignIn(userId, password);

   // Return the account
   return account;
  }

  /// <summary>
  /// Returns the address information for a specific user
  /// </summary>
  /// <param name="userId">Unique identifier for an account/customer</param>
  /// <returns>Returns the address information for the user</returns>
  public AddressInfo GetAddress(string userId) {

   // Validate input
   if (userId.Trim() == string.Empty)
    return null;
   
   // Get an instance of the account DAL using the DALFactory
   IAccount dal = PetShop.DALFactory.Account.Create();

   // Return the address information for the given userId from the DAL
   return dal.GetAddress(userId);
  }

  /// <summary>
  /// A method to insert a new Account
  /// </summary>
  /// <param name="account">An account entity with information about the new account</param>
  public void Insert(AccountInfo account) {

   // Validate input
   if (account.UserId.Trim() == string.Empty)
    return;

   // Get an instance of the account DAL using the DALFactory
   IAccount dal = PetShop.DALFactory.Account.Create();

   // Call the DAL to insert the account
   dal.Insert(account);
  }

  /// <summary>
  /// A method to update an existing account
  /// </summary>
  /// <param name="account">An account entity with information about the account to be updated</param>
  public void Update(AccountInfo account) {

   // Validate input
   if (account.UserId.Trim() == string.Empty)
    return;

   // Get an instance of the account DAL using the DALFactory
   IAccount dal = PetShop.DALFactory.Account.Create();

   // Send the udpated account information to the DAL
   dal.Update(account);
  }
 }
}

Cart.cs

using System;
using System.Collections;

//References to PetShop specific libraries
//PetShop busines entity library
using PetShop.Model;

namespace PetShop.BLL {
 
 /// <summary>
 /// An object to represent a customer's shopping cart
 /// </summary>
 [Serializable]
 public class Cart : IEnumerable {

  /// <summary>
  /// Internal storage for a cart
  /// </summary>
  private ArrayList _items = new ArrayList();

  private decimal _total=0;

  /// <summary>
  /// Returns an enumerator for the cart items in a cart
  /// </summary>
  /// <returns></returns>
  public IEnumerator GetEnumerator() {
   return _items.GetEnumerator();
  }

  // Properties
  public decimal Total {
   get { return _total; }
   set { _total = value; }
  }

  /// <summary>
  /// Returns number of items in cart
  /// </summary>
  public int Count {
   get { return _items.Count; }
  }

  /// <summary>
  /// Return CartItem representation of object at a given address
  /// </summary>
  public CartItemInfo this[int index] {
   get { return (CartItemInfo)_items[index]; }
  }

  /// <summary>
  /// Add an item to the cart
  /// </summary>
  /// <param name="ItemId">ItemId of item to add</param>
  public void Add(string ItemId) {
   foreach (CartItemInfo cartItem in _items) {
    if (ItemId == cartItem.ItemId) {
     cartItem.Quantity++;
     cartItem.InStock = (GetInStock(ItemId) - cartItem.Quantity) >= 0 ? true : false;
     _total = _total+(cartItem.Price*cartItem.Quantity);
     return;
    }
   }

   Item item = new Item();

   ItemInfo data = item.GetItem(ItemId);
   CartItemInfo newItem = new CartItemInfo(ItemId,data.Name, (data.Quantity >= 1), 1, (decimal)data.Price);
   _items.Add(newItem);
   _total = _total+(data.Price);
  }

  /// <summary>
  /// Remove item from the cart based on itemId
  /// </summary>
  /// <param name="itemId">ItemId of item to remove</param>
  public void Remove(string itemId) {
   foreach (CartItemInfo item in _items) {
    if (itemId == item.ItemId) {
     _items.Remove(item);
     _total = _total-(item.Price*item.Quantity);
     return;
    }
   }
  }

  /// <summary>
  /// Removes item from cart at specific index
  /// </summary>
  /// <param name="index">Element number of item to remove</param>
  public void RemoveAt(int index) {
   CartItemInfo item = (CartItemInfo)_items[index];
   _total = _total-(item.Price*item.Quantity);
   _items.RemoveAt(index);
   
  }

  /// <summary>
  /// Returs internal array list of cart items
  /// </summary>
  /// <returns></returns>
  public ArrayList GetCartItems() {
   return _items;
  }

  /// <summary>
  /// Method to convert internal array of cart items to order line items
  /// </summary>
  /// <returns>New array list of order line items</returns>
  public ArrayList GetOrderLineItems() {

   ArrayList orderLineItems = new ArrayList();

   int lineNum = 1;

   foreach (CartItemInfo item in _items) {

    LineItemInfo lineItem = new LineItemInfo(item.ItemId, item.Name, lineNum, item.Quantity, item.Price);
    orderLineItems.Add(lineItem);
    lineNum++;
   }

   return orderLineItems;
  }

  
  /// <summary>
  /// Internal method to get the stock level of an item
  /// </summary>
  /// <param name="ItemId">Unique identifier of item to get stock level of</param>
  /// <returns></returns>
  private int GetInStock(string ItemId){
   Inventory inventory = new Inventory();

   return inventory.CurrentQuantityInStock(ItemId);
  }
 }
}

关键是系统架构和代码学习两方面,对初学和提高有很大帮助 petshop5.0比较大,代码已经解压出来 4.03.0没有解压出来,自行安装解压(需要SqlServer数据做连接或者在安装到数据库连接时直接拷贝出来) petshop5.0 基于.NET Framework 3.5 ------------ 使用LINQ to SQL改进数据访问层 PetShop.Model.DataContext.MSPetShop4DataContext 继承System.Data.Linq.DataContext PetShop.Model.ProductInfo与PetShop.Model.CategoryInfo实体类分别映射数据库表 PetShop.Model.ProductInfo其中的Category属性存在一对一的关系 PetShop.Model.CategoryInfo中的Products属性存在一对多的关系 使用WCF来提供RSS, web/FeedService.svc目录下 PetShop.SyndicationFeeds 并在UI层上做一些改进,如使用ASP.NET AJAX,ListView控件等。 在PetShop 5.0中引入了异步处理机制。 插入订单的策略可以分为同步和异步,两者的插入策略明显不同,但对于调用者而言,插入订单的接口是完全一样的,所以PetShop 5.0中设计了IBLLStrategy模块。 虽然在IBLLStrategy模块中,仅仅是简单的IOrderStategy,但同时也给出了一个范例和信息,那就是在业务逻辑的处理中,如果存在业务操作的多样化,或者是今后可能的变化,均应利用抽象的原理。或者使用接口,或者使用抽象类,从而脱离对具体业务的依赖。 不过在PetShop中,由于业务逻辑相对简单,这种思想体现得不够明显。 也正因为此,PetShop将核心的业务逻辑都放到了一个模块BLL中,并没有将具体的实现和抽象严格的按照模块分开。所以表示层和业务逻辑层之间的调用关系,其耦合度相对较高: PetShop4.0源代码 .NET Pet Shop4 应用程序的设计说明了构建企业 n 层 .NET 2.0 应用程序的最佳做法,这种应用程序可能需要支持各种数据库平台和部署方案。 .NET Pet Shop 4 项目的目标是: 工作效率:减少了 .NET Pet Shop 3 的代码数量 - 我们减少了近 25% 的代码。 利用 ASP.NET 2.0 的新功能 - 我们利用母版页、成员身份和配置文件,并设计出一个新的、吸引人的用户界面。 企业体系结构:构建一个灵活的最佳做法应用程序 - 我们实现了设计模式,以及表示层、业务层和数据层的分离。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值