Hi,
First of all, I would like to thank you for developing and enhancing MongoDB. I am a Mongo newbie and am just beginning to learn the intricacies of this technology. That said, I am trying to write an application that has multiple tiers. One of the layers is the persistence layer where I would like to perform polyglot persistence (
http://martinfowler.com/bliki/PolyglotPersistence.html). The stack above this tier interacts via an interface (IRepository) that exposes the usual CRUD operations. Also, in this layer I have an entity interface (IEntity) that has an Id property (a string). The collections in MongoDB are going to map to objects that implement this IEntity interface. This is a pretty standard RepositoryPattern implementation.
Now, in order to get the auto map feature of the Bson driver to recognize the Id property of my entities as a string, I followed the instructions from your serialization docs where it says:
"In this case the serializer will convert the ObjectId to a string when reading data from the database and will convert the string back to an ObjectId when writing data to the database (the string value must be a valid ObjectId). Typically this is done when you want to keep your domain classes free of any dependencies on the C# driver, so you don't want to declare the Id as an ObjectId."
BsonClassMap.RegisterClassMap<IEntity>(cm => {
cm.AutoMap();
cm.IdMemberMap.SetRepresentation(BsonType.ObjectId);
});
By the way, the cm.IdMemberMap line gives a Null Pointer Exception. So I changed it to:
BsonClassMap.RegisterClassMap<IEntity>(Sub(cm)
cm.AutoMap()
cm.GetMemberMap(Function(c)c.Id).SetRepresentation(BsonType.ObjectId)
End Sub)
Then in my bootstrapper class, I call the line above. When I run my code, I get the following error
"Discriminators can only be registered for classes, not for interface."
I really do not want to exclusively register every entity class this way. I could create an abstract BaseEntity class and register it that way, but I was wondering is there a reason why the above call cannot be used with interfaces?