-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
Background and motivation
LoadDataRow into DataTable, or more precisely, into NewRecordFromArray the check is performed to ensure that the length of the array containing the row values does not exceed the number of columns:
if (colCount < value.Length)
{
throw ExceptionBuilder.ValueArrayLength();
}
Because of this, we cannot use built-in solutions like ArrayPool.Shared to pass row values, since the pool does not guarantee the exact length of the returned array.
In this case, we are forced to either perform explicit allocations without using a pool, or implement a custom pool that returns arrays with exact lengths. However, in scenarios where the number of columns and consequently, the required array length varies dynamically, the number of such pools becomes unbounded.
API Proposal
public DataRow LoadDataRow(ReadOnlySpan<object> values, bool fAcceptChanges)This will allow to use arrays of any origin. The methods in the chain must also accept ReadOnlySpan<T>.
API Usage
DataTable dt = new
DataTable
();
dt.Columns.Add(nameof(TVP.Property1), typeof(int));
dt.Columns.Add(nameof(TVP.Property2), typeof(int));
dt.Columns.Add(nameof(TVP.Property3), typeof(int));
ReadOnlySpan<object> values = ArrayPool<object>.Shared.Rent(10).AsSpan().Slice(0, 3);
dt.LoadDataRow(values, false);Alternative Designs
Or simply skip validating the length of the input array (as described above) and instead base logic on the number of columns.
Risks
No response