u/FreakyAly

▲ 2 r/csharp+1 crossposts

FreakyKit.Forge - A Roslyn source generator for object mapping

I've always been a manual mapping guy. Reflection based mappers just never made sense to me, why would I trade a runtime surprise for convenience?

A couple of years ago I was on a project that used an internal source generator for this. The diagnostics were deep but very specific to that project. That was the thing that got me though - the idea that you could get that kind of build time feedback for object mapping of all things. I've been hacking on something similar for my own projects since then and finally decided to clean it up and ship it.

This is FreakyKit.Forge. You declare the method signature and it generates the body at compile time. No reflection, no runtime dependencies, just plain C# assignments that are identical to what you'd write yourself.

[Forge]
public static partial class PersonForges
{
    public static partial PersonDto ToDto(Person source);
}

Spits out:

public static partial PersonDto ToDto(Person source)
{
    var __result = new PersonDto();
    __result.Name = source.Name;
    __result.Age = source.Age;
    return __result;
}

Benchmarks on .NET 8 show it's basically identical to hand-written code. Full numbers vs other libraries here.

It ships with 44 build time diagnostics. That part matters to me more than anything else on the feature list. If the build passes the mapping is correct.

Still working on it. EF Core projection expressions are next, then polymorphic mapping, reverse mapping and a few other things. Full roadmap here if you're curious.

What I actually want:

Not here to tell anyone to drop what's working for them. I just want to know what's still broken in this space that nobody has fixed properly. That's genuinely more useful to me than anything else right now.

If you believe manual mapping is the way, Please feel free to elaborate on what that one missing feature or problem is that, if solved, you would consider using a "mapper".

GitHub | NuGet | Benchmarks | Diagnostics | Roadmap

u/FreakyAly — 11 days ago