Skip to content

Commit 77b7008

Browse files
committed
polished and namespace changed
1 parent f93bdcd commit 77b7008

12 files changed

Lines changed: 207 additions & 113 deletions

FluentMinimalApiMapper.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1212
global.json = global.json
1313
EndProjectSection
1414
EndProject
15+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{C7538F9E-E3D4-4987-B6D8-177B6709A38A}"
16+
EndProject
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentMinimalApiMapper.Demo", "tests\FluentMinimalApiMapper.Demo\FluentMinimalApiMapper.Demo.csproj", "{D8C76F94-457E-4CBC-9A4B-964AB98F1B03}"
18+
EndProject
1519
Global
1620
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1721
Debug|Any CPU = Debug|Any CPU
@@ -22,8 +26,13 @@ Global
2226
{25001943-A870-4E17-A9B9-0D190CEC819B}.Debug|Any CPU.Build.0 = Debug|Any CPU
2327
{25001943-A870-4E17-A9B9-0D190CEC819B}.Release|Any CPU.ActiveCfg = Release|Any CPU
2428
{25001943-A870-4E17-A9B9-0D190CEC819B}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{D8C76F94-457E-4CBC-9A4B-964AB98F1B03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30+
{D8C76F94-457E-4CBC-9A4B-964AB98F1B03}.Debug|Any CPU.Build.0 = Debug|Any CPU
31+
{D8C76F94-457E-4CBC-9A4B-964AB98F1B03}.Release|Any CPU.ActiveCfg = Release|Any CPU
32+
{D8C76F94-457E-4CBC-9A4B-964AB98F1B03}.Release|Any CPU.Build.0 = Release|Any CPU
2533
EndGlobalSection
2634
GlobalSection(NestedProjects) = preSolution
2735
{25001943-A870-4E17-A9B9-0D190CEC819B} = {F8A6DCFE-8924-49A4-B3E9-2034593F54E5}
36+
{D8C76F94-457E-4CBC-9A4B-964AB98F1B03} = {C7538F9E-E3D4-4987-B6D8-177B6709A38A}
2837
EndGlobalSection
2938
EndGlobal

Readme.md

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,98 @@
1-
# Pandatech.FluentMinimalApiMapper - Simplify Your API Routing
1+
# Pandatech.FluentMinimalApiMapper
22

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.
74

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.
97

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+
---
139

14-
## Considerations
10+
## ✨ Features
1511

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).
2016

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+
---
2518

26-
## Getting Started
19+
## 📦 Installation
2720

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+
```
3043

31-
### Program.cs Registration Example
44+
### 2️⃣ Register endpoints in *Program.cs*
45+
46+
#### Single assembly
3247

3348
```csharp
49+
using FluentMinimalApiMapper;
50+
3451
var builder = WebApplication.CreateBuilder(args);
35-
// Single project with one assembly
3652
builder.AddMinimalApis();
53+
3754
var app = builder.Build();
38-
app.MapEndpoints();
55+
app.MapMinimalApis();
3956
app.Run();
4057
```
4158

59+
#### Multiple assemblies (modular setup)
60+
4261
```csharp
62+
using FluentMinimalApiMapper;
63+
4364
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+
4667
var app = builder.Build();
4768
app.MapMinimalApis();
4869
app.Run();
4970
```
5071

51-
### Endpoint Example
72+
---
73+
74+
## ⚙️ Advanced
75+
76+
### Route grouping / versioning
77+
78+
You can pass a `RouteGroupBuilder` to group endpoints:
5279

5380
```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);
6183
```
6284

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
6588

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.
6792

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+
---
7294

73-
## License
95+
## 📄 License
7496

75-
Pandatech.FluentMinimalApiMapper is licensed under the MIT License.
97+
Licensed under the MIT License.
98+
Copyright Pandatech

src/FluentMinimalApiMapper/AddEndpointExtension.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/FluentMinimalApiMapper/FluentMinimalApiMapper.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>2.0.4</Version>
11+
<Version>3.0.0</Version>
1212
<PackageId>Pandatech.FluentMinimalApiMapper</PackageId>
1313
<Title>FluentMinimalApiMapper: Effortless Endpoint Integration</Title>
1414
<PackageTags>Pandatech, library, MinimalAPI, AutoMapping, FluentAPI, EndpointRegistration</PackageTags>
1515
<Description>FluentMinimalApiMapper redefines the simplicity of minimal API development, offering an intuitive and automated approach to endpoint mapping. With a focus on streamlined integration, this NuGet package by PandaTech is the perfect tool for developers looking to enhance the efficiency and readability of their modular applications. Dive into the seamless world of API development with FluentMinimalApiMapper, where every route is a journey simplified.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-fluent-minimal-api-mapper</RepositoryUrl>
17-
<PackageReleaseNotes>Nuget updates</PackageReleaseNotes>
17+
<PackageReleaseNotes>Nuget updates + Polished + Updated Readme + Namespace change</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>
@@ -23,7 +23,7 @@
2323
</ItemGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.8" />
26+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.10" />
2727
</ItemGroup>
2828

2929
</Project>

src/FluentMinimalApiMapper/MapEndpointExtension.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Reflection;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Routing;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.DependencyInjection.Extensions;
6+
7+
namespace FluentMinimalApiMapper;
8+
9+
public static class WebAppExtensions
10+
{
11+
public static WebApplicationBuilder AddMinimalApis(this WebApplicationBuilder builder, params Assembly[] assemblies)
12+
{
13+
if (assemblies.Length == 0)
14+
{
15+
return builder;
16+
}
17+
18+
var candidates = assemblies
19+
.SelectMany(a => a.DefinedTypes)
20+
.Where(t => t is { IsAbstract: false, IsInterface: false } && t.IsAssignableTo(typeof(IEndpoint)))
21+
.Distinct()
22+
.ToArray();
23+
24+
foreach (var t in candidates)
25+
{
26+
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient(typeof(IEndpoint), t));
27+
}
28+
29+
return builder;
30+
}
31+
32+
public static WebApplicationBuilder AddMinimalApis(this WebApplicationBuilder builder)
33+
{
34+
var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
35+
return builder.AddMinimalApis(assembly);
36+
}
37+
38+
public static WebApplication MapMinimalApis(this WebApplication app, RouteGroupBuilder? routeGroupBuilder = null)
39+
{
40+
var endpoints = app.Services
41+
.GetRequiredService<IEnumerable<IEndpoint>>();
42+
43+
IEndpointRouteBuilder builder =
44+
routeGroupBuilder is null ? app : routeGroupBuilder;
45+
46+
foreach (var endpoint in endpoints)
47+
{
48+
endpoint.AddRoutes(builder);
49+
}
50+
51+
return app;
52+
}
53+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace FluentMinimalApiMapper.Demo;
2+
3+
public class Endpoints : IEndpoint
4+
{
5+
public void AddRoutes(IEndpointRouteBuilder app)
6+
{
7+
app.MapGet("/", () => "Hello World!");
8+
}
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.10"/>
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.6" />
12+
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="9.0.6" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\..\src\FluentMinimalApiMapper\FluentMinimalApiMapper.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using FluentMinimalApiMapper;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
builder.AddMinimalApis();
6+
builder.Services.AddEndpointsApiExplorer();
7+
builder.Services.AddSwaggerGen();
8+
9+
10+
var app = builder.Build();
11+
12+
app.MapMinimalApis();
13+
app.UseSwagger();
14+
app.UseSwaggerUI();
15+
16+
17+
app.Run();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "http://localhost",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)