1

I'm trying to write the following code using 10gen C# drivers for Mongo Db. What I'm trying to achieve here is that write a simple adapter so as to seamlessly cache C# objects.

The RegisterClassMap() is not able to recognize CacheId field from the class contract. I couldn't find enough documentation on MapIdField api.

Can somebody please help ? Thanks in advance !!

public interface ICacheable
{
    byte[] CacheId { get; }
}

public class Contract : ICacheable
{
    public byte[] CacheId { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }
}

public class MongoCacheStore
{
    private void RegisterClass<T>() where T : class, ICacheable
    {
        if (!BsonClassMap.IsClassMapRegistered(typeof(T)))
        {
            BsonClassMap.RegisterClassMap<T>();

            BsonClassMap.RegisterClassMap<T>(cm =>
            {
                cm.AutoMap();
                **cm.MapIdField("CacheId");**
            });
        }
    }
}
1
  • I also changed cm.MapIdField("CacheId"); to cm.MapIdField("Id"); and declared Id as long datatype. But still it couldn't recognize Id. So it is not related about not been able to identify byte array id field Commented Feb 14, 2012 at 18:51

1 Answer 1

4

The following worked for me:

public class MongoCacheStore
{
    public void RegisterClass<T>() where T : class, ICacheable
    {
        BsonClassMap.RegisterClassMap<T>(cm =>
        {
            cm.AutoMap();
            cm.MapIdProperty("CacheId");
        });
    }
}

You have to use MapIdProperty instead of MapIdField because CacheId is a property.

Did you mean to not have a set accessor in the interface?

Also, it is much better if you arrange to have RegisterClass only called once rather then testing for IsClassMapRegistered every time. For one, I don't think it is thread safe as written (two threads could both get false back from calling IsClassMapRegistered and one of them is going to fail when calling RegisterClassMap).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for replying Robert. Yes I meant to not have set accessor in the interface. Is it possible in anyway that I use CacheId as the _id field for that document but not serialize it. Something like this code public class MongoCacheStore { public void RegisterClass<T>() where T : class, ICacheable { BsonClassMap.RegisterClassMap<T>(cm => { cm.AutoMap(); cm.MapIdProperty("CacheId"); ** cm.SkipFromSerialization("CacheId"); }); } }
Pardon me for the lack of indentation the above comment
Even if you were to configure your class map so that the _id is not serialized the server will add one anyway when you insert the document. The _id field is mandatory.

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.