Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a Newbe.Claptrap project

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about how to create a Newbe.Claptrap project, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Newbe.Claptrap is a distributed development framework for dealing with concurrency problems easily.

Install the project template

Open the console and run the following command to install the latest project template:

Dotnet new-install Newbe.Claptrap.Template

After the installation is complete, you can see the installed project template in the installation results.

Create a project

Select a location and create a folder. In this example, you choose to create a folder called HelloClaptrap under D:\ Repo. This folder will be used as the code folder for the new project.

Open the console and change the working directory to D:\ Repo\ HelloClaptrap. Then run the following command to create the project:

Dotnet new newbe.claptrap-name HelloClaptrap

Generally speaking, we recommend that you create D:\ Repo\ HelloClaptrap as the Git repository folder. Manage your source code through version control.

Start the project

Next, we use the command line to start the project. Switch the command line to C:\ Repos\ HelloClaptrap\ HelloClaptrap and run the following command:

Tye run

After startup, you can see all the projects contained in the project template on tye dashboard:

The address of tye dashboard is usually http://localhost:8000, and other ports will be used automatically if the port is occupied. You can check the prompt on the command line to get the current specific address.

We can find the running address of the helloclaptrap-webapi service on the interface shown above. For example, as shown in the figure above, the endpoint address is http://localhost:14285.

Therefore, we use a browser to open the address to view the swagger interface.

On the swagger page, try calling / AuctionItems/ {itemId} / statusAPI:

newbe.claptrap AuctionItems

If the service returns 200, it means that each component of the current service has started normally.

Experience project

The project created using the project template is actually a program that simulates the auction bidding business.

Auction bidding is a typical business scenario that may have concurrent requests to be processed. This problem can be easily solved with Newbe.Claptrap. We will continue to use this business scenario for demonstration in subsequent documents, so here is a simple description of the business scenario.

Business rules

The business rules are roughly as follows:

Each auction item consists of a unique itemId

Auction items can only be auctioned within a period of time.

The auction item has a starting auction price.

All bidders have a unique userId

The bidder may bid for the auction item indefinitely during the auction period, and as long as the bid is larger than the current highest bid, it can be counted as a valid bid and become the current winning bidder of the auction item.

Details of all successful bids need to be recorded, including the time of the bid, the amount of the bid, and the bidder.

The status of the auction items is as follows:

0 Planned waiting to start shooting

1 OnSell is at auction

2 Sold has been photographed

3 UnSold streaming beat

API design

For the simplest demonstration, the following API is designed for this example:

GET / AuctionItems/ {itemId} / status to obtain the current bidding status of the specified auction item

GET / AuctionItems/ {itemId} get the details of the specified auction item

POST / AuctionItems bid for the designated auction item

Let's use a simple scenario to experience the effects of these API.

Look for the auction items currently under sale

As the status of auction items is affected by time, in order to enable developers to find auction items in various states at any time, some time-based algorithms are used to generate auction items in all states.

Developers can call GET / AuctionItems/ {itemId} / status to get the current status of the auction item using the four itemId of 0Uniqqqqq2Action3.

There is at least one auction item with a status of 1 OnSell. For further illustration, let's assume that the itemId is 1.

View the details of the auction item

Use GET / AuctionItems/ {itemId} to query the details of the auction items. For example, if we use itemId as 1 to query, we might get the following result:

{state: {"biddingRecords": null, "basePrice": 10, "startTime": "2021-02-27T12:59:12.673013+08:00", "endTime": "2021-02-27T16:59:12.673013+08:00"}}

The above results show that:

The starting price for the item is basePrice 10.

The auction period is the startTime-endTime period.

The current auction record biddingRecords is empty

The time period may change due to the time it takes to start the project template, depending on the specific project start time.

Try to bid

Next, we call POST / AuctionItems to try to bid on the current auction item, and pass the following parameters:

{"userId": 1, "price": 36524, "itemId": 1}

The parameters are described as follows:

Bidder userId is 1

The bid is 36524

The auction item is itemId 1.

This will result in:

{"success": true, "userId": 1, "auctionItemStatus": 1, "nowPrice": 36524}

The returned result shows that:

Success bid is successful

Bidder userId is 1

The latest bid is 36524.

The current status of the auction item is 1 OnSell.

Then you can view the latest auction items by using GET / AuctionItems/ {itemId}:

{"state": {"biddingRecords": {"36524": {"userId": 1, "price": 36524, "biddingTime": "2021-02-27T07:31:09.8954519+00:00"}}, "basePrice": 10, "startTime": "2021-02-27T12:59:12.673013+08:00" "endTime": "2021-02-27T16:59:12.673013+08:00"}

The above results show that:

The bid record has been updated to add the latest auction details.

This completes the simplest demonstration of the bid.

Developers can try more different states and parameters to experience the basic usage of the above API. For example, the bid is less than the current highest bid, making bids for non-resale items, and so on.

Stop the project

To stop the currently running template project. Just press Ctrl+C with the keyboard on the console where you just ran tye run to stop the running program.

The above is how to create a Newbe.Claptrap project. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report