vendredi 9 janvier 2015

MongoDB dynamically register class-types


Vote count:

0




in MongoDB I need to register class-maps dynamically that I don't need to register all the current and future-classes myself.


There is a Method called



BsonClassMap.RegisterClassMap<T>


but with this I need to know the Class (T) and can't use a Type instead.


I want to iterate through the assembly-types like this to register the class-maps:



var assemblyTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(t => t.GetTypes()).Where(t => t.IsClass);
foreach (var type in assemblyTypes)
{
//Register classmap for type
}


Is there a way to do that?



asked 35 secs ago


1 Answer



Vote count:

0




I finally found a solution for this problem. Just for everyone else who has the same problem.


I checked the mongodb-c#-driver-source to find out if there is another method to register the types dynamically.


The method



BsonClassMap.LookupClassMap(type);


will check if the provided type is registered already. If not it will register it and calls AutoMap(), too.


And to make sure that the class is not registered somewhere else you should use it like this:



if (BsonClassMap.IsClassMapRegistered(type))
return;

//will check if the type is registered. if not it will be automatically registered.
//AutoMap will also called automatically.
BsonClassMap.LookupClassMap(type);


answered 35 secs ago






MongoDB dynamically register class-types

Aucun commentaire:

Enregistrer un commentaire