Search code examples
c#nhibernatefluentmappings

Fluent NHibernate reflection mappings


I'd like to parse through an Fluent NHibernate mapping file so I can get the table name and column names that are specified in there.

Loading the assembly and reflecting the type as ClassMap isn't a problem but that class only get methods to set the table and column names - nothing to get the names back again.

Any ideas?


Solution

  • Using Reflector, it looks like Table method has this signature:

    public void Table(string tableName)
    {
        this.attributes.Set<string>(x => x.TableName, tableName);
    }
    

    Columns will be a lot harder as it keeps properties, references, and collections separate. Eg.

    protected virtual PropertyPart Map(PropertyInfo property, string columnName)
    {
        PropertyPart propertyMap = new PropertyPart(property, typeof(T));
        if (!string.IsNullOrEmpty(columnName))
        {
            propertyMap.Column(columnName);
        }
        this.properties.Add(propertyMap);
        return propertyMap;
    }
    

    In theory though you could get the private fields attributes, properties, and references via reflection and get the information that way.