. Net Pet Shop 4: Order Management Module

Source: Internet
Author: User

Preface

It can be said that the order management module is the most worthwhile module in petshop4. because it uses multiple important technologies: factory mode design and policy mode design, especially advanced technologies such as order asynchronous processing and Windows message mechanism.

Number of orders

To speed up access, the order data of petshop4 is stored separately in a database called mspetshop4orders. in the first article of this series of blog posts, we have made a detailed analysis of this database. I will not add more descriptions here.

Order data access layer

Like the shopping basket module, the Order module adopts a typical factory model.

First, analyze the entity type of the order. In petshop4, the order is divided into two parts: the basic information of the Order, saved in the order table, and the detailed product information of the order, saved in the lineitem table. Based on the relationship between the object class and the table, the object class is also divided into two types: lineiteminfo class and orderinfo class. their attributes correspond to the fields of two tables in the database. these entity classes are located under the Model Project.

Next, let's analyze the order data category. the data orders class is a centralized embodiment of the Dal class. In the system, only one order class is used to handle order-related operations. take the order class under the sqlserverdal project as an example. The structure of the Order class is as follows:

 Business logic layer of orders

There are two ways to submit an order at the business logic layer: synchronous and asynchronous.

Synchronization means that the user inserts data into the table after submitting the order and then performs the following operations. asynchronous means that after the user submits the order, the data is processed by MSMQ (Message Queue) in the background. You can continue to perform the following operations without waiting for this order to be completed. obviously, asynchronous processing can greatly improve the operation efficiency without affecting other operations.

The bll implementation process of Order Management is roughly as follows:

There are three types of business logic associated with order operation: Order, oderasynchronous and ordersynchronous, both under the BLL project.

Order class, mainly used to insert orders and obtain order information. The specific structure is as follows:

Note the following when declaring several static variables:

// Using a static variable will cache the order insert strategy object for all instances of order
// We implement it this way to improve performance, so that the Code will only load the instance once
Private Static readonly petshop. ibllstrategy. iorderstrategy orderinsertstrategy = loadinsertstrategy ();

// Create a message query for an order.
Private Static readonly petshop. imessaging. iorder orderqueue = petshop. messagingfactory. queueaccess. createorder ();

// Get an instance of the Order Dal using the dalfactory
// Making this static will cache the Dal instance after the initial load
Private Static readonly iorder Dal = petshop. dalfactory. dataaccess. createorder ();

The loadinsertstrategy () method returns a policy.AlgorithmInstance. automatically determines whether synchronous or asynchronous processing is used by reading the configuration file.

Ordersynchronous class, which adopts the synchronous processing method to operate orders. This class is inherited from the iorderstrategy interface together with the asynchronous processing class oderasynchronous. The iorderstrategy interface is located in the. ordersynchronous class under the ibllstrategy project.CodeAs follows:

Public class ordersynchronous: iorderstrategy {

// Get an instance of the Order Dal using the dalfactory
// Making this static will cache the Dal instance after the initial load
Private Static readonly petshop. idal. iorder Dal = petshop. dalfactory. dataaccess. createorder ();

/// <Summary>
/// Inserts the order and updates the inventory stock within a transaction.
/// </Summary>
/// <Param name = "order"> all information about the order </param>
Public void insert (petshop. model. orderinfo order ){

Using (transactionscope Ts = new transactionscope (transactionscospontion. Required )){

Dal. insert (order );

// Update the inventory to reflect the current inventory after the order submission
Inventory inventory = new inventory ();
Inventory. takestock (order. lineitems );

// Calling complete commits the transaction.
// Excluding this call by the end of transactionscope's scope will rollback the transaction
TS. Complete ();
}
}
}

The Code implements the insert () method defined in the parent interface and updates the number of items through a transaction.

Compare the oderasynchronous class code:

Public class orderasynchronous: iorderstrategy
{
// Get an instance of the messagingfactory
// Making this static will cache the messaging instance after the initial load
Private Static readonly petshop. imessaging. iorder asynchorder = petshop. messagingfactory. queueaccess. createorder ();

/// <Summary>
/// This method serializes the order object and send it to the queue for asynchronous Processing
/// </Summary>
/// <Param name = "order"> all information about the order </param>
Public void insert (petshop. model. orderinfo order ){

Asynchorder. Send (order );
}
}

The order operation to be processed by the insert () method is completely handed over to the Message Queue for processing. asynchorder is an MSMQ object in the class. The "send" method of some objects is used to send messages, and the messages are stored in the system's MSMQ.

MSMQ Introduction

Message Queue (MSMQ), which enables message queues that are not running at the same time.ProgramCan communicate between different types of networks and systems that may be temporarily offline. A solution for synchronous and asynchronous message transmission. message Processing is a key aspect of asynchronous processing. MSMQ is a Windows component and is not installed by default. you can install it by adding or deleting an application.

The message processing class is located in the "system. messaging" namespace. The following describes several important objects and methods:

1. initialize the message team code:

Messagequeue MQ = new messagequeue ("hostname \ quequname ");

The name of the queue must be specified in the message queue.

2. You can also use the path attribute to reference a message queue.

Messagequeue MQ = new messagequeue ();

MQ. Path = "hostname \ quequname ";

3. Use the create method to create a queue programmatically:

Messagequeue MQ = new messagequeue. Create ("hostname \ quequname ");

4. send information

Message Queue. Send (1000 );

MQ. Send ("test ");

5. receive messages. The recerve method permanently deletes messages from the queue when receiving messages. The peek method does not remove messages from the Message Queue after receiving messages.

MQ. recerve ();

MQ. Peek ();

The message interface design is used in petshop4. All message-related processing classes are stored in the imessaging and msmqmessaging projects, and the iorder interface is defined in the imessaging project, this interface is implemented in the order class in msmqmessaging, and the order class also inherits the petshopqueue class. The following is the order structure diagram:

The Order class is a key class for implementing message queues. The send and receive methods do not complete queue operations, but call the methods in its parent class petshopqueue.

Asynchronous order submission

We have studied the message processing class used in the system to Implement Asynchronous submission. Next we will implement the asynchronous order submission function.

First, create MSMQ: After MSMQ is installed, you can find the "message queue" node in Computer Management and create a "dedicated queue". The name must be the same as that defined in the configuration file, select "transactional ".

Next, modify the configuration file to Implement Asynchronous functions.

Next, create a Windows service to process orders in the background. The service program obtains data from the queue according to the specified time interval and submits the order. All of this is automatically completed in the background. Note that this service uses multiple threads. Run the "installutil" command to load the service program. In Windows Service Management, set the service to run automatically. In this way, when the order is submitted asynchronously in the petshop4 program, the order data will appear in the dedicated queue of the message queue.

Uidesign of order Module

The order submission interface uses the wizard control to create multiple steps in Wizard form.

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.