|
1 | | -# Pandatech.FluentMinimalApiMapper - Simplify Your API Routing |
| 1 | +# Pandatech.FluentMinimalApiMapper |
2 | 2 |
|
3 | | -FluentMinimalApiMapper is a streamlined, focused NuGet package designed to bring ease and efficiency to the registration |
4 | | -of minimal API endpoints in ASP.NET Core applications, especially within modular monolithic architectures. Inspired by |
5 | | -the concept behind the Carter package, FluentMinimalApiMapper hones in on the essential feature of endpoint mapping, |
6 | | -presenting a lightweight and highly focused tool to enhance your project's structure and startup flow. |
| 3 | +A lightweight, dependency-free helper for auto-discovering and mapping Minimal API endpoints across your assemblies. |
7 | 4 |
|
8 | | -## Why No Carter? |
| 5 | +FluentMinimalApiMapper was inspired by **Carter**'s routing concept but focuses purely on endpoint mapping, ideal for * |
| 6 | +*modular monolithic** and **clean architecture** solutions. |
9 | 7 |
|
10 | | -While Carter offers a broad range of features for handling minimal APIs, its adaptability in modular monolithic |
11 | | -structures can be less than ideal. FluentMinimalApiMapper steps in to fill this gap, offering a specialized approach |
12 | | -that prioritizes seamless endpoint mapping across modular setups without the overhead of unused features. |
| 8 | +--- |
13 | 9 |
|
14 | | -## Considerations |
| 10 | +## ✨ Features |
15 | 11 |
|
16 | | -- **Startup Performance:** The package employs reflection to dynamically register endpoints, which may introduce a |
17 | | - slight |
18 | | - delay in startup time as the number of endpoints grows. This trade-off is considered for the benefit of reduced |
19 | | - boilerplate code and improved maintainability. |
| 12 | +- 🚀 Auto-discovers all `IEndpoint` implementations via reflection. |
| 13 | +- 🧩 Works seamlessly across multiple assemblies or modules. |
| 14 | +- 🔄 Zero configuration, minimal boilerplate. |
| 15 | +- ⚙️ Optional assembly scanning (defaults to EntryAssembly). |
20 | 16 |
|
21 | | -- **Assembly Scanning:** FluentMinimalApiMapper intelligently registers endpoints on a per-assembly basis. For projects |
22 | | - with a |
23 | | - singular API layer, manual assembly specification is unnecessary, as the package will automatically scan the current |
24 | | - assembly. |
| 17 | +--- |
25 | 18 |
|
26 | | -## Getting Started |
| 19 | +## 📦 Installation |
27 | 20 |
|
28 | | -Integrating FluentMinimalApiMapper into your project is straightforward. Below are the basic steps to set up your |
29 | | -`Program.cs` to leverage this package for automatic endpoint registration. |
| 21 | +```bash |
| 22 | +dotnet add package Pandatech.FluentMinimalApiMapper |
| 23 | +``` |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## 🧭 Quick Start |
| 28 | + |
| 29 | +### 1️⃣ Define an endpoint |
| 30 | + |
| 31 | +```csharp |
| 32 | +using FluentMinimalApiMapper; |
| 33 | +using Microsoft.AspNetCore.Routing; |
| 34 | + |
| 35 | +public class MyEndpoint : IEndpoint |
| 36 | +{ |
| 37 | + public void AddRoutes(IEndpointRouteBuilder app) |
| 38 | + { |
| 39 | + app.MapGet("/hello", () => "Hello, world!"); |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
30 | 43 |
|
31 | | -### Program.cs Registration Example |
| 44 | +### 2️⃣ Register endpoints in *Program.cs* |
| 45 | + |
| 46 | +#### Single assembly |
32 | 47 |
|
33 | 48 | ```csharp |
| 49 | +using FluentMinimalApiMapper; |
| 50 | + |
34 | 51 | var builder = WebApplication.CreateBuilder(args); |
35 | | -// Single project with one assembly |
36 | 52 | builder.AddMinimalApis(); |
| 53 | + |
37 | 54 | var app = builder.Build(); |
38 | | -app.MapEndpoints(); |
| 55 | +app.MapMinimalApis(); |
39 | 56 | app.Run(); |
40 | 57 | ``` |
41 | 58 |
|
| 59 | +#### Multiple assemblies (modular setup) |
| 60 | + |
42 | 61 | ```csharp |
| 62 | +using FluentMinimalApiMapper; |
| 63 | + |
43 | 64 | var builder = WebApplication.CreateBuilder(args); |
44 | | -// Multiple projects with multiple assemblies (e.g., modular monolithic) |
45 | | -builder.AddEndpoints([typeof(Startup).Assembly, typeof(OtherAssembly).Assembly]); |
| 65 | +builder.AddMinimalApis(typeof(MyEndpoint).Assembly, typeof(OtherModuleEndpoint).Assembly); |
| 66 | + |
46 | 67 | var app = builder.Build(); |
47 | 68 | app.MapMinimalApis(); |
48 | 69 | app.Run(); |
49 | 70 | ``` |
50 | 71 |
|
51 | | -### Endpoint Example |
| 72 | +--- |
| 73 | + |
| 74 | +## ⚙️ Advanced |
| 75 | + |
| 76 | +### Route grouping / versioning |
| 77 | + |
| 78 | +You can pass a `RouteGroupBuilder` to group endpoints: |
52 | 79 |
|
53 | 80 | ```csharp |
54 | | -public class MyEndpoint : IEndpoint |
55 | | -{ |
56 | | - public void AddRoutes(IEndpointRouteBuilder app) |
57 | | - { |
58 | | - app.MapGet("/myroute", () => "Hello, World!"); |
59 | | - } |
60 | | -} |
| 81 | +var generalApiPolicy = app.MapGroup("").DisableAntiForgery(); |
| 82 | +app.MapMinimalApis(generalApiPolicy); |
61 | 83 | ``` |
62 | 84 |
|
63 | | -By encapsulating your endpoint definitions within classes that implement `IEndpoint`, you maintain a clean and organized |
64 | | -project structure, with the added benefit of FluentMinimalApiMapper's automatic discovery and registration capabilities. |
| 85 | +--- |
| 86 | + |
| 87 | +## 💡 Notes |
65 | 88 |
|
66 | | -## Conclusion |
| 89 | +- Reflection happens once at startup—negligible in most cases. |
| 90 | +- Uses `TryAddEnumerable` to prevent duplicate registrations. |
| 91 | +- Works seamlessly with dependency injection and test hosts. |
67 | 92 |
|
68 | | -FluentMinimalApiMapper is designed to offer a focused, efficient solution for managing minimal API endpoints in modular |
69 | | -monolithic architectures. Its design philosophy centers around simplicity and performance, catering specifically to |
70 | | -developers who seek a straightforward approach to endpoint mapping without the need for additional overhead. Adopt |
71 | | -FluentMinimalApiMapper today to streamline your API development process. |
| 93 | +--- |
72 | 94 |
|
73 | | -## License |
| 95 | +## 📄 License |
74 | 96 |
|
75 | | -Pandatech.FluentMinimalApiMapper is licensed under the MIT License. |
| 97 | +Licensed under the MIT License. |
| 98 | +Copyright Pandatech |
0 commit comments