Re: Limiting C# driver dependency only to the Persistence Layer (Entity dependency on ObjectId)

180 views
Skip to first unread message

craiggwilson

unread,
Dec 11, 2012, 8:58:29 AM12/11/12
to mongod...@googlegroups.com
There is a fairly extensive convention system such that you wouldn't need to do this for every entity.  See the docs here: http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial#CSharpDriverSerializationTutorial-Conventions.  You can create a custom ISerializationOptionsConvention to do exactly what you need above.

To answer your question, let me ask another one.  Do you intend every class implementing IEntity to be stored in the same collection?  If not, then you don't need discriminators and it would be best to register them each separately anyways.  The ultimate problem with your statement above is that we don't know how to instantiate an IEntity when we read it back in from the database.  We can't instantiate an interface...

On Saturday, December 8, 2012 6:33:06 PM UTC-6, Akshay Chand wrote:
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?
Reply all
Reply to author
Forward
0 new messages