feat(ExcelIO): add ExcelClassMap base class with fluent Map method
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace JdeScoping.ExcelIO.Mapping;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for Excel class maps (non-generic access).
|
||||
/// </summary>
|
||||
public interface IExcelClassMap
|
||||
{
|
||||
/// <summary>The type this map applies to.</summary>
|
||||
Type MappedType { get; }
|
||||
|
||||
/// <summary>Excel table name (for named ranges).</summary>
|
||||
string? TableName { get; }
|
||||
|
||||
/// <summary>Worksheet tab name.</summary>
|
||||
string? TabName { get; }
|
||||
|
||||
/// <summary>Ordered column definitions.</summary>
|
||||
IReadOnlyList<ColumnDefinition> Columns { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base class for defining Excel column mappings via fluent API.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The model type to map.</typeparam>
|
||||
public abstract class ExcelClassMap<T> : IExcelClassMap
|
||||
{
|
||||
private readonly List<ColumnDefinition> _columns = [];
|
||||
|
||||
/// <inheritdoc />
|
||||
public Type MappedType => typeof(T);
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? TableName { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? TabName { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<ColumnDefinition> Columns =>
|
||||
_columns.OrderBy(c => c.Order).ThenBy(c => c.PropertyName).ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Configures the table and tab names for this model.
|
||||
/// </summary>
|
||||
protected void Table(string tableName, string tabName)
|
||||
{
|
||||
TableName = tableName;
|
||||
TabName = tabName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a property to an Excel column.
|
||||
/// </summary>
|
||||
protected ColumnBuilder<T, TProperty> Map<TProperty>(Expression<Func<T, TProperty>> 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<T, TProperty>(definition);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user