Check for existing mapping when writing a custom applier in ConfORM

99 Views Asked by At

I am writing my first custom column name applier for ConfORM.

How do I check if another column has already been map with same mapping name?

This is what I have so far:

public class MyColumnNameApplier : IPatternApplier<PropertyPath, IPropertyMapper>
{
    public bool Match(PropertyPath subject)
        {
            return (subject.LocalMember != null);
        }

        public void Apply(PropertyPath subject, IPropertyMapper applyTo)
        {
            string shortColumnName = ToOracleName(subject);
            // How do I check if the short columnName already exists?
            applyTo.Column(cm => cm.Name(shortColumnName));
        }

        private string ToOracleName(PropertyPath subject)
        {
            ...
        }
    }
}

I need to shorten the generated column names to less than 30 characters to fit in with Oracle's 30 character limit. Because I am shortening the column names it is possible that the same column name can potentially be generated two different properties. I would like to know when a duplicate mapping occurs.

If I don't handle this scenario ConfORM/NHibernate allows two different properties to 'share' the same column name - this is obviously creates a problem for me.

1

There are 1 best solutions below

0
On

if column names are mapped twice you will get exception about parameter count on first load. You can can check after configuring:

foreach (var clazz in config.ClassMappings)
{
    var propertiesWithOneColumn = clazz.PropertyClosureIterator.Where(p => p.ColumnSpan == 1);

    var distinctColumns = new HashSet<string>();
    foreach (var prop in propertiesWithOneColumn)
    {
        var col = prop.ColumnIterator.First().Text;
        if (distinctColumns.Add(col))
        {
            Console.WriteLine("duplicate column "+ col + " found for property " + prop.PersistentClass.ClassName + "." + prop.Name);
        }
    }
}