How do you use BsonClassMap to map the properties of a POCO domain object as a guide or DBRef reference?

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); // How do I map the B.Reference to a manual reference (like // the sample doc structure above) or possibly as a DBRef? } 

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; } // Don't reference the object directly, // just store the Guid to the // referenced object. } 
+4
source share
1 answer

I asked the same question to the mongodb-csharp user group and got an answer from craiggwilson:

You will need to change your ReferenceProperty to ReferencePropertyId. We do not support lazy loading (or loading) of referenced documents.

Since A is not a collection for B, this makes sense when discussed in these terms. Generally, it is not necessary to download the link aggregate (B) to process the link aggregate (A). Perhaps you really need some information from B. In this case, think about denormalizing a little and create a true entity (BSummary), the totality of which is A. This makes sense if any summary information is unchanged or rarely changes.

+2
source

All Articles