Building Smart API Endpoints with CSharp Source Generators

🧭 Introduction

In modern .NET development, we often find ourselves repeating the same patterns to expose CRUD operations for our models. Yes, frameworks like ASP.NET  Core simplify things with conventions and routing helpers, but what if we could go further? What if we could:
βœ… Auto-generate endpoints
βœ… Integrate with Entity Framework
βœ… Avoid writing any boilerplate
βœ… Customize behaviors when needed
That’s where Roslyn Source Generators come in. In this post, I’ll show you how I built a powerful yet flexible tool called AutoApiGenerator, capable of generating full REST endpoints at compile-time, just by annotating your entity classes with an attribute.


πŸ”– The [AutoApi] Attribute

Let’s start simple. Here’s the attribute that tells the generator what to process:


πŸ“Œ Highlights:

PropertyDescription
RoutePrefixControls the base route for generated endpoints
DbContextTypeEnables EF Core support for database operations

πŸ—οΈ Generator Architecture

The generator pipeline is structured in a clean, composable way:

  1. Discover entity classes decorated with [AutoApi]

  2. Analyze their syntax and semantic models

  3. Generate two files per entity:

    • *.gen.cs β†’ fully auto-generated base CRUD endpoints

    • *.cs β†’ partial class to allow custom extension

🧬 Architecture Diagram:


βš™οΈ Source Generator Code Breakdown

1. πŸ”„ Initialization Phase

2. 🧠 Endpoint Generation Logic


πŸ—ƒοΈ EF Core Integration

If a DbContextType is specified, the generator automatically includes full EF-backed CRUD:

πŸ‘¨β€πŸ”¬ EF Core Features:

  • Supports async operations

  • Uses DI for DbContext

  • Handles ID-based lookups for GET /{id}PUT, and DELETE


🧩 Customization via Partial Classes

Your own logic can coexist with the generated one using partials:


πŸ“Š Comparative Table

ApproachBoilerplateCustomizableType-safeCompile-time
Manual CRUD Controllers❌ Highβœ… Yesβœ… Yes❌ No
Minimal APIs with Hand Coding⚠️ Mediumβœ… Yesβœ… Yes❌ No
πŸ”₯ AutoApiGeneratorβœ… Very Lowβœ… Yes (partials)βœ… Yesβœ… Yes

βœ… Key Benefits

🌟 Why Use AutoApiGenerator?

  • πŸ’‘ 90% less boilerplate β€“ No more repetitive controller code

  • 🧠 Type-safe β€“ Backed by Roslyn’s compiler-level analysis

  • πŸ› οΈ Customizable β€“ Extend or override any part with partials

  • πŸ—ƒοΈ EF Core Ready β€“ Auto-queries your DbContext

  • 🧾 Predictable β€“ Pure compile-time, no hidden runtime behavior


πŸ”š Final Thoughts

This pattern highlights the real strength of Roslyn Source Generators:
πŸ’Ό Professional-grade APIs with minimal effort
πŸ“ Consistency across all endpoints
βš™οΈ Automation without sacrificing flexibility
Whether you're building admin panels, internal tools, or just speeding up prototyping β€” this generator saves time while improving code quality.


πŸ“¦ GitHub Repository

Want to try it yourself or contribute?
πŸ‘‰ View the full source on GitHub 

An unhandled error has occurred. Reload πŸ—™