tags:

views:

479

answers:

2

Similar to the one here on StackOverFlow, I would be needing to implement a way for people to vote up and vote down comments in a forum like site.

However instead of having a generic overall score, we will display the total amount of "thumbs up" and "thumbs down". The overall score will be needed for filtering purposes, such as "sort by highest rated", "show only ratings with + 3"

What is the best implementation strategy?

As a user suggested, I would also be storing the information who casted the vote

+1  A: 

Well you'll need to store ratings (Comment ID, UserID, Vote-Value) so you can calculate and stop duplicate voting but I would strongly suggest you also add a VotesUp and VotesDown fields on your main comment entity.

Why the duplication? Speed. You're going to be doing disgusting amounts of SUM-WHERE statements otherwise and they'll run your database server into the ground. A few extra bytes on the header record and you'll be able to sort and filter to your heart's content.

Edit: If you're going to also sort by overall score in some cases, you might want to add a third field (VoteTotal).

Edit 2: The duplication is pointless if you're able to cache all the comment headers in memory. That's a lot of data though and you'd need a ton of memory to effectively cache it. If you're not a billionaire, I'd just duplicate the data.

Oli
Do you recommend incrementing/decrementing the comment header summed values at the same time as the writing of the vote info (who,when,value) to the database in a database transaction to prevent possible inconsistancies or perhaps a scheduled task that updates the summed values frequently?
Sprogz
Good question but I think data should always be kept accurately. If you need it to scale to the moon, as I said in my second edit, by storing object-representations of the data in memory/cache you can cut out a LOT of the DBIO and make it fly.
Oli
Ie. If you had all your headers in RAM, voting would cause 2 writes to DB and that's it. Without caching, you would write the vote record, then SUM based on a WHERE, and write again... Which, needless to say, is pretty nasty. If you can't cache the votes, perhaps that scheduling the updates is best?
Oli
A: 

A good open source reference for some but not all you ask would be dotnetkicks

Also listen to this StackOverFlow podcast where the creators of this site discuss how they worked out some of their requirements. Required listening if you're planning the above.

Then once you're there, linking in with a service like uservoice would give you some valuable feedback early in your development.

dove