Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions wiki/advanced-features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Advanced Features

Mediator.Net offers several advanced features to enhance its functionality.

## Context Services

Share services between middleware and handlers:

```csharp
// In middleware
public Task Execute(TContext context, CancellationToken cancellationToken)
{
context.RegisterService(new AuditInfo { Timestamp = DateTime.UtcNow });
return Task.CompletedTask;
}

// In handler
public async Task Handle(IReceiveContext<MyCommand> context, CancellationToken cancellationToken)
{
if (context.TryGetService(out AuditInfo auditInfo))
{
// Use the audit info
}
}
```

## Publishing Events from Handlers

```csharp
public async Task Handle(IReceiveContext<CreateOrderCommand> context, CancellationToken cancellationToken)
{
// Process the command
var order = new Order(context.Message.CustomerId);

// Publish domain event
await context.Publish(new OrderCreatedEvent
{
OrderId = order.Id,
CustomerId = order.CustomerId
});
}
3 changes: 3 additions & 0 deletions wiki/contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Contributing

We welcome contributions! Please see our [Contributing Guide](../CONTRIBUTING.md) for details.
37 changes: 37 additions & 0 deletions wiki/dependency-injection-integration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Dependency Injection Integration

Mediator.Net integrates with various dependency injection containers to manage the lifecycle of handlers and other components.

## Microsoft.Extensions.DependencyInjection

```bash
Install-Package Mediator.Net.MicrosoftDependencyInjection
```

```csharp
services.AddMediator(builder =>
{
builder.RegisterHandlers(typeof(Program).Assembly);
});
```

## Autofac

```bash
Install-Package Mediator.Net.Autofac
```

```csharp
var builder = new ContainerBuilder();
var mediatorBuilder = new MediatorBuilder()
.RegisterHandlers(typeof(Program).Assembly);

builder.RegisterMediator(mediatorBuilder);
var container = builder.Build();
```

## Other Supported Containers

- **SimpleInjector**: `Mediator.Net.SimpleInjector`
- **StructureMap**: `Mediator.Net.StructureMap`
- **Ninject**: `Mediator.Net.Ninject`
9 changes: 9 additions & 0 deletions wiki/documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Documentation

For more detailed documentation, examples, and advanced scenarios, visit our [Wiki](https://github.com/mayuanyang/Mediator.Net/wiki).

For additional resources and support, please refer to the following:

- [Contributing Guide](../CONTRIBUTING.md)
- [License](../LICENSE.txt)
- [Support](../README.md#️-support)
11 changes: 11 additions & 0 deletions wiki/features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Features

Mediator.Net offers the following features:

- **Command/Query Separation**: Clear separation between commands, queries, and events.
- **Pipeline Support**: Extensible middleware pipeline for cross-cutting concerns.
- **Streaming Support**: Handle multiple responses with `IAsyncEnumerable`.
- **Dependency Injection**: Built-in support for popular IoC containers.
- **Event Publishing**: Publish events from within handlers.
- **Flexible Registration**: Both explicit and assembly scanning registration.
- **Middleware Ecosystem**: Rich collection of pre-built middlewares.
23 changes: 23 additions & 0 deletions wiki/handler-registration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Handler Registration

Mediator.Net supports two methods for registering handlers: assembly scanning and explicit registration.

## Assembly Scanning (Recommended)

```csharp
var mediator = new MediatorBuilder()
.RegisterHandlers(typeof(Program).Assembly)
.Build();
```

## Explicit Registration

```csharp
var mediator = new MediatorBuilder()
.RegisterHandlers(() => new List<MessageBinding>
{
new MessageBinding(typeof(CreateUserCommand), typeof(CreateUserCommandHandler)),
new MessageBinding(typeof(GetUserQuery), typeof(GetUserQueryHandler)),
new MessageBinding(typeof(UserCreatedEvent), typeof(UserCreatedEventHandler))
})
.Build();
17 changes: 17 additions & 0 deletions wiki/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Mediator.Net Wiki

This is the wiki for the Mediator.Net project. Below are the sections that provide detailed information about the project.

- [Features](features.md)
- [Installation](installation.md)
- [Quick Start](quick-start.md)
- [Usage Examples](usage-examples.md)
- [Handler Registration](handler-registration.md)
- [Pipeline & Middleware](pipeline-middleware.md)
- [Dependency Injection Integration](dependency-injection-integration.md)
- [Official Middleware Packages](official-middleware-packages.md)
- [Advanced Features](advanced-features.md)
- [Documentation](documentation.md)
- [Contributing](contributing.md)
- [License](license.md)
- [Support](support.md)
14 changes: 14 additions & 0 deletions wiki/installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Installation

To install Mediator.Net, you can use NuGet or the .NET CLI.

## Using NuGet

```bash
Install-Package Mediator.Net
```

## Using .NET CLI

```bash
dotnet add package Mediator.Net
3 changes: 3 additions & 0 deletions wiki/license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License

This project is licensed under the MIT License - see the [LICENSE.txt](../LICENSE.txt) file for details.
29 changes: 29 additions & 0 deletions wiki/official-middleware-packages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Official Middleware Packages

Mediator.Net provides several official middleware packages to extend its functionality.

## Serilog Logging

```bash
Install-Package Mediator.Net.Middlewares.Serilog
```

```csharp
.ConfigureGlobalReceivePipe(x => x.UseSerilog(LogEventLevel.Information))
```

## Unit of Work

```bash
Install-Package Mediator.Net.Middlewares.UnitOfWork
```

Provides `CommittableTransaction` support for transactional operations.

## EventStore Integration

```bash
Install-Package Mediator.Net.Middlewares.EventStore
```

Automatically persists events to EventStore.
85 changes: 85 additions & 0 deletions wiki/pipeline-middleware.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Pipeline & Middleware

Mediator.Net supports five types of pipelines for different scenarios:

![Pipeline Architecture](https://cloud.githubusercontent.com/assets/3387099/21959127/9a065420-db09-11e6-8dbc-ca0069894e1c.png)

## Pipeline Types

| Pipeline | Description | Triggers For |
|----------|-------------|--------------|
| **GlobalReceivePipeline** | Executes for all messages | Commands, Requests, Events |
| **CommandReceivePipeline** | Executes only for commands | ICommand |
| **RequestReceivePipeline** | Executes only for requests | IRequest |
| **EventReceivePipeline** | Executes only for events | IEvent |
| **PublishPipeline** | Executes when events are published | IEvent (outgoing) |

## Creating Custom Middleware

### 1. Create Middleware Extension

```csharp
public static class LoggingMiddleware
{
public static void UseLogging<TContext>(
this IPipeConfigurator<TContext> configurator,
ILogger logger = null)
where TContext : IContext<IMessage>
{
logger ??= configurator.DependencyScope?.Resolve<ILogger>();
configurator.AddPipeSpecification(new LoggingMiddlewareSpecification<TContext>(logger));
}
}
```

### 2. Create Middleware Specification

```csharp
public class LoggingMiddlewareSpecification<TContext> : IPipeSpecification<TContext>
where TContext : IContext<IMessage>
{
private readonly ILogger _logger;

public LoggingMiddlewareSpecification(ILogger logger)
{
_logger = logger;
}

public bool ShouldExecute(TContext context, CancellationToken cancellationToken) => true;

public Task BeforeExecute(TContext context, CancellationToken cancellationToken)
{
_logger.LogInformation("Processing message: {MessageType}", context.Message.GetType().Name);
return Task.CompletedTask;
}

public Task Execute(TContext context, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}

public Task AfterExecute(TContext context, CancellationToken cancellationToken)
{
_logger.LogInformation("Completed processing: {MessageType}", context.Message.GetType().Name);
return Task.CompletedTask;
}

public void OnException(Exception ex, TContext context)
{
_logger.LogError(ex, "Error processing message: {MessageType}", context.Message.GetType().Name);
throw ex;
}
}
```

## Configuring Pipelines

```csharp
var mediator = new MediatorBuilder()
.RegisterHandlers(typeof(Program).Assembly)
.ConfigureGlobalReceivePipe(x => x.UseLogging())
.ConfigureCommandReceivePipe(x => x.UseValidation())
.ConfigureRequestPipe(x => x.UseCaching())
.ConfigureEventReceivePipe(x => x.UseEventStore())
.ConfigurePublishPipe(x => x.UseOutboxPattern())
.Build();
66 changes: 66 additions & 0 deletions wiki/quick-start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Quick Start

## Basic Setup

To get started with Mediator.Net, you can create and configure the mediator as follows:

```csharp
// Create and configure mediator
var mediaBuilder = new MediatorBuilder();
var mediator = mediaBuilder.RegisterHandlers(typeof(Program).Assembly).Build();
```

## Define Messages and Handlers

Define your messages and handlers as shown below:

```csharp
// Command (no response)
public class CreateUserCommand : ICommand
{
public string Name { get; set; }
public string Email { get; set; }
}

public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand>
{
public async Task Handle(IReceiveContext<CreateUserCommand> context, CancellationToken cancellationToken)
{
// Handle the command
var user = new User(context.Message.Name, context.Message.Email);
// Save user...

// Publish an event
await context.Publish(new UserCreatedEvent { UserId = user.Id });
}
}

// Request/Response
public class GetUserQuery : IRequest<UserDto>
{
public int UserId { get; set; }
}

public class GetUserQueryHandler : IRequestHandler<GetUserQuery, UserDto>
{
public async Task<UserDto> Handle(IReceiveContext<GetUserQuery> context, CancellationToken cancellationToken)
{
// Handle the query and return response
return new UserDto { Id = context.Message.UserId, Name = "John Doe" };
}
}

// Event
public class UserCreatedEvent : IEvent
{
public int UserId { get; set; }
}

public class UserCreatedEventHandler : IEventHandler<UserCreatedEvent>
{
public async Task Handle(IReceiveContext<UserCreatedEvent> context, CancellationToken cancellationToken)
{
// Handle the event
Console.WriteLine($"User {context.Message.UserId} was created!");
}
}
7 changes: 7 additions & 0 deletions wiki/support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Support

For support and questions, please refer to the following resources:

- [Documentation](../README.md#-documentation)
- [Stack Overflow](http://stackoverflow.com/questions/tagged/memdiator.net) (use the `mediator.net` tag)
- [Issues](https://github.com/mayuanyang/Mediator.Net/issues)
Loading
Loading