I think ITypeConvention.CanHandle should take the IProperty interface
instead of just a type.
I have a ITypeConvention implementation
public class TypePrependsFieldName<T> : ITypeConvention
{
private readonly string _fieldName;
public TypePrependsFieldName(string fieldName)
{
_fieldName = fieldName;
}
public bool CanHandle(Type type)
{
return type == typeof (T);
}
public void AlterMap(IProperty propertyMapping)
{
if (
propertyMapping.Property.Name != _fieldName) return;
propertyMapping.TheColumnNameIs
(
propertyMapping.ParentType.Name +
propertyMapping.Property.Name);
}
}
So that I can add the following conventions
convention.AddTypeConvention(new TypePrependsFieldName<string>
("Name"));
convention.AddTypeConvention(new TypePrependsFieldName<string>
("Description"));
However since
Conventions.cs
public ITypeConvention FindConvention(Type propertyType)
{
var find = _typeConventions.Find(c => c.CanHandle
(propertyType));
return find ?? new DefaultConvention();
}
uses the Type, it only finds the first TypePrependsFieldName class
because it thinks it can handle the property, but it can't because it
doesn't know until AlterMap gets called to check the name of the
property.
Or at the very least to change it from Type to be PropertyInfo since
you can always go up to the type anyway since that's exactly what the
code does that calls FindConvention does anyway
AutoMapColumn.cs
private bool HasExplicitTypeConvention(PropertyInfo property)
{
var convention = conventions.FindConvention
(property.PropertyType);
....
}
I know this would be a breaking change but it's very hard to figure
out if the Convention can handle the type unless you end up creating a
very complex AlterMap method and have to merge a bunch of classes
together as opposed to being able to add ones like I did using a
little bit of generics.