i have implemented repository pattern base entity class collections. till collections had _id of objectid type. in code, needed represent id string.
here how entitybase class like
public abstract class entitybase { [bsonrepresentation(bsontype.objectid)] public virtual string id { get; set; } } here mapping:
bsonclassmap.registerclassmap<entitybase>(cm => { cm.automap(); cm.mapidproperty(x => x.id).setidgenerator(stringobjectidgenerator.instance); cm.idmembermap.setserializer(new stringserializer().withrepresentation(bsontype.objectid)); }); now have language collection id plain string en-gb.
{ "_id" : "en-gb", "term1" : "translation 1", "term2" : "translation 2" } language class inheriting entitybase class
public class language : entitybase { [bsonextraelements] public idictionary<string, object> terms { get; set; } public override string id { get; set; } } the question can somehow change how id serialized language class?
i don't want change behaviour of entitybase class since have lot of other collections inheriting entitybase.
update
here tried , got exception. not sure if have tried possible.
bsonclassmap.registerclassmap<language>(cm => { cm.automap(); cm.mapextraelementsmember(c => c.terms); cm.mapidproperty(x => x.id).setidgenerator(stringobjectidgenerator.instance); cm.idmembermap.setserializer(new stringserializer().withrepresentation(bsontype.string)); }); here exception getting:
an exception of type 'system.argumentoutofrangeexception' occurred in mongodb.bson.dll not handled in user code additional information: memberinfo argument must class language, class entitybase. @ mongodb.bson.serialization.bsonclassmap.ensurememberinfoisforthisclass(memberinfo memberinfo) @ mongodb.bson.serialization.bsonclassmap.mapmember(memberinfo memberinfo) @ mongodb.bson.serialization.bsonclassmap`1.mapmember[tmember](expression`1 memberlambda) @ mongodb.bson.serialization.bsonclassmap`1.mapproperty[tmember](expression`1 propertylambda) @ mongodb.bson.serialization.bsonclassmap`1.mapidproperty[tmember](expression`1 propertylambda) @ test.utilities.mongodbclassconfig.<>c.<configure>b__0_1(bsonclassmap`1 cm) in f:\development\test\utilities\mongodbclassconfig.cs:line 23 @ mongodb.bson.serialization.bsonclassmap`1..ctor(action`1 classmapinitializer) @ mongodb.bson.serialization.bsonclassmap.registerclassmap[tclass](action`1 classmapinitializer) @ test.utilities.mongodbclassconfig.configure() in f:\development\test\utilities\mongodbclassconfig.cs:line 20 @ test.portal.backend.startup..ctor(ihostingenvironment env) in f:\development\test\startup.cs:line 43
when use attribute [bsonrepresentation(bsontype.objectid)] telling serializer, though string — want represented objectid type. "en-gb" not valid objectid therefor, serializer throws exception.
use [bsonid]-attribute if want specify property named id must unique identifier within collection.
there objectid-data type within driver, can implement instead.
i add generic type-parameter entitybase-class.
public abstract class entitybase<tid> { [bsonid] public tid id { get; set; } } you implement entitybase-class, without generic type argument such:
public abstract class entitybase : entitybase<objectid> { } then language-class
public class language : entitybase<string> { [bsonextraelements] public idictionary<string, object> terms { get; set; } } the driver should not have trouble serializing string-representation of objectid objectid; try first — make sure.
Comments
Post a Comment