In BalloonShop, the pipeline will look like the one in Figure 14-2.
Figure 14-2. The BalloonShop order pipeline
The tasks carried out in these pipeline sections are, in order:
• Customer Notification: An email is sent notifying the customer that order processing has started and confirming the items to be sent and the address that goods will be sent to.
• Credit Card Authorization: The credit card used for purchasing is checked, and the total order amount is set aside (although no payment is taken at this stage).
• Stock Check: An email is sent to the supplier with a list of the items that have been ordered. Processing continues when the supplier confirms that the goods are available.
• Payment: The credit card transaction is completed using the funds set aside earlier.
C H A P T E R 1 4 ■ O R D E R P I P E L I N E 519
• Shipping: An email is sent to the supplier confirming that payment for the items ordered has been taken. Processing continues when the supplier confirms that the goods have been shipped.
• Customer Notification: An email is sent notifying the customer that the order has been shipped, and thanking them for using the BalloonShop web site.
■Note In terms of implementation, as you’ll see shortly, there are actually more stages than this, because the stock check and shipping stages consist of two pipeline sections: one for sending the email and one that waits for confirmation.
As orders flow through this pipeline, entries are added to a new database table called Audit. These entries can be examined to see what has happened to an order and are an excellent way of identifying problems if they occur. Each entry in the Orders database also will be flagged with a status, identifying which point in the pipeline it has reached.
To process the pipeline, you’ll create classes representing each stage. These classes carry out the required processing and then modify the status of the order in the Orders database to advance the order. You’ll also need a coordinating class (or processor), which can be called for any order and will execute the appropriate pipeline stage class. This processor will be called once when the order is placed, and in normal operation, will be called twice more: once for stock confirmation and once for shipping confirmation.
To make life easier, you’ll also define a common interface supported by each pipeline stage class to enable the order processor class to access each stage in a standard way. You’ll also define several utility functions and expose several common properties in the order processor class, which will be used as and when necessary by the pipeline stages. For example, the ID of the order should be accessible to all pipeline stages, so to save code duplication, you’ll put that information in the order processor class.
Now, let’s get on to the specifics. You’ll build a series of classes that we’ll refer to collectively as the CommerceLib classes. These classes could be contained in a separate assembly, but for simplicity, we’ll include them in the BalloonShop code. This also simplifies access to customer information because you’ll have access to the user profile classes defined by ASP.NET, as used in the last chapter. To differentiate the code from the existing code, however, you’ll place all the CommerceLib files in a subdirectory of the App_Code directory and in a CommerceLib namespace. The CommerceLib directory will contain the following classes:
• OrderProcessor: The main class for processing orders.
• OrderProcessorException: The custom exception class for use in the order processor and pipeline sections.
• IPipelineSection: The interface definition for pipeline sections.
• Customer, OrderDetails, OrderDetail: The classes used to store data extracted from the database, for ease of access.
• PSInitialNotification, PSCheckFunds, PSCheckStock, PSStockOK, PSTakePayment, PSShipGoods, PSShipOK, PSFinalNotification: The pipeline section classes.
The progress of an order through the pipeline as mediated by the order processor relates to the pipeline shown earlier (see Figure 14-3).
Figure 14-3. CommerceLib pipeline processing
The process shown in Figure 14-3 is divided into three sections as follows:
• Customer places order.
• Supplier confirms stock.
• Supplier confirms shipping.
C H A P T E R 1 4 ■ O R D E R P I P E L I N E 521
The first stage is as follows:
1. When the customer confirms an order, Checkout.aspx creates the order in the database and calls OrderProcessor to begin order processing.
2. OrderProcessor detects that the order is new and calls PSInitialNotification.
3. PSInitialNotification sends an email to the customer confirming the order and advances the order stage. It also instructs OrderProcessor to continue processing.
4. OrderProcessor detects the new order status and calls PSCheckFunds.
5. PSCheckFunds checks that funds are available on the customer’s credit card and stores the details required to complete the transaction if funds are available. If this is successful, then the order stage is advanced and OrderProcessor is told to continue.
6. OrderProcessor detects the new order status and calls PSCheckStock.
7. PSCheckStock sends an email to the supplier with a list of the items ordered, instructs the supplier to confirm via OrderAdmin.aspx, and advances the order status.
8. OrderProcessor terminates.
The second stage is as follows:
1. When the supplier confirms that stock is available, OrderAdmin.aspx calls OrderProcessor to continue order processing.
2. OrderProcessor detects the new order status and calls PSStockOK.
3. PSStockOK advances the order status and tells OrderProcessor to continue.
4. OrderProcessor detects the new order status and calls PSTakePayment.
5. PSTakePayment uses the transaction details stored earlier by PSCheckFunds to complete the transaction, advances the order status, and tells OrderProcessor to continue.
6. OrderProcessor detects the new order status and calls PSShipGoods.
7. PSShipGoods sends an email to the supplier with a confirmation of the items ordered, instructs the supplier to ship these goods to the customer, and advances the order status.
8. OrderProcessor terminates.
The third stage is as follows:
1. When the supplier confirms that the goods have been shipped, OrderAdmin.aspx calls OrderProcessor to continue order processing.
2. OrderProcessor detects the new order status and calls PSShipOK.
3. PSShipOK enters the shipment date in the database, advances the order status, and tells OrderProcessor to continue.
4. OrderProcessor detects the new order status and calls PSFinalNotification.
5. PSFinalNotification sends an email to the customer confirming that the order has been shipped and advances the order stage.
6. OrderProcessor terminates.
If anything goes wrong at any point in the pipeline processing, such as a credit card being declined, an email is sent to an administrator. This administrator then has all the information necessary to check what has happened, get in contact with the customer involved, and cancel or replace the order if necessary.
No point in this process is particularly complicated; it’s just that a lot of code is required to put this into action!