Skip to main content
Version: Next

Data Import

Files Referenced in this Document

  • IMyEntityNameAppService.cs
  • MyEntityNameAppService.cs
  • MyEntityNameExcelDto.cs

Table of Contents​

  1. Introduction
  2. Import Process
  3. Template Generation
  4. Data Validation and Parsing
  5. Error Handling

Introduction​

Data import functionality allows users to bulk create entities by uploading an Excel file. This is crucial for initial data seeding or migrating data from legacy systems. The MyEntityNameAppService implements IDataImportAppService to provide standard import capabilities.

Import Process​

Method Signature: Task ImportAsync(ImportExcelInput input)

Process:

  1. Upload: User uploads an Excel file containing the data.
  2. Read: The system reads the file stream using an Excel library (e.g., MiniExcel, EPPlus).
  3. Map: Rows in the Excel sheet are mapped to MyEntityNameExcelDto objects.
  4. Validate: Each DTO is validated (e.g., check for duplicates, required formats).
  5. Persist: Valid DTOs are converted to Entities and saved to the database.

Template Generation​

Method Signature: Task<FileContentResult> GetImportTemplateAsync()

Description: Generates a blank Excel template with correct headers, helping users format their data correctly before upload. This reduces import errors caused by mismatched columns.

Data Validation and Parsing​

The import logic should handle:

  • Type Conversion: Ensuring string cells are correctly parsed into numbers, dates, or enums.
  • Business Rules: Checking if codes are unique or referenced data exists.
  • Empty Rows: Ignoring empty or invalid rows.

Error Handling​

If errors occur during import (e.g., invalid data format), the system typically:

  • Stops and reports the error to the user.
  • Or, skips invalid rows and imports valid ones, returning a report of skipped rows. (The specific behavior depends on the implementation details in ExcelManager).