I have on hand a system of database-driven entities which very seriously use the relational aspect of the database.
Like for example, a table to describe animals. Then a table to describe insects. Then a table that describes relationships between animals and insects and the nature of it. This table is keyed by foreign keys from the first two tables.
In the end, I get an entity Animal that looks like this:
public class Animal {
Guid ID;
String Name;
Range<Int> mass;
Enum physique;
Enum dietType;
Enum lifestyleType;
IQUeryable<Insects> insects;
}
Where each Insects is like this:
public class Insects{
Guid ID;
Guid animalID;
Guid insectID;
Animal animal;
Insect insect;
Enum relationship;
}
Relationships can be something like “animal eats insect”, or “insect feeds on animal corpse”, or “symbiosis”, or “parasitic”, etc…
The entity Insect is as follows:
public class Insect{
Guid ID;
String name;
Enum wingType;
Enum socialStructure;
String camouflage;
IQueryable<Animals> animals;
}
Where each Animals is an alias for entity “Insects” we saw previously.
So when I create a new EcosystemEntities db, I can query it for… let’s say db.Animals.Where(x => x.Name.Contains("Lupus")).ToList()
This gives me a List where each Animal contains a List, where each Insects contains an Insect, where Insect contains List, where each Animals contains an Animal, where each Animal … et cetera.
Potentially I can have any single Animal, through these links, connected to all other insects and animals in the database. When I have a single Animal variable like that, is it de-facto equivalent to loading whole EcosystemEntities db
into memory, just more difficult to traverse? What happens to a List? Does it essentially have EcosystemEntities db
repeated i times? Would I be better off using a single EcosystemEntities db
and multiple List<Guid>
by which I would use db.Insects.Where(i => db.Relationships.Where(r => animalIDs.Contains(r.animalID)).Contains(i.ID)).Select(i => i.name).ToList();
? Should I be worried about performance when I'm using Linq in this context?
My database can describe tens of thousands of distinct entities across, say, 15 types, and the number of connections between just two types of entities is in hundreds of thousands or even over a million.