When and how often should I be registering class maps in mongo?
At what point should I be registering class maps when using Mongo?
I understand that auto mapping is fine most of the time, but I have a hierarchical class struc开发者_StackOverflow社区ture.
Should I be registering class maps in a static constructor or before each query/insert?
I would like to put a check in before each query/insert but the mongo-csharp API doesn't provide a ClassMapExists() method.
Class maps must be registered exactly once, as your application initializes. It doesn't matter where you do this (static constructor, etc...) as long as it is only done once and is done before any serialization of those classes is attempted. If it's a console application, do your initialization in Main. If it's a web app, do your initialization in the application Load event.
In v1.1 the driver will have a new method called IsClassMapRegistered, but normally you can find a place to do your initialization that doesn't require calling IsClassMapRegistered. If you want to try using this new method you can get the latest development version of the driver from github.
精彩评论