This project has moved. For the latest updates, please go here.

I have found a problem?

Feb 20, 2013 at 11:12 PM
Edited Feb 20, 2013 at 11:28 PM
In source code Util.cs
private static string GetCollectionNameFromType(Type entitytype)
{
        string collectionname;

        // Check to see if the object (inherited from Entity) has a CollectionName attribute
        var att = Attribute.GetCustomAttribute(entitytype, typeof(CollectionName));
        if (att != null)
        {
            // It does! Return the value specified by the CollectionName attribute
            collectionname = ((CollectionName)att).Name;
        }
        else
        {
            // No attribute found, get the basetype

            while (!entitytype.BaseType.Equals(typeof(Entity)))
            {
                entitytype = entitytype.BaseType;
            }

            collectionname = entitytype.Name;
        }

        return collectionname;
}
while statement block may be occur to error that null exception.
so, this GetCollectionNameFromType() method change to below.
private static string GetCollectionNameFromType(Type entitytype)
{
        string collectionname;

        // Check to see if the object (inherited from Entity) has a CollectionName attribute
        var att = Attribute.GetCustomAttribute(entitytype, typeof(CollectionName));
        if (att != null)
        {
            // It does! Return the value specified by the CollectionName attribute
            collectionname = ((CollectionName)att).Name;
        }
        else
        {
            // No attribute found, get the basetype
            if (entitytype.Equals(typeof(Entity)))
            {
                while (entitytype.BaseType != null && !entitytype.BaseType.Equals(typeof(Entity)))
                {
                    entitytype = entitytype.BaseType;
                }
            }
                
            collectionname = entitytype.Name;
        }

        return collectionname;
}
Am I right?
If my suggestion be correct, I wish to update MongoRepository library.
Thank you for your effort. I use this library very useful :)
Coordinator
Feb 22, 2013 at 8:48 AM
In what situation will the problem occur?
Feb 24, 2013 at 11:33 PM
Edited Feb 24, 2013 at 11:37 PM
RobIII wrote:
In what situation will the problem occur?
I'm this library in my project.
My class hierarchy is ...

public abstract BaseItem : IEntity
{}

public abstract BaseA : BaseItem
{}

public SpecialA : BaseA
{}

and I use ...
var specialRepository = new MongoRepository<Special>();
It's cause a problem that NullException();

So, I inherit Entity class instead of IEntity interface.
And then surprisingly no error and well done all.

I thinking in abnormal those problem so, I download source code and debugging.
Finally I find that GetCollectionNameFromType() method can be cause a problem.
Feb 5, 2014 at 3:09 PM
I also have this problem.

I suspect it stems from how we are using inheritance in our domain model.

However, I found that a work around is to use the 'CollectionName' attribute on your subclass, such as:
[CollectionName("BaseItem")]
public class SpecialA : BaseA
Coordinator
Feb 5, 2014 at 3:20 PM
Hmmm, somehow this has escaped my attention. I'll look into it.
Coordinator
Feb 5, 2014 at 3:27 PM
See this commit :-)