熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

petshop4體現的面向接口編程思想

2022-06-13   來源: .NET編程 

  petshop充分體現了面向接口編程的思想就是給你一個接口你別管我是怎麼實現的你只管用別說其他的          namespace PetShopBLL {
        /// <summary>
        /// A business component to manage products
        /// </summary>
        public class Product {
        // Get an instance of the Product DAL using the DALFactory
        // Making this static will cache the DAL instance after the initial load private static readonly IProduct dal = PetShopDALFactoryDataAccessCreateProduct();

  在bll層這裡使用創建Produce的接口你只管調用他的方法

  namespace PetShopIDAL
        {
        /// <summary>
        /// Interface for the Product DAL
        /// </summary>

  public interface IProduct
        {
        /// <summary>
        /// Method to search products by category name
        /// </summary>
        /// <param name=category>Name of the category to search by</param>
        /// <returns>Interface to Model Collection Generic of search results</returns> IList<ProductInfo> GetProductsByCategory(string category);
        /// <summary>
        /// Method to search products by a set of keyword
        /// </summary>
        /// <param name=keywords>An array of keywords to search by</param>
        /// <returns>Interface to Model Collection Generic of search results</returns> IList<ProductInfo> GetProductsBySearch(string[] keywords);
        /// <summary>
        /// Query for a product
        /// </summary>
        /// <param name=productId>Product Id</param>
        /// <returns>Interface to Model ProductInfo for requested product</returns> ProductInfo GetProduct(string productId); }

  這裡是定義了Product接口和他的虛方法          namespace PetShopSQLServerDAL {
        public class Product : IProduct {
        //Static constants private const string SQL_SELECT_PRODUCTS_BY_CATEGORY = SELECT ProductProductId ProductName ProductDescn ProductImage ProductCategoryId FROM Product WHERE ProductCategoryId = @Category; private const string SQL_SELECT_PRODUCTS_BY_SEARCH = SELECT ProductId Name Descn ProductImage ProductCategoryId FROM Product WHERE ((; private const string SQL_SELECT_PRODUCTS_BY_SEARCH = LOWER(Name) LIKE % + {} + % OR LOWER(CategoryId) LIKE % + {} + %; private const string SQL_SELECT_PRODUCTS_BY_SEARCH = ) OR (; private const string SQL_SELECT_PRODUCTS_BY_SEARCH = )); private const string SQL_SELECT_PRODUCT = SELECT ProductProductId ProductName ProductDescn ProductImage ProductCategoryId FROM Product WHERE ProductProductId = @ProductId; private const string PARM_CATEGORY = @Category; private const string PARM_KEYWORD = @Keyword; private const string Parm_PRODUCTID = @ProductId;
        /// <summary>
        /// Query for products by category
        /// </summary>
        /// <param name=category>category name</param>
        /// <returns>A Generic List of ProductInfo</returns> public IList<ProductInfo> GetProductsByCategory(string category) {
        IList<ProductInfo> productsByCategory = new List<ProductInfo>();

  這裡是實現Product接口的類          namespace PetShopDALFactory {
        /// <summary>
        /// This class is implemented following the Abstract Factory pattern to create the DAL implementation
        /// specified from the configuration file
        /// </summary> public sealed class DataAccess {
        // Look up the DAL implementation we should be using private static readonly string path = ConfigurationManagerAppSettings[WebDAL]; private static readonly string orderPath = ConfigurationManagerAppSettings[OrdersDAL]; private DataAccess() { }
        public static PetShopIDALICategory CreateCategory() { string className = path + Category; return (PetShopIDALICategory)AssemblyLoad(path)CreateInstance(className); }

  這裡是利用工廠模式來映射你需要你想創建哪一個

  後面還有一些消息隊列MSMQMessage利用cache緩存以後達到異步處理購物車裡訂單的功能!

  剛開始看應先從先從Product入口關於Product的一些操作串聯起來看一遍!


From:http://tw.wingwit.com/Article/program/net/201311/11385.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.