Is there a better way to store c# event information in a database than storing it's name and using late binding to execute it?

StackOverflow https://stackoverflow.com/questions/16008360

  •  04-04-2022
  •  | 
  •  

Question

I'm designing a card game (think Magic the Gathering for purposes of this example) and want to take the information for the cards and store it in a database. In this game, there are events (for instance, one card might say "when this comes into play, opponent takes 2 damage") that are tied to a particular card. The design decisions have led to loosely building the cards in a builder factory, but I'm looking to take the cards and store them in a database instead. Since most of the cards are instances of a base "Card" class, it's easy to load the features common to every card (name, cost, etc.) but I've struggled to find a good way to tie these events to a single type of card. The only way I have thought of so far was to store the function name in the database and use late binding to register the event when the card is loaded. Is there a better way to do this?

The only similar post I've found is this: Store function name in database and then execute it

The answer of using eval() seems similar to late binding, but got down-voted. However, no one had a better suggestion how to perform this function.

Was it helpful?

Solution

This sounds like a usable approach, however it is questionable if it is a good one.

Why don't you construct propper objects from a card representation in the database? Either by using an object database, or, more likely, but using object-relation-mapping. THis way you can represent each type of card in a clean and easy to read and use way yet work with rich instances of specialized classes derived from a common base class.

So you use a common table to store all the cards like this:

+-------------------------------------------------+
| card type | data1 | data2 | data3 | ... | dataN |
+-------------------------------------------------+
| Card      | 123   | 456   | 789   | ... | abc   |
| CardS1    | 123   | 456   | 789   | ... | abc   |
| CardS3    | 123   | 456   | 789   | ... | abc   |
| CardS2    | 123   | 456   | 789   | ... | abc   |
...

And a class hierarchy like this:

+---------------------------+
| class Card                |>-+
+---------------------------+  |
| var data1                 |  |
| var data2                 |  |
| var data3                 |  |
| ...                       |  |
| var dataN                 |  |
| baseMethod1()             |  |
| baseMethod2()             |  |
+---------------------------+  |
                               |
+---------------------------+  |
| class CardS1. public Card |<-+
+---------------------------+  |
| specialMethod_1_1()       |  |
+---------------------------+  |
                               |
+---------------------------+  |
| class CardS2. public Card |<-+
+---------------------------+  |
| specialMethod_2_1()       |  |
| specialMethod_2_2()       |  |
+---------------------------+  |
                               |
+---------------------------+  |
| class CardS3. public Card |<-+
+---------------------------+
| specialMethod_3_1()       |
+---------------------------+
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top