1. The Name property has not been stored in the database since it is not configured. Serializers do not serialize properties that are not set (because if the serializer serializes such a property, it cannot deserialize it back). Therefore, if you want to serialize the Name property, just add a fake setter (in ICommand you need to add it as well):
public override string Name { get { return "Move Command"; } set{} }
2. If you do not want to use the BsonKnownTypes attribute, there is another way to notify the serializer of the types of knowledge that it may encounter during deserialization. Just register cards once, in case of application launch:
BsonClassMap.RegisterClassMap<MoveCommand>(); //all other inherited from ICommand classes need register here also
Therefore, you must use either the attribute or the KnownTypes attribute or the BsonClassMap register or register for each polymorphic class, otherwise you will receive an "unknown descriminator" error message during deserialization:
var commands = col.FindAllAs<ICommand>().ToList();
3 You said:
This is a poor OO design and is forced into my class hierarchy by the BSON library.
In any case, even without KnownTypes assign your code using Bson lib via the BsonId attribute. If you want to avoid this, you can:
BsonClassMap.RegisterClassMap<MoveCommand>(cm => { cm.AutoMap(); cm.SetIdMember(cm.GetMemberMap(c => c.Id)); });
So now you can remove the link to Mongodb.Bson lib from your lib domain code.
source share