InternetException

source generators

source generators · 4 posts (all tags)

Type is safe to use for equality in Roslyn source generators

· 338 words · ~1 min read · in .NET, C#

#source generators #records #Roslyn

Lately I am playing a lot with Roslyn Source Code generators. This allows you to do interesting things, like automatic generation of validation classes based on data in the domain layer. Or generating tests that check if your API endpoints return 200 OK when a bare GET request is sent.

One thing you have to remember to do is use types with strict value equality. Otherwise it may work incorrectly or it may leak memory. You can, for example, use records that do that for you, with the gotcha being no extra fields or properties. Because of that I was mostly translating Types into other values, i.e., the full name of the type, to avoid equality issues. But it turns out you can use it in IValueProvider directly.

Read the whole thing →

Using Junie agent CLI with local model

· 573 words · ~2 min read · in AI

#coding agents #source generators

Jet Brains have their own coding agent. Since I am already using Rider and all coding agents are pretty slow and inefficient with what they do I wanted to test if Junie have better integrations with IDE which would make things much faster.

For example if you will have following unused namespace in Rider.

Removing it all cleaning all unused namespaces in entire project or even entire solution is just few clicks away. Or key strokes.

Read the whole thing →

Writing a simple C# source code generator with Fluent API

· 1812 words · ~9 min read · updated · in .NET, C#

#source generators #NuGet

Introduction

Usually in .NET world when you want to write generic code you use generic method or classes. But sometimes it is still not enough when you want to automate some things. I.e. you want to dynamically register in DI all the classes that implements some interface. Probably, in this case, you would do something like that:

var implementations = typeof(MyClass).Assembly.Types
   .Where(t => t.GetInterfaces().Any(i => i == typeof(IMyInterface))
foreach(var implementation in implementations)
{
   services.AddScoped<IMyInterface>(implementation);
}

And it would be completely fine. Even if Reflection is pretty slow, your application would not be getting significantly slower because of that. Certainly you would not notice the slow down.

Read the whole thing →

HamsterWheel.FluentCodeGenerators is out

· 159 words · ~1 min read · in Hamster Wheel, .NET

#source generators #NuGet

I released today first new Nuget package connected to my new project that I am working on for sometime: dynamically configurable, zero-downtime API, with low-code flows to write small pieces of logic - Hamster Wheel.

System heavily relies on dynamic source code generation. For that I wrote
https://www.nuget.org/packages/HamsterWheel.FluentCodeGenerators and https://www.nuget.org/packages/HamsterWheel.FluentCodeGenerators.Abstractions
small package that helps with writing C# incremental source code generators for Roslyn compiler.

Original Roslyn APIs are a bit hard to use so this package helps with that. Also it helps with:
\

Read the whole thing →