3

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?

2
  • ? you dont have to register any. Mongo c# driver will take care of any class and automatically serialize and deserialze all public field and properties of a class.register a class map is for times which you want to map some properties not all of them Commented Jan 9, 2015 at 10:55
  • 1
    ok in my case I need to register all classes which implement an interface (The MongoDb-Driver has problems to handle interfaces). this classes can increase in future. And to make sure I never forget one I need to register them automatically. In my example I could verify that the class really implements the needed interface Commented Jan 9, 2015 at 11:00

2 Answers 2

11

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I just want to say thanks for posting this answer, it helped me. :-)
Old post bus saved my day :) Thx
0

You may use this way.

IEnumerable<Type> domainEventTypes = typeof(IDomainEvent)
                                   .Assembly
                                   .GetTypes()
                                   .Where(t => t.IsClass && !t.IsAbstract && typeof(IDomainEvent).IsAssignableFrom(t));

    foreach (Type eventType in domainEventTypes)
    {
        if (BsonClassMap.IsClassMapRegistered(eventType)) continue;
        BsonClassMap cm = new(eventType);
        cm.AutoMap();
        cm.SetIgnoreExtraElements(true);
        cm.SetDiscriminatorIsRequired(true);
        cm.SetDiscriminator(eventType.Name);
        BsonClassMap.RegisterClassMap(cm);
    }

In this way you don't have to use generic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.