prettier way for naming and code suggestions
Opened this issue · 0 comments
You asked for a prettier way to name your getters. You can actually use generics here with generic type constraints. You just have to put the methods in different extension classes.
public static class {typeNode.Identifier}DataReaderExtensions {
public static List<{{typeNodeSymbol.FullName()}}> To<T>(this IDataReader dr) where T : {{typeNodeSymbol.FullName()}}
}
Also instead of populating a full List synchronously, you may want to return IAsyncEnumerable<T>
and populate it with yield return
and use async functions to read from reader (but you will need IAsyncEnumerable<{{typeNodeSymbol.FullName()}}> ToAsync<T>(this DbDataReader dr)
instead of IDataReader
to access async functions like ReadAsync
and GetFieldValueAsync
)
For compilers sanity, please use null checks:
if (context.SyntaxContextReceiver is not TargetTypeTracker targetTypeTracker)
{
return;
}
foreach (var typeNode in targetTypeTracker.TypesNeedingGening)
{
if (context.Compilation
.GetSemanticModel(typeNode.SyntaxTree)
.GetDeclaredSymbol(typeNode) is INamedTypeSymbol typeNodeSymbol)
...
If you can't use the new is c# feature, just change the <LangVersion>preview</LangVersion>
in your generators csproj. You can use a higher c# version even with <TargetFramework>netstandard2.0</TargetFramework>
When you use c# 11+ it's better to use raw string literals for code to avoid the indents.
With $$"""
you can use single brackets { } to appear in text and double brackets for variables {{typeNodeSymbol.FullName()}}
If you compile your nuget with github actions it will generate the code with \n newlines mixed with your hardcoded \r\n. It would be better to use something like
static readonly string nl = @"
";
and replace your \r\n
with {{nl}}