In modern .NET applications, maintaining clean architecture is critical — and that’s where the Mediator pattern shines. It decouples the request/response flow and promotes separation of concerns.
While libraries like MediatR are popular, sometimes you want something even lighter — with zero dependencies.
Enter: GMediator, a custom lightweight mediator implementation.
In this blog, we’ll create a simple GET API using GMediator that returns a list of products stored in an in-memory store — no database, no ORM — just the core pattern in action.
๐งฑ Step 1: Define the Product Model
Code Snippet
๐พ Step 2: Create an In-Memory Store
Code Snippet
๐ฌ Step 3: Create the Query Request
Code Snippet
๐ฏ Step 5: Create the Controller
Code Snippet
๐งช Step 6: Register GMediator in Program.cs
Code Snippet
๐ก Final Thoughts
GMediator is a great option for internal projects, low-dependency APIs, or learning the Mediator pattern hands-on.
You can extend this by adding Create, Update, Delete, or integrating with a real database like SQLite or SQL Server.
No comments:
Post a Comment