Note that there are some explanatory texts on larger screens.

plurals
  1. POStore objects with common base class in database
    primarykey
    data
    text
    <p>Let's say i have a common base class/interface</p> <pre><code> interface ICommand { void Execute(); } </code></pre> <p>Then there are a few commands inheriting from this interface.</p> <pre><code> class CommandA : ICommand { int x; int y; public CommandA(int x, int y) { ... } public void Execute () { ... } } class CommandB : ICommand { string name; public CommandB(string name) { ... } public void Execute () { ... } } </code></pre> <p>Now i want to store these commands in a database, with a common method, and then later load all of them from the DB into a <code>List&lt;ICommand&gt;</code> and execute the Execute-method of them.</p> <p>Right now I just have one table in the DB called commands and here i store a string serialization of the object. Basically the columns in the table are: <code>id|commandType|commaSeparatedListOfParameters</code>. While this is very easy and works good for <em>loading all</em> commands, I can't <em>query</em> the commands easily without using substring and other obscure methods. I would like to have an easy way of <code>SELECT id,x,y FROM commandA_commands WHERE x=...</code> and at the same time have a generic way of loading all commands from the commands-table (i guess this would be some kind of UNION/JOIN of commandA_commands, commandB_commands, etc). </p> <p>It is important that not much manual fiddling in the DB, or manual creation of serialize/parse-methods, is required to add a new command. I have tons of them and new ones are added and removed all the time. I don't mind creating a command+table+query generation tool though if this would be required for the best solution.</p> <p>The best i can think of myself is a common table like <code>id|commandType|param1|param2|param3|etc..</code> which isn't much better (actually worse?) than my current solution as many commands are going to need null parameters and the datatype will vary so i have to resort to common string conversion again and size each field big enough for the largest command.</p> <p>The database is SQL Server 2008</p> <p><strong>Edit:</strong> Found similar question here <a href="https://stackoverflow.com/questions/3413459/designing-sql-database-to-represent-oo-class-hierarchy">Designing SQL database to represent OO class hierarchy</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload