I have a Winforms Control from a 3rd party developer and want to store its data directly in a MongoDB. It is a gantt chart which has a list of Resources and a list of Appointments. The Appointments have also a list of Resources to which is responsible for the mapping of Appointments to Resources.
To simplify the storage I created a class called "PlanningDocument":
public class PlanningDocument
{
[BsonId]
public ObjectId Id { get; set; }
public string Name { get; set; }
public ResourceCollection Resources { get; set; }
public ItemCollection Schedule { get; set; }
}
The Document gets stored correctly in the database (I'm using MongoVUE to check the content of the database). But the Items in the "Schedule" collection do not contain references to the Resources as it is during runtime of the program.
What do I have to do to store referenced relations between objects?
EDIT: I'll try to explain what I want to do with a more concrete example:
public class Identity
{
public string Id;
public Identity()
{
Id = Guid.NewGuid().ToString();
}
}
public class Thing : Identity
{
public string Name = "";
}
public class Drawer : Identity
{
public List<Thing> SomeThings;
public List<Thing> AdditionalThings;
public Drawer()
{
SomeThings = new List<Thing>();
AdditionalThings = new List<Thing>();
}
}
I have these classes "Thing" and "Drawer". The Drawer can contain Things in "SomeThings" and "AdditionalThings".
var d = new Drawer();
d.AdditionalThings.Add(new Thing() { Name = "Thing 1"});
d.AdditionalThings.Add(new Thing() { Name = "Thing 2" });
d.AdditionalThings.Add(new Thing() { Name = "Thing 3" });
d.SomeThings.Add(new Thing() { Name ="Thing A"});
d.SomeThings.Add(d.AdditionalThings[0]);
When I store the same Thing in both Lists (like done with "Thing 1" here), MongoDB stores a copy of Thing 1 - not a reference.
My Question is: does a generic way exist to make MongoDB and the C# driver store a reference here instead of a copy?
Thanks in advance Achim
No, there is no way to store a reference automatically in the C# driver. This is by design as storing references automatically would imply that we would automatically resolve those references at run time, which we do not do.
Since your "thing" objects have an identity, then it won't be difficult to keep them in sync when you update one of them, either by doing it client-side or by writing the appropriate update statement using the positional $ operator and the update-multi flag.