using System.Linq.Expressions;
namespace JdeScoping.ExcelIO.Mapping;
///
/// Interface for Excel class maps (non-generic access).
///
public interface IExcelClassMap
{
/// The type this map applies to.
Type MappedType { get; }
/// Excel table name (for named ranges).
string? TableName { get; }
/// Worksheet tab name.
string? TabName { get; }
/// Ordered column definitions.
IReadOnlyList Columns { get; }
}
///
/// Base class for defining Excel column mappings via fluent API.
///
/// The model type to map.
public abstract class ExcelClassMap : IExcelClassMap
{
private readonly List _columns = [];
///
public Type MappedType => typeof(T);
///
public string? TableName { get; private set; }
///
public string? TabName { get; private set; }
///
public IReadOnlyList Columns =>
_columns.OrderBy(c => c.Order).ThenBy(c => c.PropertyName).ToList();
///
/// Configures the table and tab names for this model.
///
protected void Table(string tableName, string tabName)
{
TableName = tableName;
TabName = tabName;
}
///
/// Maps a property to an Excel column.
///
protected ColumnBuilder Map(Expression> property)
{
var memberExpr = property.Body as MemberExpression
?? throw new ArgumentException("Expression must be a property access", nameof(property));
var propertyName = memberExpr.Member.Name;
var compiled = property.Compile();
var definition = new ColumnDefinition
{
PropertyName = propertyName,
PropertyType = typeof(TProperty),
ValueGetter = obj => compiled((T)obj),
HeaderText = propertyName // Default to property name
};
_columns.Add(definition);
return new ColumnBuilder(definition);
}
}