Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A lightweight and straightforward mediator implementation for .NET applications, facilitating in-process messaging with minimal setup.

License

NotificationsYou must be signed in to change notification settings

NetDevPack/SimpleMediator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

.NET DevPack

SimpleMediator

A lightweight and straightforward mediator implementation for .NET applications, facilitating in-process messaging with minimal setup.

PackageVersionPopularity
NetDevPack.SimpleMediatorNuGetNuget

Give a Star! ⭐

If you found this project helpful or it assisted you in any way, please give it a star. It helps to support the project and the developers.

Samples

You can find complete example projects demonstrating how to use the SimpleMediator in the/samples folder.

These include:

  • ✅ Basic usage withSend andPublish
  • ✅ Modular application structure
  • ✅ Manual and automatic registration of handlers

Feel free to explore and run them to see how the mediator works in different scenarios.

Getting Started

Installation

You can install the SimpleMediator package via NuGet Package Manager or the .NET CLI:

dotnet add package NetDevPack.SimpleMediator

Using Contracts-Only Package

To reference only the contracts for SimpleMediator, which includes:

  • IRequest (including generic variants)
    • Represents a command or query that expects a single response
  • INotification
    • Represents an event broadcast to multiple handlers (if any)

Advanced Usage: Request + Notification

This example demonstrates how to combine aRequest (command/query) and aNotification (event) in a real-world use case.

✅ This scenario uses onlyMicrosoft.Extensions.DependencyInjection.Abstractions for DI registration — no framework-specific packages.


1. Define the Request and Notification

publicclassCreateCustomerCommand:IRequest<string>{publicstringName{get;set;}}publicclassCustomerCreatedEvent:INotification{publicGuidCustomerId{get;}publicCustomerCreatedEvent(GuidcustomerId){CustomerId=customerId;}}

2. Implement the Handlers

publicclassCreateCustomerHandler:IRequestHandler<CreateCustomerCommand,string>{privatereadonlyIMediator_mediator;publicCreateCustomerHandler(IMediatormediator){_mediator=mediator;}publicasyncTask<string>Handle(CreateCustomerCommandrequest,CancellationTokencancellationToken){varid=Guid.NewGuid();// Simulate persistence...// Publish eventawait_mediator.Publish(newCustomerCreatedEvent(id),cancellationToken);return$"Customer '{request.Name}' created with ID{id}";}}publicclassSendWelcomeEmailHandler:INotificationHandler<CustomerCreatedEvent>{publicTaskHandle(CustomerCreatedEventnotification,CancellationTokencancellationToken){Console.WriteLine($"Sending welcome email to customer{notification.CustomerId}");returnTask.CompletedTask;}}

3. Register the Handlers (Dependency Injection)

You can register everything manually if you want full control:

services.AddSingleton<IMediator,Mediator>();services.AddTransient<IRequestHandler<CreateCustomerCommand,string>,CreateCustomerHandler>();services.AddTransient<INotificationHandler<CustomerCreatedEvent>,SendWelcomeEmailHandler>();

Or use assembly scanning with:

services.AddSimpleMediator();

4. Execute the Flow

publicclassCustomerAppService{privatereadonlyIMediator_mediator;publicCustomerAppService(IMediatormediator){_mediator=mediator;}publicasyncTask<string>CreateCustomer(stringname){returnawait_mediator.Send(newCreateCustomerCommand{Name=name});}}

When theCreateCustomer method is called:

  1. CreateCustomerHandler handles the request
  2. It creates and persists the customer (simulated)
  3. It publishes aCustomerCreatedEvent
  4. SendWelcomeEmailHandler handles the event

This structure cleanly separatescommands (which change state and return a result) fromnotifications (which communicate to the rest of the system that something happened).

Features

  • Lightweight: Minimal dependencies and straightforward setup.
  • In-Process Messaging: Facilitates in-process communication between components.
  • Handler Registration: Automatically registers handlers from specified assemblies.

Compatibility

NetDevPack.SimpleMediator targets .NET Standard 2.1, and is compatible with .NET Core 3.1+, .NET 5+, .NET 6+, .NET 7+, .NET 8, and newer versions of the .NET runtime.

About

NetDevPack.SimpleMediator was developed byEduardo Pires under the MIT license.

About

A lightweight and straightforward mediator implementation for .NET applications, facilitating in-process messaging with minimal setup.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp