Automapping with ignored Dictionary<string,object>

Cameron's Avatar

Cameron

26 Apr, 2010 02:58 PM via web

Hi,
I'm getting an exception trying to generate the mappings for my domain model with the automapper.

I have a DocumentVersion class with the following property in it that I don't want to map/persist:

[DisablePersistence]
public virtual Dictionary<string, object> Metadata  {  get; set; }

When I instantiate my AutoPersistenceModel, I tried to ignore it with two methods:

var mappings = new AutoPersistenceModel();
mappings.OverrideAll(this.Ignores);
mappings.Override<DocumentVersion>(v => v.IgnoreProperty(p => p.Metadata));
...
private void Ignores(IPropertyIgnorer x)
{
    x.IgnoreProperties(p => p.MemberInfo.GetCustomAttributes(typeof(DisablePersistenceAttribute), false).Length > 0);
}

Yet, when I run it, I get the following exception:

The number of generic arguments provided doesn't equal the arity of the generic type definition.\r\nParameter name: instantiation
at System.RuntimeType.MakeGenericType(Type[] instantiation)
at FluentNHibernate.Automapping.AutoMapManyToMany.GetInverseProperty(Member property) in C:\Subversion\Personal\cph\Dependencies\app\FluentNHibernate\Automapping\AutoMapManyToMany.cs:line 33
at FluentNHibernate.Automapping.AutoMapManyToMany.MapsProperty(Member property) in C:\Subversion\Personal\cph\Dependencies\app\FluentNHibernate\Automapping\AutoMapManyToMany.cs:line 26
at FluentNHibernate.Automapping.AutoMapper.TryToMapProperty(ClassMappingBase mapping, Member property, IList`1 mappedProperties) in C:\Subversion\Personal\cph\Dependencies\app\FluentNHibernate\Automapping\AutoMapper.cs:line 130
at FluentNHibernate.Automapping.AutoMapper.MapEverythingInClass(ClassMappingBase mapping, Type entityType, IList`1 mappedProperties) in C:\Subversion\Personal\cph\Dependencies\app\FluentNHibernate\Automapping\AutoMapper.cs:line 120
at FluentNHibernate.Automapping.AutoMapper.MergeMap(Type classType, ClassMappingBase mapping, IList`1 mappedProperties) in C:\Subversion\Personal\cph\Dependencies\app\FluentNHibernate\Automapping\AutoMapper.cs:line 54
at FluentNHibernate.Automapping.AutoMapper.Map(Type classType, List`1 types) in C:\Subversion\Personal\cph\Dependencies\app\FluentNHibernate\Automapping\AutoMapper.cs:line 152
at FluentNHibernate.Automapping.AutoPersistenceModel.AddMapping(Type type) in C:\Subversion\Personal\cph\Dependencies\app\FluentNHibernate\Automapping\AutoPersistenceModel.cs:line 155
at FluentNHibernate.Automapping.AutoPersistenceModel.CompileMappings() in C:\Subversion\Personal\cph\Dependencies\app\FluentNHibernate\Automapping\AutoPersistenceModel.cs:line 132
at FluentNHibernate.Automapping.AutoPersistenceModel.Configure(Configuration configuration) in C:\Subversion\Personal\cph\Dependencies\app\FluentNHibernate\Automapping\AutoPersistenceModel.cs:line 142
at FluentNHibernate.Cfg.AutoMappingsContainer.Apply(Configuration cfg) in C:\Subversion\Personal\cph\Dependencies\app\FluentNHibernate\Cfg\AutoMappingsContainer.cs:line 84

The error, as it mentions, is coming from here:

    private static Member GetInverseProperty(Member property)
    {
        Type type = property.PropertyType;
        var inverseSide = type.GetGenericTypeDefinition()
            .MakeGenericType(property.DeclaringType);

        var argument = type.GetGenericArguments()[0];
        return argument.GetProperties()
            .Where(x => x.PropertyType == inverseSide)
            .Select(x => x.ToMember())
            .FirstOrDefault();
    }

... which looks like it'd only work with lists, and not dictionaries.

Either way, I'm not actually trying to map it, but I'm still getting this mapping exception. That appears to be because of the ordering of the conditions in AutoMapper.cs:

    protected void TryToMapProperty(ClassMappingBase mapping, Member property, IList<string> mappedProperties)
    {
        if (!property.HasIndexParameters)
        {
            foreach (var rule in mappingRules)
            {
                if (rule.MapsProperty(property))
                {
                    if (!mappedProperties.Any(name => name == property.Name))
                    {
                        rule.Map(mapping, property);
                        mappedProperties.Add(property.Name);

                        break;
                    }
                }
            }
        }
    }

Putting the if (!mappedProperties.Any(name => name == property.Name)) condition as the outer, rather than as the inner most condition stops it from calling rule.MapsProperty, where it throws the exception. Nonetheless, I'm guessing the exception shouldn't be being thrown anyway, or it should at least be given a more user-friendly exception message.

This is happening with the latest git source. Is this a bug or am I misunderstanding what should be happening?

Thanks!
Cameron

  1. Support Staff 2 Posted by James Gregory on 16 May, 2010 05:32 PM

    James Gregory's Avatar

    Generic dictionaries don't work with the automapper, but until today we were still attempting to map them (and seeing your exception). Please try the latest version, where we explicitly don't try to map dictionaries.

  2. 3 Posted by Cameron on 19 Jul, 2010 11:18 AM

    Cameron's Avatar

    It seems to be working now.

    Thanks, Cameron

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What is five times five?