Skip to Content
TUtilityImport preview models

Import preview models

On this page

TUtility.Core provides ImportPreviewOutput<T>, ImportPreviewListOutput<T> and ImportPreviewResult in the TUtility namespace. They carry validation results; they do not read Excel, validate business rules or persist data.

Choose the candidate collection

TypeDataUse when
ImportPreviewOutput<T>Dictionary<string, T>Each record has an explicit string identity used for lookup
ImportPreviewListOutput<T>List<T>Input order matters or the batch has no dictionary key

Both inherit ImportPreviewOutputBase; T must be a reference type. The application supplies comparison and duplicate handling.

Complete example

Use this Program.cs in a .NET 10 console project with TUtility.Core installed:

using TUtility; var source = new[] { (Row: 2, Code: "0001", Name: "Pump A"), (Row: 3, Code: "0001", Name: "Pump B"), (Row: 4, Code: "0003", Name: "Pump C") }; var preview = new ImportPreviewOutput<DeviceDraft> { Data = new Dictionary<string, DeviceDraft>(StringComparer.Ordinal), DataCount = source.Length }; foreach (var row in source) { var added = preview.Data.TryAdd(row.Code, new DeviceDraft { Code = row.Code, Name = row.Name }); preview.Results.Add(new ImportPreviewResult(row.Row, added, added ? "" : "Duplicate device code.")); preview.HasError |= !added; } Console.WriteLine($"DataCount={preview.DataCount}; Candidates={preview.Data.Count}; Results={preview.Results.Count}; HasError={preview.HasError}"); foreach (var result in preview.Results) Console.WriteLine($"Row {result.Row}: {result.Success} {result.ErrorMessage}".TrimEnd()); /// <summary>用于预检展示的候选数据,不携带数据库或权限状态。</summary> public sealed class DeviceDraft { /// <summary>保留字符串形式的稳定业务身份。</summary> public string Code { get; set; } = ""; /// <summary>设备的可读名称,不用作字典身份。</summary> public string Name { get; set; } = ""; }

Expected output:

DataCount=3; Candidates=2; Results=3; HasError=True Row 2: True Row 3: False Duplicate device code. Row 4: True

Fields are independent

FieldApplication responsibility
DataCountNumber of input records; duplicates count as inputs in this example
HasErrorWhether any validation failed; explicitly maintained by validation code
ResultsSynchronizedList<ImportPreviewResult> populated by the application
ImportPreviewResult.RowSource location; an Excel import can use the actual worksheet row number
Success / ErrorMessageStatus and error text for this validation result
DataRetained candidates, not persisted records

Adding a failed result does not set HasError. Adding candidates does not update DataCount. ImportPreviewResult.Success defaults to false; assign it explicitly for successful results. Decide whether results are per row or per validation rule.

Bound returned details while retaining complete error state

This separate Program.cs demonstrates summary accounting and a detail limit without retaining candidates. It scans 10,003 rows, retains only the first two results, and encounters an error in the final row. ImportPreviewOutputBase is sufficient for a summary without candidate data.

using TUtility; var summary = new ImportPreviewOutputBase(); var detailLimit = 2; var errorCount = 0; for (var row = 2; row <= 10_004; row++) { var success = row != 10_004; summary.DataCount++; summary.HasError |= !success; if (!success) errorCount++; if (summary.Results.Count < detailLimit) summary.Results.Add(new ImportPreviewResult(row, success, success ? "" : "Invalid final row.")); } Console.WriteLine($"Rows={summary.DataCount}; Errors={errorCount}; Results={summary.Results.Count}; HasError={summary.HasError}"); Console.WriteLine($"Visible errors: {summary.Results.Count(result => !result.Success)}");

Expected output:

Rows=10003; Errors=1; Results=2; HasError=True Visible errors: 0

Truncated Results do not represent the complete error state; use the full-scan HasError for commit eligibility. The model has no total-error field. This example maintains failed-row errorCount separately; distinguish failed rows from diagnostic counts when a row can produce multiple errors.

BudgetBoundsBehavior at the limit
Returned-detail limitResponse size and retained resultsContinue validating input and updating global counts; report truncation
Total rows / cells / durationComplete scan costFail and stop the preview; unscanned input has not passed validation
Candidate-storage limitData, duplicate indexes and other candidate stateReject overflow or use separately budgeted staging

A detail limit does not bound duplicate indexes, candidates or Excel shared strings. Add full counts and truncation flags to the summary DTO. Retain candidates or diagnostics on the server when paging is required.

Concurrency and commit boundaries

Results is synchronized, but Data is a regular dictionary/list, and HasError and DataCount are not atomic aggregators. The entire object is not a concurrent accumulator. Prefer sequential row validation or merge results after parallel work completes.

Commit server-owned candidates after rechecking file identity, authorization and database constraints. Client-supplied candidates and error flags are not a basis for validation.

See TExcel import validation for an XLSX example. Source: TUtility/src/TUtility.Core/Common/ImportPreviewOutput.cs.