使用 BsonClassMap,是否可以映射域对象引用,同时保持域对象程序集持久无知(将public A Reference { get; set; }
属性更改为下面public MongoDBRef Reference{ get; set; }
的示例类B
是不可接受的)。
对于这种情况,引用的对象不是同一聚合的一部分,不应存储为嵌套文档。
是否可以在这样的关系中映射两个域对象:
public class A
{
public Guid Id {get; private set; }
}
public class B
{
public Guid Id { get; private set; }
public A Reference { get; set; }
}
进入如下文档结构:
// 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"
}
映射可能如下所示:
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?
}
那么,在不更改模型的情况下,我如何将Reference
属性映射到 object A
,从 objectB
作为 DBRef 或作为手动引用(如上面的示例文档结构)?
Is this possible using BsonClassMap? Or in order to use BsonClassMap and keep my domain assembly persistent ignorant, do 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.
}