Using persistent agnostic domain objects, how do I map a reference property between two objects using BsonClassMap?
In the NH/FluentNh world (I know, relational), you would use something like:
Reference(c=>c.ReferenceProperty, "reference_id");
How do you achieve the similar sort of thing for MongoDB using BsonClassMap? I know, no foreign keys, but the relationship can be manually encoded (as in `{entity}_id`) or as a DbRef. The former I understand being preferred.
For example, if I have the following classes:
public class A
{
public Guid Id { get; protected set; }
}
public class B
{
public Guid Id { get; protected set; }
public A ReferenceProperty{ get; set; }
}
Where A is not an aggregate of B, and so I don't want to embed the A document within the document of B. Instead I want a reference field in the B document to the _id of the A document.
So, how would I map `ReferenceProperty` using the BsonClassMap to achieve the following document structure:
// collectionAs
{ _id: "guid-for-A" }
// collectionBs
{ _id: "guid-for-B", ReferenceProperty_id: "guid-for-A" }
I know the BsonClassMap for A and B would look something like this (with a gap in my knowledge for how to map the `ReferenceProperty`):
BsonClassMap.RegisterClassMap<A>(cm =>
{
cm.MapIdProperty(c => c.Id)
.SetIdGenerator(new GuidGenerator())
.SetRepresentation(BsonType.String);
});
BsonClassMap.RegisterClassMap<B>(cm =>
{
cm.MapIdProperty(c => c.Id)
.SetIdGenerator(new GuidGenerator())
.SetRepresentation(BsonType.String);
cm.MapProperty(c => c.ReferenceProperty); // this embeds the document, how do I map a *_id field or a dbref?
});
Is this even possible, or do I need to change the model to keep it persistent agnostic (not include MongoDbRef as the `ReferenceProperty` type, etc) to achieve this aggregate structure in the database? A model change could be:
public class A
{
public Guid Id { get; protected set; }
}
public class B
{
public Guid Id { get; protected set; }
public Guid ReferencePropertyId{ get; set; }
}