Vidyano.SourceGenerators 2.21.0

Prefix Reserved
dotnet add package Vidyano.SourceGenerators --version 2.21.0                
NuGet\Install-Package Vidyano.SourceGenerators -Version 2.21.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Vidyano.SourceGenerators" Version="2.21.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Vidyano.SourceGenerators --version 2.21.0                
#r "nuget: Vidyano.SourceGenerators, 2.21.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Vidyano.SourceGenerators as a Cake Addin
#addin nuget:?package=Vidyano.SourceGenerators&version=2.21.0

// Install Vidyano.SourceGenerators as a Cake Tool
#tool nuget:?package=Vidyano.SourceGenerators&version=2.21.0                

Disclaimer

  • Use of SourceGenerators depend on available content.
  • Analyzers and CodeFixes are available in any project.

Some Patterns are not supported by Analyzers at the moment:

  • When Clause on Switch Expression / Statements

Generators

  • Actions Generator
  • BusinessRules Generator
  • Context Generator
  • CustomAction Generator
  • Index Generator
  • Model Generator
  • ProjectTargetType Generator

Generated Constant classes

All generated Constant classes are available under {RootNamespace}.Service.Generated namespace.

  • BusinessRuleNames
  • PersistentObjectTypes
  • PersistentObjectAttributeNames
  • ProgramUnitNames
  • ProgramUnitItemsNames
  • QueryNames
  • QuerySources
  • ActionNames
  • Languages
  • MessageKeys and Messages
  • AppRoles
  • WebsiteNames
  • Obsolete.PersistentObjectTypes

Dependency Injection

By using the InitializeByCtor attribute on a field, the source generator will inject this field via the generated constructor.

The Attribute can only be applied when no constructor is provided.

public partial class CompanyActions
{
    [InitializeByCtor]
    private readonly IMyService myService;
}

Attribute is available via reference Vidyano.Abstractions.

Index Generator

Generate an overview index by adding the GenerateIndex attribute to an entity.

This will result in the following being generated:

  • A partial {Entity} class with a QueryType attribute.
  • A partial {VEntity} QueryType class.
  • A partial {Entities}_Overview Index class.
  • A V{Entities} property on the Context if it does not already exist.

Creating an Index for an Entity

Adding this GenerateIndexattribute creates an overview index.

  • To change the name of the Index, pass the desired type as an argument. Note: Include a namespace if you want the index to be generated under a different namespace.
  • To change the name of the QueryType, use the QueryType attribute.

Important Notes:

  • Index will not be generated if QueryType or Index is already added manually, except if they are partial.
  • If the QueryType contains audit fields, the appropriate interface will be applied. This can differ from the Entity if you add IgnoreForIndex to an audit field.
  • Only the IId interface will be copied to the QueryType.

Additional Attributes

You can add several attributes to the entity's properties to control the index:

  • Search: Adding this attribute creates a second {Property}_Sort property to allow full search on this property.

    Note: This is only needed when values can contain spaces.

  • IgnoreForIndex: This property will not be included in the index.

  • IndexReference: You can include a property from the Reference in the index by adding one attribute per property and defining the path on the reference. This can be more than one level deep.

    Note:

    • If the Reference is Nullable, then every generated IndexReference property is also Nullable.
    • Path cannot contains a Collection property.
[GenerateIndex]
public partial class Customer
{
    public string Id {get; set;}

    [Search]
    public string Name {get; set;}

    [IgnoreForIndex]
    public string Private {get; set;}

    [IndexReference(nameof(Person.Name), Search = true)]
    [IndexReference("Address.CityName")]
    [Reference(typeof(Person)]
    public string Person {get; set;}
}

Projects

Main project

Additional Files & Global Usings

Add following ItemGroup to the .csproj project file.


<ItemGroup>
  
  <AdditionalFiles Include="App_Data\**\*.json" />
  
  <Using Include="$(MSBuildProjectName).Service.Generated" />
  <Using Alias="Types" Include="$(MSBuildProjectName).Service.Generated.PersistentObjectTypes.$(MSBuildProjectName)" />
  <Using Alias="AttributeNames" Include="$(MSBuildProjectName).Service.Generated.PersistentObjectAttributeNames.$(MSBuildProjectName)" />
  <Using Alias="QueryNames" Include="$(MSBuildProjectName).Service.Generated.QueryNames.$(MSBuildProjectName)" />
  <Using Alias="QuerySources" Include="$(MSBuildProjectName).Service.Generated.QuerySources.$(MSBuildProjectName)" />
</ItemGroup>   

External Context

If the context file does not exist in the main project, you can use the appsettings.json file as an alternative, as the context will be read from there.


<ItemGroup>
  
  <AdditionalFiles Include="appsettings.json" />    
</ItemGroup>

Library project

Missing App_Data json files warning

When incorporating SourceGenerators into a library project, you may encounter a VIDYANO0010 warning indicating missing App_Data JSON files. This warning is not applicable to library projects, as they do not utilize App_Data.

To bypass this warning, two methods are available.

  • By disabling the Model SourceGenerator via local .editorconfig file
is_global = true

# Source Generators
disable_model_source_generator = true
  • By ignore the warning via csproj file
<NoWarn>VIDYANO0010</NoWarn>

Exceptions

When using EmitCompilerGeneratedFiles in .csproj project file you can get exceptions when compiling in Windows because of use of long filenames.

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>

To fix this issue you need to add the following registration key (Reboot needed to take effect)

reg ADD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem /v LongPathsEnabled /t REG_DWORD /d 1

Backwards Compatibility

For backwards compatibility we created an Obsolete PersitentObjectTypes class (old version). The only thing you need todo when upgrading to the new source generator is changing the using in de .csproj project file by adding the Generated.Obsolete namespace.

<Using Alias="Types" Include="Fleet.Service.Generated.Obsolete.PersistentObjectTypes.Fleet" />

This way the project wil run as before.

Note: If you add the new usings directly you can use de CodeFix to update to the correct code.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.21.0 0 3/5/2025
2.19.1 956 1/23/2025
2.19.0 452 1/16/2025
2.18.0 141 1/16/2025
2.16.0 1,604 12/4/2024
2.15.0 899 11/19/2024
2.14.0 1,554 10/18/2024
2.13.0 930 10/2/2024
2.12.0 412 9/16/2024
2.11.0 307 8/30/2024
2.10.0 1,601 7/7/2024
2.9.0 869 6/27/2024
2.8.0 1,584 5/26/2024
2.7.0 841 5/7/2024
2.6.0 725 4/19/2024
2.5.1 975 3/29/2024
2.4.0 1,032 2/29/2024
2.3.0 311 2/22/2024
2.2.3 2,814 10/23/2023
2.1.6 1,668 9/19/2023
1.8.1 1,554 6/29/2023
1.8.0 211 6/28/2023
1.7.0 264 6/25/2023
1.6.2 689 6/12/2023
1.6.1 752 5/17/2023
1.6.0 162 5/17/2023
1.5.3 593 5/2/2023
1.5.2 973 4/1/2023
1.5.1 275 3/31/2023
1.4.1 913 2/17/2023