Skip to main content

Maestria.TypeProviders by Fábio Monteiro Naspolini

NuGet / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: Maestria.TypeProviders

.NET 5 C# Source TypeProviders.

Author: Fábio Monteiro Naspolini

NuGet: https://www.nuget.org/packages/Maestria.TypeProviders/

You can find more details at https://github.com/MaestriaNet/TypeProviders

Source: https://github.com/MaestriaNet/TypeProviders

Author

note

Fábio Monteiro Naspolini Alt text

Original Readme

note

Maestria.TypeProviders

NuGet Version NuGet Downloads Apimundo


buy-me-a-coffee smile.png

If my contributions helped you, please help me buy a coffee :D

donate


What is Maestria.Type.Providers?

Source Generator pack to increase productivity and improve source code writing.

How install and configure package?

First, install Maestria.Type.Providers from the dotnet cli command line:

dotnet add package Maestria.TypeProviders
dotnet add package ClosedXML
Providers x Dependencies

This package does not include dependencies references when installed on your project, its only generate source code files.
You need install thirds dependencies to compile your project according to the features used, bellow instructions of source generator providers:

  • ExcelProvider: Generated strong data sctruct and factory class to load xls/xlsx data.
  • OpenApiProvider: Generate HTTP client from OpenApi / Swagger specification.
ExcelProvider

Generate strong data struct and class factory to load excel data from xls/xlsx template.

Attribute: ExcelProvider

Dependencies

Dependencies install

dotnet add package ClosedXML

Source code sample

Use case sample

// The relative path is based at the source code file location.
// In this example the first page was used as none were explicitly entered.
[ExcelProvider(TemplatePath = @"../../resources/Excel.xlsx")]
public partial class MyExcelData
{
}

var data = MyExcelDataFactory.Load(filePath);
foreach (var item in data)
// Access strong typing by "item.<field-name>"

Use case sample two

// The relative path is based at the source code file location.
// Loadind data struct from second page
[ExcelProvider(TemplatePath = @"../../resources/Excel.xlsx", SheetName = "Plan2")]
public partial class MyExcelData
{
}

var data = MyExcelDataFactory.Load(filePath, "Plan2");
foreach (var item in data)
// Access strong typing by "item.<field-name>"

Generator engine:

  • Nullable types: To create a nullable property, seed excel template file with one row cell empty, and another not empty.
  • Decimal types: To create decimal property, seed one row of cell with floating point value.

Good practices: Don't use big file by template, this file is used always you need recreated source code. Big file impact is slow build time.


OpenApiProvider

Provider to generate source code HTTP client from OpenApi / Swagger specification.

It's planned used NSwagStudio engine with .NET 5 source generator.
This package allows automatized generation code.

...As soon as possible


Troubleshooting

Optional configuration in VS Code: To view the automatically generated codes it is necessary to indicate to write it to disk with the configuration in the .csproj file.
On CompilerGeneratedFilesOutputPath property its configured with /../generated/$(MSBuildProjectName). This folder is one level above of file project on this sample.
This mode allow see generated files, but not works go to navigation feature of VS Code.

<!-- Enable source disk file write to correct IDE's works -->
<PropertyGroup>
<CompilerGeneratedFilesOutputPath>$(MSBuildProjectDirectory)/../generated/$(MSBuildProjectName)</CompilerGeneratedFilesOutputPath>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

Optional configuration in VS Code: To allow go to navigation feature you need write files at solution level.

Problem's: The source code generated will be used on next build, to solve problems of duplicated classes, it's need removed generated files before build.
On next build, if there was no change on yout source code used by generators, the files has no generated. You need force a rebuild with dotnet build --no-incremental <args> to regenerate files.

<!-- Enable source disk file write to correct IDE's works -->
<PropertyGroup>
<CompilerGeneratedFilesOutputPath>$(MSBuildProjectDirectory)/generated/$(MSBuildProjectName)</CompilerGeneratedFilesOutputPath>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<!-- Remove files on build start to solve recreate bug message "alwaready exists" -->
<Target Name="ExcludeGenerated" BeforeTargets="AssignTargetPaths">
<ItemGroup>
<Generated Include="/generated/**/*.cs" />
<Compile Remove="@(Generated)" />
</ItemGroup>
<Delete Files="@(Generated)" />
</Target>

4 devs

Restart build server to validate clean source generator build.

dotnet build-server shutdown


Sample of .csproj file


buy-me-a-coffee smile.png

If my contributions helped you, please help me buy a coffee :D

donate

About

note

Generating strong typed code from Excel.

How to use

Example (source csproj, source files)

This is the CSharp Project that references Maestria.TypeProviders

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ClosedXML" Version="0.105.0" />
<PackageReference Include="Maestria.Extensions" Version="3.7.2.0" />
<PackageReference Include="Maestria.TypeProviders" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<None Update="MyExcel.xlsx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

//----------------------
// <auto-generated>
// Generated using Maestria.TypeProviders (https://github.com/MaestriaNet/TypeProviders)
// </auto-generated>
//----------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using ClosedXML.Excel;
using Maestria.TypeProviders.Excel;

namespace DemoExcel
{
public partial class MyExcelPerson
{
public int ID \{ get; set; }
public string FirstName \{ get; set; }
public string LastName \{ get; set; }
}

public static partial class MyExcelPersonFactory
{
public static IEnumerable<MyExcelPerson> Load(Stream input, int sheetPosition = 1)
{
using var workbook = new XLWorkbook(input);
return Load(workbook, sheetPosition, null);
}

public static IEnumerable<MyExcelPerson> Load(Stream input, string sheetName)
{
using var workbook = new XLWorkbook(input);
return Load(workbook, 0, sheetName);
}

public static IEnumerable<MyExcelPerson> Load(string filePath, int sheetPosition = 1)
{
using var workbook = ExcelExtensions.OpenWorkbook(filePath);
return Load(workbook, sheetPosition, null);
}

public static IEnumerable<MyExcelPerson> Load(string filePath, string sheetName)
{
using var workbook = ExcelExtensions.OpenWorkbook(filePath);
return Load(workbook, 0, sheetName);
}

public static IEnumerable<MyExcelPerson> Load(XLWorkbook workbook, int sheetPosition = 1, string sheetName = "")
{
var result = new List<MyExcelPerson>();
var sheet = string.IsNullOrEmpty(sheetName) ? workbook.Worksheet(sheetPosition) : workbook.Worksheet(sheetName);
var indexOfID = sheet.ColumnByName(@"ID");
var indexOfFirstName = sheet.ColumnByName(@"FirstName");
var indexOfLastName = sheet.ColumnByName(@"LastName");
foreach (var row in sheet.Rows(2, sheet.RowUsedCount()))
{
var iDValue = row.Cell(indexOfID);
var firstNameValue = row.Cell(indexOfFirstName);
var lastNameValue = row.Cell(indexOfLastName);
result.Add(new MyExcelPerson
{
ID = iDValue.GetValue<int>(),
FirstName = firstNameValue.GetValue<string>(),
LastName = lastNameValue.GetValue<string>(),
});
}
return result;
}
}
}

Useful

Download Example (.NET C#)

Share Maestria.TypeProviders

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Maestria.TypeProviders

Category "FilesToCode" has the following generators:

1 Chorn.EmbeddedResourceAccessGenerator Nuget GitHub Repo stars 2024-01-21

2 corecraft Nuget GitHub Repo stars 2024-02-17

3 Datacute.EmbeddedResourcePropertyGenerator Nuget GitHub Repo stars 2024-11-03

4 DotnetYang Nuget GitHub Repo stars 2024-06-29

5 EmbedResourceCSharp Nuget GitHub Repo stars 2023-04-16

6 kli.Localize Nuget GitHub Repo stars 2025-10-01

7 LingoGen Nuget GitHub Repo stars 2024-02-23

8 Maestria.TypeProviders Nuget GitHub Repo stars 2026-04-09

9 NFH.FileEmbed Nuget GitHub Repo stars 2025-08-08

10 NotNotAppSettings Nuget GitHub Repo stars 2024-01-26

11 Podimo.ConstEmbed Nuget GitHub Repo stars 2023-04-16

12 ResXGenerator Nuget GitHub Repo stars 2023-10-02

13 RSCG_JSON2Class Nuget GitHub Repo stars 2024-02-29

14 RSCG_Utils Nuget GitHub Repo stars 2023-04-16

15 Strings.ResourceGenerator Nuget GitHub Repo stars 2025-07-06

16 SvgIconGenerator Nuget GitHub Repo stars 2026-04-04

17 ThisAssembly_Resources Nuget GitHub Repo stars 2023-09-16

18 ThisAssembly.Strings Nuget GitHub Repo stars 2024-07-21

19 TypedPaths Nuget GitHub Repo stars 2026-04-01

20 Weave Nuget GitHub Repo stars 2024-01-27

See category

FilesToCode