With this reading, you're ready to try using Claptrap to implement your business.
该文档仅适用于 0.7 及以下版本，若想要查看最新版本内容，请点击右上角进行切换。 :::
In this article, I learned how to use Minion in an existing project sample to complete asynchronous business processing by implementing the requirements of "ordering goods".
First, take a look at the business use cases involved in this article：
- The user can place an order, which will be placed using all SKUs in the current shopping cart to form an order.
- The inventory of the relevant SKU will be deducted after the order is made.If an SKU is out of stock, the order fails.
- Ordering is only until the inventory deduction is successful, and the next steps do not require the scope of this sample discussion.Therefore, after this example is successfully placed, an order record is generated in the database to indicate the end of the order creation.
Although this article focuses on the use of Minion, it does require knowledge of the previous "Defining Claptrap" because of the need to use a new OrderGrain object.
Minion is a special Claptrap, and its relationship to MasterClaptrap is shown in the following image：
Its main development process is similar to Claptrap's, with only a few cuts.Compare the following：
|Define the Grain interface||√||√|
|Sign up for Grain||√||√|
|Sign up for EventHandler.||√||√|
The reason for this reduction is that because Minion is an event consumer for Claptrap, the definition of event-related does not need to be handled.But other parts are still necessary.
At the beginning of this article, we will no longer list the specific file location of the relevant code, hoping that readers will be able to find their own in the project, in order to master.
Based on the knowledge related to the previous "Defining Claptrap", we implement an OrderGrain here to represent the order order operation.To save space, we list only the key parts of it.
The status of the order is defined：
- OrderCreated indicates whether the order has been created and avoids creating the order repeatedly
- UserId under the single user ID
- Skus orders contain SkuIds and order quantities
The order creation event is defined as follows：
- OrderGrain implements the core logic of order creation, where the CreateOrderAsync method completes shopping cart data acquisition and inventory deduction-related actions.
- The relevant fields in State will be updated after orderCreatedEvent has been successfully executed and are no longer listed here.
From the beginning of the series to this, we never mentioned database-related operations.Because when you're using the Claptrap framework, the vast majority of operations have been replaced by "write to events" and "state updates," you don't need to write database operations yourself at all.
However, since Claptrap is usually designed for a single object (one order, one SKU, one shopping cart), it is not possible to obtain all (all orders, all SKUs, all shopping carts) data.At this point, you need to persist the state data into another persistence structure (database, file, cache, etc.) in order to complete queries or other operations for the entire situation.
The concept of Minion was introduced into the Claptrap framework to address these requirements.
Next, we introduce an OrderDbGrain (a Minion) in the sample to asynchronously complete OrderGrain's order-in operation.
Minion is a special Claptrap, in other words, it is also a Claptrap.ClaptrapTypeCode is required for Claptrap and needs to be added to this definition.
Because this sample only needs to write an order record to the database and does not require any data in State, this step is not actually required in this sample.
- ClaptrapMinion is used to mark the Grain as a Minion, where Code points to its corresponding MasterClaptrap.
- ClaptrapState is used to mark Claptrap's State data type.In the previous step, we made it clear that the Minion does not need StateData, so use NoneStateData instead of the built-in type of the framework.
- IClaptrapMinionGrain is the Minion interface that differs from IClaptrapGrain.If a Grain is Minion, you need to inherit the interface.
- ClaptrapCodes.OrderGrain and ClaptrapCodes.OrderDbGrain are two different strings, and I hope the reader is not an interstellar patrist.
Star Master：Because of the fast pace of StarCraft competition, the amount of information, players can easily ignore or misjudge some of the information, so often "players do not see the key events that occur under the nose" funny mistakes.Players thus joke that interstellar players are blind (there was really a blind and professional duel), the higher the segment, the more serious the blind, professional interstellar players are blind.
- MasterEventReceivedAsync is a method defined from IClaptrapMinionGrain that means receiving event notifications from MasterClaptrap in real time.Don't expand the description here, just follow the template above.
- WakeAsync is the method defined from IClaptrapMinionGrain, which represents masterClaptrap's active wake-up of Minion.Don't expand the description here, just follow the template above.
- When the reader views the source code, they find that the class is defined separately in an assembly.This is just a classification method that can be understood as classifying Minion and MasterClaptrap in two different projects.It's actually no problem to put it together.
Here, because we define OrderDbGrain in a separate assembly, additional registration is required for this assembly.As follows：
- IOrderRepository is an interface that operates directly on the storage tier for the addition and deletion of orders.The interface is called here to implement the incoming operation of the order database.
In fact, to save space, we've registered in the code for the "Implement Grain" section.
Because StateData has no special definition, there is no need to implement IInitialStateDataFactory.
In the example, we added OrderController to place orders and query orders.Readers can view it at the source code.
Readers can use the following steps to perform a real-world：
/api/cart/123""skuId": "yueluo-666", "count":30" add 30 units of yueluo-666 concentrate to the 123 shopping cart.
/api/order"userId": "999", "cartId": "123"" as 999 userId, from the 123 shopping cart to place an order.
/api/ordercan be viewed through the API after the order has been placed successfully.
/api/sku/yueluo-666the SKU API can view the inventory balance after the order has been ordered.
At this point, we have completed the "commodity order" this requirement of the basic content.This sample gives you a first look at how multiple Claptraps can work together and how to use Minion to accomplish asynchronous tasks.
However, there are a number of issues that we will discuss later.
You can get the source code for this article from the following address.：