Using BsonClassMap, is it possible to map a reference to a domain object, while maintaining constant opacity of the assembly of the domain object (changing the public A Reference { get; set; } public MongoDBRef Reference{ get; set; } to public MongoDBRef Reference{ get; set; } in the sample class B below is unacceptable).
In this case, the reference object is not part of the same aggregate and should not be stored as an attached document.
Is it possible to match two domain objects in the following ratio:
public class A { public Guid Id {get; private set; } } public class B { public Guid Id { get; private set; } public A Reference { get; set; } }
In the following document structure:
// Collection for class A { _id: "11111111-1111-1111-1111-111111111111" } // Collection class B { _id: "22222222-2222-2222-2222-222222222222", reference_id: "11111111-1111-1111-1111-111111111111" }
The mapping might look like this:
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);
So, without changing the model, how to map the Reference property to object A , from object B as either DBRef or as reference links (as in my sample document structure above)?
Is this possible with BsonClassMap? Or in order to use BsonClassMap and permanently save my domain assembly, I need to change the model to something like:
public class A { public Guid Id {get; private set; } } public class B { public Guid Id { get; private set; } public Guid ReferenceId { get; set; }
Geoff source share