CsvReaderAdvanced 1.0.7
See the version list below for details.
dotnet add package CsvReaderAdvanced --version 1.0.7
NuGet\Install-Package CsvReaderAdvanced -Version 1.0.7
<PackageReference Include="CsvReaderAdvanced" Version="1.0.7" />
paket add CsvReaderAdvanced --version 1.0.7
#r "nuget: CsvReaderAdvanced, 1.0.7"
// Install CsvReaderAdvanced as a Cake Addin #addin nuget:?package=CsvReaderAdvanced&version=1.0.7 // Install CsvReaderAdvanced as a Cake Tool #tool nuget:?package=CsvReaderAdvanced&version=1.0.7
CsvReaderAdvanced
The faster and most modern CSV reader adapted to DI principles.
How to use
First add the service to the ServiceCollection.
builder.ConfigureServices((context, services) =>
{
services.AddCsvReader(context.Configuration);
...
Csv schemas via appsettings.json
To understand exactly what the method does, it assumes that the current configuration file contains a csvSchemas
section, typically in the appsettings.json
file:
public static IServiceCollection AddCsvReader(this IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<ICsvReader,CsvReader>();
services.AddTransient<ICsvFile,CsvFile>();
//Microsoft.Extensions.Hosting must be referenced
services.Configure<CsvSchemaOptions>(configuration.GetSection(CsvSchemaOptions.CsvSchemasSection));
return services;
}
The schema in the appsettings.json file typically contains a property named csvSchemas
:
"csvSchemas": {
"schemas": [
{
"name": "products",
"fields": [
{
"name": "ProductID",
"alternatives": [ "Product ID" ],
"required": true
},
{
"name": "Weight",
"unit": "t",
"alternativeFields": [ "Volume", "TEU" ],
"required": true
},
{
"name": "Volume",
"unit": "m^3",
"alternativeUnits": [ "m3", "m^3" ]
...
We assume that we get the options via DI like the following example:
public Importer(
IUnitOfWork context,
IMapper mapper,
IServiceProvider provider,
ILogger logger,
IOptions<CsvSchemaOptions> options)
{
_context = context;
_mapper = mapper;
_provider = provider;
_logger = logger;
_options = options.Value;
}
protected readonly IUnitOfWork _context;
protected readonly IMapper _mapper;
protected readonly IServiceProvider _provider;
protected readonly ILogger _logger;
protected readonly CsvSchemaOptions _options;
public CsvSchema? GetSchema(string name) =>
_options?.Schemas?.FirstOrDefault(s => s.Name == name);
public ValidationResult CheckForSchema(string name)
{
if (_options?.Schemas is null || !_options.Schemas.Any())
{
_logger.LogError("Could not retrieve csv schemas from settings");
return new ValidationResult(
new ValidationFailure[] { new ValidationFailure("CsvSchemas", "Cannot retrieve csv schemas from settings") });
}
var schema = GetSchema(name);
if (schema is null)
{
_logger.LogError("Could not retrieve '{schemaName}' schema from settings",name);
return new ValidationResult(
new ValidationFailure[] { new ValidationFailure(name, $"Cannot retrieve '{name}' schema from settings") });
}
return new ValidationResult();
}
Read the file
We instantiate a CsvFile
in order to read the file. Note that the aforementioned CsvSchema
is not needed if we do not have a header and/or do not want to validate the existence of fields.
For the example below, we assume that a CsvSchema
is checked.
//We assume that _provider is an IServiceProvider which is injected via DI
var file = _provider.GetCsvFile();
file.ReadFromFile(path, Encoding.UTF8, withHeader:true);
//the line above is equivalent to the 2 commands:
file.ReadFromFile(path, Encoding.UTF8);
file.PopulateColumns();
The PopulateColumns()
method updates the internal ExistingColumns
dictionary. The ExistingColumns
dictionary is case insensitive and stores the index location for each column. The index location is zero-based.
To check the existence of fields against a schema we should call the CheckAgainstSchema()
method as shown below:
CsvScema schema = _options.Schemas.FirstOrDefault(s => s.Name == "products");
file.CheckAgainstSchema(schema);
The CheckAgainstSchema()
method also calls the PopulateColumns()
method if the ExistingColumns
property is not populated. It then updates the AllFieldColumns
property which is a dictionary of the column index location based on the field name. Non-existend fields will have a column value of -1.
Two additional properties: MissingFields
and ExistingFields
return the names of the missing and non-missing fields correspondingly.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- Microsoft.Extensions.Hosting (>= 7.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on CsvReaderAdvanced:
Package | Downloads |
---|---|
EndpointProviders
A modern way to add Dependency Injection used for Minimal API apps. See README. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
2.3.8 | 82 | 10/16/2024 | |
2.3.7 | 131 | 7/27/2024 | |
2.3.6 | 98 | 7/27/2024 | |
2.3.5 | 78 | 7/26/2024 | |
2.3.4 | 91 | 7/12/2024 | |
2.3.3 | 109 | 6/22/2024 | |
2.3.2 | 97 | 6/22/2024 | |
2.3.1 | 246 | 3/1/2024 | |
2.3.0 | 214 | 12/13/2023 | |
2.2.1 | 149 | 11/28/2023 | |
2.2.0 | 178 | 10/17/2023 | |
2.1.1 | 142 | 10/15/2023 | |
2.1.0 | 129 | 10/15/2023 | |
1.3.3 | 122 | 10/14/2023 | |
1.3.2 | 117 | 10/14/2023 | |
1.3.0 | 136 | 10/13/2023 | |
1.2.6 | 139 | 9/29/2023 | |
1.2.5 | 191 | 7/18/2023 | |
1.2.4 | 189 | 7/16/2023 | |
1.2.2 | 156 | 7/16/2023 | |
1.2.1 | 167 | 7/14/2023 | |
1.2.0 | 162 | 7/14/2023 | |
1.1.15 | 168 | 7/14/2023 | |
1.1.14 | 165 | 7/14/2023 | |
1.1.13 | 178 | 7/7/2023 | |
1.1.12 | 270 | 7/6/2023 | |
1.1.11 | 155 | 7/5/2023 | |
1.1.10 | 177 | 7/5/2023 | |
1.1.9 | 156 | 6/27/2023 | |
1.1.8 | 143 | 6/26/2023 | |
1.1.7 | 145 | 6/24/2023 | |
1.1.6 | 141 | 6/24/2023 | |
1.1.5 | 144 | 6/23/2023 | |
1.1.2 | 151 | 6/23/2023 | |
1.0.28 | 162 | 6/23/2023 | |
1.0.27 | 154 | 6/23/2023 | |
1.0.26 | 137 | 6/19/2023 | |
1.0.25 | 164 | 6/18/2023 | |
1.0.24 | 144 | 6/18/2023 | |
1.0.23 | 163 | 6/18/2023 | |
1.0.22 | 151 | 6/18/2023 | |
1.0.21 | 150 | 6/17/2023 | |
1.0.20 | 150 | 6/17/2023 | |
1.0.19 | 148 | 6/17/2023 | |
1.0.18 | 151 | 6/17/2023 | |
1.0.17 | 161 | 6/17/2023 | |
1.0.16 | 145 | 6/17/2023 | |
1.0.15 | 149 | 6/17/2023 | |
1.0.12 | 143 | 6/17/2023 | |
1.0.11 | 152 | 6/17/2023 | |
1.0.10 | 136 | 6/17/2023 | |
1.0.9 | 145 | 6/17/2023 | |
1.0.8 | 154 | 6/17/2023 | |
1.0.7 | 140 | 6/17/2023 | |
1.0.6 | 153 | 6/16/2023 | |
1.0.5 | 164 | 6/16/2023 | |
1.0.4 | 143 | 6/16/2023 | |
1.0.2 | 134 | 6/16/2023 |