From 45243aa3ca3f0566e9a8e15b1556c2668d69a39a Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Tue, 6 Jan 2026 23:23:51 -0500 Subject: [PATCH] feat(ExcelIO): add ExcelClassMap base class with fluent Map method --- .../Mapping/ExcelClassMap.cs | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 NEW/src/JdeScoping.ExcelIO/Mapping/ExcelClassMap.cs diff --git a/NEW/src/JdeScoping.ExcelIO/Mapping/ExcelClassMap.cs b/NEW/src/JdeScoping.ExcelIO/Mapping/ExcelClassMap.cs new file mode 100644 index 0000000..7f607ef --- /dev/null +++ b/NEW/src/JdeScoping.ExcelIO/Mapping/ExcelClassMap.cs @@ -0,0 +1,75 @@ +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); + } +}