SourceGenerators
Last updated
Was this helpful?
Last updated
Was this helpful?
Vidyano makes use of the to offer various helpers.
The latest Vidyano project template will enable this by default, otherwise there are a few things that need to be added to the .csproj to provide enough context for it to work.
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
Actions Generator
BusinessRules Generator
Context Generator
CustomAction Generator
Index Generator
Model Generator
ProjectTargetType Generator
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
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.
Attribute is available via reference Vidyano.Abstractions
.
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 GenerateIndex
attribute 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.
Additional Files & Global Usings
Add following ItemGroup to the .csproj
project file.
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.
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
By ignore the warning via csproj file
When using EmitCompilerGeneratedFiles in .csproj
project file you can get exceptions when compiling in Windows because of use of long filenames.
To fix this issue you need to add the following registration key (Reboot needed to take effect)
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.
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.