技术开发 频道

PetShop的购物车和订单处理模块

  5.PetShop.Profile. PetShopProfileProvider类, 继承自ProfileProvider

// 创建 PetShop.SQLProfileDAL.PetShopProfileProvider类-数据库操作 private static readonly IPetShopProfileProvider dal = DataAccess.CreatePetShopProfileProvider(); /// <summary> /// 设置指定的属性设置组的值 /// </summary> public override void SetPropertyValues(SettingsContext context,
SettingsPropertyValueCollection collection) {
string username = (string)context["UserName"
]; CheckUserName(username); bool isAuthenticated = (bool)context["IsAuthenticated"]; int uniqueID = dal.GetUniqueID(username, isAuthenticated, false, ApplicationName); if(uniqueID == 0) uniqueID = dal.CreateProfileForUser( username, isAuthenticated, ApplicationName); ……   } } UpdateActivityDates(username, false); }

  6.PetShop.SQLProfileDAL. PetShopProfileProvider类

  使用事务:包含两个sql动作,先删除,再插入。

  订单处理技术

  订单处理技术:――分布式事务

  同步:直接在事务中 将订单 插入到数据库中,同时更新库存

  异步:订单-》消息队列(使用MSMQ)-》后台处理 

  MSMQ 消息队列简介

  1)引用队列

  引用队列有三种方法,通过路径、格式名和标签引用队列,这里我只介绍最简单和最常用的方法:通过路径引用队列。队列路径的形式为 machinename\queuename。指向队列的路径总是唯一的。

  2)消息的创建

  不过要使用MSMQ开发你的消息处理程序,必须在开发系统和使用程序的主机上安装消息队列。消息队列的安装属于Windows组件的安装,和一般的组件安装方法类似。往系统中添加队列十分的简单,打开[控制面板]中的[计算机管理],展开[服务和应用程序],找到并展开[消息队列](如果找不到,说明你还没有安装消息队列,安装windows组件),右击希望添加的消息队列的类别,选择新建队列即可。

  消息接收服务位于System.Messaging中,在初始化时引用消息队列的代码很简单,如下所示:

MessageQueue Mq=new MessageQueue(“.\\private$\\jiang”)

  通过Path属性引用消息队列的代码也十分简单:

MessageQueue Mq=new MessageQueue(); Mq.Path=”.\\private$\\jiang”;

  使用Create 方法可以在计算机上创建队列:

System.Messaging.MessageQueue.Create(@".\private$\jiang");

  3)发送和接收消息

  过程:消息的创建-》发送-》接收-》阅读-》关闭

  简单消息的发送示例如下:

Mq.Send(1000); //发送整型数据 Mq.Send(“This is a test message!”); //发送字符串

  接收消息由两种方式:通过Receive方法接收消息同时永久性地从队列中删除消息;通过Peek方法从队列中取出消息而不从队列中移除该消息。如果知道消息的标识符(ID),还可以通过ReceiveById方法和PeekById方法完成相应的操作。

  接收消息的代码很简单:

Mq.Receive(); //或Mq.ReceiveById(ID); Mq.Peek(); // 或Mq.PeekById(ID);

  阅读消息

  接收到的消息只有能够读出来才是有用的消息,因此接收到消息以后还必须能读出消息,而读出消息算是最复杂的一部操作了。消息的序列化可以通过Visual Studio 和 .NET Framework 附带的三个预定义的格式化程序来完成:XMLMessageFormatter 对象( MessageQueue 组件的默认格式化程序设置)、BinaryMessageFormatter 对象、ActiveXMessageFormatter 对象。由于后两者格式化后的消息通常不能为人阅读,所以我们经常用到的是XMLMessageFormatter对象。

  关闭消息队列

  消息队列的关闭很简单,和其他对象一样,通过Close函数就可以实现了:Mq.Close();

  PetShop程序中订单处理-使用同步消息

  默认程序使用同步消息处理,直接操作数据库插入订单,更新库存类。

  PetShop程序中订单处理-使用异步消息

  Web程序中调用PetShop.BLL.Order类方法:  Insert(OrderInfo order);

  PetShop.BLL.Order类

private static readonly PetShop.IBLLStrategy. IOrderStrategy orderInsertStrategy = LoadInsertStrategy(); //IOrder接口中有两种方法:Send()与Receive() -消息队列 private static readonly PetShop.IMessaging.IOrder orderQueue = PetShop.MessagingFactory.QueueAccess.CreateOrder(); public void Insert(OrderInfo order) { // Call credit card procesor,采用随机化方法设置订单认证数字 ProcessCreditCard(order); // Insert the order (a)synchrounously based on configuration orderInsertStrategy.Insert(order);   //调用PetShop.BLL.OrderAsynchronous类 }   PetShop.BLL. OrderAsynchronous类 // CreateOrder()方法得到PetShop.MSMQMessaging .Order类的实例 private static readonly PetShop.IMessaging.IOrder asynchOrder = PetShop.MessagingFactory.QueueAccess.CreateOrder(); public void Insert(PetShop.Model.OrderInfo order) { asynchOrder.Send(order);  //调用PetShop.MSMQMessaging.Order类 }   PetShop.MSMQMessaging项目 -关键(发送/接收消息)   PetShopQueue基类:创建消息队列,发送和接收消息。   Order类:继承自PetShopQueue类 public new OrderInfo Receive() {//从队列中接收消息 base.transactionType = MessageQueueTransactionType.Automatic; return (OrderInfo)((Message)base.Receive()).Body; } public void Send(OrderInfo orderMessage) {//发送消息到队列 base.transactionType = MessageQueueTransactionType.Single; base.Send(orderMessage); }

  PetShop.OrderProcessor项目-后台处理订单,将它们插入到数据库中。Program类:多线程后台订单处理程序,可写成一个控制台程序,作为windows服务开启。处理队列中的批量异步订单,在事务范围内把它们提交到数据库。

0
相关文章