views:

77

answers:

2

I am currently building a system which will have entities that will have scores like reputation etc..

I will have a service that will check for certain rules having been triggered, and will perform certain logic if they are triggered.

Previously I have used say an Enum for doing this when I have only had to store an id and a description.

public enum ShoppingCratCalculation
{
    PartialCalculation = 1,
    CompleteCalculation =2
}

But in this situation I want to carry more information, such as the modification to reputation, all in one place.

I'm essentially asking what data structure would be best suited to storing this information, for each rule in the system.

1. Description = string ("User forgot to write a review")
2. DB id = int (23)
3. Rep score modification = int (-5)

Maybe a little class (Rule) with these as properties , and then just a list?

Does anyone have any best practice suggestions for this kind of struct?

+1  A: 

You may want to look at some of the production systems. These allow you to process rules and react to actions by performing defined actions.

Marek
Looks interesting, but this is mainly about c# specific data structures.
optician
data structures do not give you any value unless you have an algorithm that operates on them efficiently. There are many existing production systems that handle peresistence. Learning about production systems provides you with some insight needed to evaluate your requirements given the constraints - you would certainly need a different data structure if you have 10 rules and if you have thousands of them.
Marek
I do think that this is interesting, and I will probably find I have to implement something like this at some point. However as I am currently in a personal RAD stage of a project, I just need to test out proof of concept.
optician
+1  A: 

I'd probably use a class to store the rule data, but the data structure that you use to store them really depends on how your rules are organized. You say it's like SO, but SO usually maps actions onto reputation. From your example, it sounds as if you are mapping qualities of an action onto reputation. In that case, using the same sort of system may not be appropriate. For example, do you really want the possibility of a negative scoring action -- SO does allow this, but perhaps you don't. After all, it sounds like your objective is to sell things -- you might want to consider the impact of giving a person a negative reputation for buying something.

Having said that, here are a few things I'd consider. If the structure is flat and only one rule can apply at any given time or each rule is independent, then a Dictionary keyed by rule identifier (perhaps an enum) might be appropriate. If more than one rule can apply and they interact, then perhaps you might want store them in a collection and build up a compound rule using the Decorator pattern from all the matching rules. For example, purchasing with an authenticated account and writing a review might give you more reputation than writing a review with an anonymous account. If the rules are hierarchical, i.e., there's some precedence, then a filter chain, with the option to abort the chain, might be more appropriate.

tvanfosson
Thanks very much, all great points. My system isn't actually a shop, it just happened that that was a simple example. It's actually going to me more similar to a computer game, and the score will actually be health. So as the user performs or doesn't perform tasks, health will be modified. Any more info you could with your thoughts on that would be greatly appreciated.
optician