I'm going through some sort of phase where I over analyze and second guess every single decision I make when attempting to write software that has preventing me from getting anything done.
I recently came across a need to store a"complex" C# object in a SQLite database. To boil down the object into a minimal example lets just say that this object, of type Node, has a list of children of type Leaf. While defining the classes most would agree it is good practice to keep any references to Node out of Leaf, this allows Leaf to be independent, reusable, and have a "single purpose".
public class Node
{
public string Name { get; set; }
public List<Leaf> Leaves { get; set;}
}
public class Leaf
{
public string Name { get; set; }
public int SomeValue { get; set; }
}
Now, when writing up the SQL table definitions, pre-meltdown plast1k would do something like this (and I assume the rest of the world would too):
Nodes
-ID (int, identity)
-Name (varchar)
Leaves
-ID (int, identity)
-Name (varchar)
-SomeValue (int)
-NodeID (int, foreign key)
-Index (int)
But we've now created a dependency between Leaf and Nodes. If you look carefully we also created a dependency between Leaf and List<>. The more I think about it (stop me here if that is in fact the only problem) the more I don't like this.
I have limited practical database design experience. My instinct was to start factoring out the relationship, which I later learned is called a bridge/linking table, and seems to be standard practice when the relationship is many to many.
Leaves
-ID (int, identity)
-Name (varchar)
-SomeValue (int)
LeavesMapping
-LeafID
-NodeID
-Index
This seemed fine at first, but started getting difficult to manage with real my real life data. I've also decided to optimized for re-ordering of the list items and am modeling it in the database as a linked list (using NextLeaf rather than an index, for example).
Is this really the right way to do this? Are there any alternatives? What constructs are missing from relational data models that force us to do this? Are there any databases that can model this in the same way that the code does?