The database design should include the auction end date and time. On the UI side you can write a count down timer for which there are many examples online.
How to register same event for different times ASP.MVC C#
I'm building an Auction Web App in Asp.Net MVC and I have an auction expiration time for each auction item. It means whenever that time will reach the auction will be ended for that particular item and a flag of Auction Ended will be set in the database for the particular auction item. Now I'm struggling with how I can initialize a timer or background job or something like that for each auction item so whenever the expiration time will reach it will change the item status to Auction Ended immediately in the database. I have worked with Schedulers and Timers in the Winforms but I think it's not a better idea to have a separate scheduler for each auction item.
Looking forward to any suggestion and idea.
Thanks
Developer technologies ASP.NET Other
Developer technologies C#
3 answers
Sort by: Most helpful
-
-
Jose Zero 576 Reputation points
2022-03-08T12:03:50.037+00:00 Did you considered Background Jobs using Quartz or HangFire?
www.quartz-scheduler.net
www.hangfire.ioReviewed my answer to include FrontEnd alternative.
Since it is an webapp in ASP.NET MVC looks you can have Background Jobs running on server, that controls when Auction Start/End, and SignalR at client FrontEnd, so Background Job can notify client about Start/End auction. -
AgaveJoe 30,126 Reputation points
2022-03-08T14:34:39.173+00:00 It seems you made up your mind before making this post even though you initially asked for suggestions and ideas.
In my opinion, you are over complicating the design. Replace the timer and the flag column with a simple WHERE clause. The same WHERE logic that the timer must use to set the flag. Take a look at this basic SQL example.
IF OBJECT_ID(N'tempdb..#Auction') IS NOT NULL DROP TABLE #Auction IF OBJECT_ID(N'tempdb..#AuctionBids') IS NOT NULL DROP TABLE #AuctionBids CREATE TABLE #Auction( AuctionId INT IDENTITY(1,1), Item VARCHAR(10), StartDate DATETIME, EndDate DATETIME ) CREATE TABLE #AuctionBids( AuctionBidId INT IDENTITY(1,1), AuctionId INT, BidValue DECIMAL ) INSERT INTO #Auction(Item, StartDate, EndDate) VALUES ('Widget', DATEADD(second, -1, GETDATE()), DATEADD(second, 10, GETDATE())) INSERT INTO #AuctionBids(AuctionId, BidValue) VALUES(1, 10.00) INSERT INTO #AuctionBids(AuctionId, BidValue) SELECT a.AuctionId, 11.00 FROM #Auction AS a WHERE a.AuctionId = 1 AND GETDATE() BETWEEN a.StartDate AND a.EndDate
A bid cannot be inserted if the auction end date has passed. Your design uses the same concept but sets a flag by DateTime with a timer. Why add all the extra logic when a simple SQL query using GETDATE() or DateTime.Now determines if the auction has ended?