-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from june-it/develop
Develop
- Loading branch information
Showing
19 changed files
with
258 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,88 @@ | ||
# MyStack.LocalMessage | ||
|
||
开源的轻量级本地消息总线类库 | ||
Open-source lightweight local message bus library | ||
|
||
# Getting Started | ||
|
||
## Add Service Support | ||
``` | ||
services.AddLocalMessage(Assembly.GetExecutingAssembly()); | ||
``` | ||
|
||
## 1、Event Subscription and Publication | ||
### Define Events | ||
``` | ||
public class HelloEvent: ILocalEvent | ||
{ | ||
} | ||
``` | ||
|
||
### Subscribe to Events | ||
``` | ||
public class HelloEventHandler : ILocalEventHandler<HelloEvent> | ||
{ | ||
public async Task HandleAsync(HelloEvent eventData, CancellationToken cancellationToken = default) | ||
{ | ||
await Task.CompletedTask; | ||
} | ||
} | ||
``` | ||
### Publish Events | ||
``` | ||
await eventBus.PublishAsync(new HelloEvent()); | ||
``` | ||
## 2、Request/Response Subscription and Publication | ||
### Define Requests | ||
``` | ||
public class Ping : IRequest<Pong> | ||
{ | ||
} | ||
``` | ||
### Define Responses | ||
``` | ||
public class Pong | ||
{ | ||
} | ||
``` | ||
|
||
### Subscribe to Requests and Return Response Results | ||
``` | ||
public class PingHandler : IRequestHandler<Ping, Pong> | ||
{ | ||
public Task<Pong> HandleAsync(Ping eventData, CancellationToken cancellationToken = default) | ||
{ | ||
return Task.FromResult(new Pong()); | ||
} | ||
} | ||
``` | ||
### Publish Requests | ||
``` | ||
var pongMessage = eventBus.SendAsync(ping); | ||
``` | ||
|
||
## 3、Wrapped Event Data Subscription and Publication | ||
### Define Wrapped Event Data | ||
``` | ||
public class WrappedEventData | ||
{ | ||
} | ||
``` | ||
|
||
### Subscribe to Wrapped Event Data | ||
``` | ||
public class WrappedEventHandler : ILocalEventHandler<LocalEventWrapper<WrappedEventData>> | ||
{ | ||
public async Task HandleAsync(LocalEventWrapper<WrappedEventData> eventData, CancellationToken cancellationToken = default) | ||
{ | ||
await Task.CompletedTask; | ||
} | ||
} | ||
``` | ||
|
||
### Publish Wrapped Event Data | ||
``` | ||
eventBus.SendAsync(new WrappedEventData()); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Extensions.LocalMessage | ||
{ | ||
/// <summary> | ||
/// Represents the event publisher interface | ||
/// </summary> | ||
public interface IEventPublisher | ||
{ | ||
/// <summary> | ||
/// Publish an Event | ||
/// </summary> | ||
/// <param name="eventData">Event</param> | ||
/// <param name="cancellationToken">Cancellation Token</param> | ||
/// <returns></returns> | ||
Task PublishAsync(ILocalEvent eventData, CancellationToken cancellationToken = default); | ||
/// <summary> | ||
/// Publish an Event Data | ||
/// </summary> | ||
/// <param name="eventData">Event data</param> | ||
/// <param name="cancellationToken">Cancellation Token</param> | ||
/// <returns></returns> | ||
Task PublishAsync(object eventData, CancellationToken cancellationToken = default); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
namespace Microsoft.Extensions.LocalMessage | ||
{ | ||
/// <summary> | ||
/// 表示一个事件接口 | ||
/// Represents an event interface | ||
/// </summary> | ||
public interface ILocalEvent | ||
{ | ||
{ | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,16 @@ | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using System.Xml.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Extensions.LocalMessage | ||
{ | ||
/// <summary> | ||
/// 表示一个本地事件订阅接口 | ||
/// Represents an event subscription interface | ||
/// </summary> | ||
public interface ILocalEventHandler | ||
{ | ||
} | ||
/// <summary> | ||
/// 表示一个事件订阅接口 | ||
/// </summary> | ||
/// <typeparam name="TEvent">事件的类型</typeparam> | ||
public interface ILocalEventHandler<TEvent> : ILocalEventHandler | ||
/// <typeparam name="TEvent">The type of the event</typeparam> | ||
public interface ILocalEventHandler<TEvent> | ||
where TEvent : class, ILocalEvent | ||
{ | ||
Task HandleAsync(TEvent eventData, CancellationToken cancellationToken = default); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,9 @@ | ||
using Microsoft.Extensions.LocalMessage; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Extensions.LocalMessage | ||
namespace Microsoft.Extensions.LocalMessage | ||
{ | ||
/// <summary> | ||
/// 表示本地事件总线接口 | ||
/// Represents the local message bus interface | ||
/// </summary> | ||
public interface ILocalMessageBus | ||
public interface ILocalMessageBus : IEventPublisher, IRequestSender | ||
{ | ||
/// <summary> | ||
/// 发布一个事件 | ||
/// </summary> | ||
/// <param name="eventData">事件数据</param> | ||
/// <param name="cancellationToken">取消令牌</param> | ||
/// <returns></returns> | ||
Task PublishAsync(ILocalEvent eventData, CancellationToken cancellationToken = default); | ||
/// <summary> | ||
/// 发布一个事件体 | ||
/// </summary> | ||
/// <param name="eventData">事件体对象</param> | ||
/// <param name="cancellationToken">取消令牌</param> | ||
/// <returns></returns> | ||
Task PublishAsync(object eventData, CancellationToken cancellationToken = default); | ||
/// <summary> | ||
/// 发送一个有返回结果的事件请求 | ||
/// </summary> | ||
/// <typeparam name="TResponse">返回结果的类型</typeparam> | ||
/// <param name="requestData">事件数据</param> | ||
/// <param name="cancellationToken">取消令牌</param> | ||
/// <returns>处理成功时返回结果,否则返回null</returns> | ||
Task<TResponse?> SendAsync<TResponse>(IRequest<TResponse> requestData, CancellationToken cancellationToken = default) | ||
where TResponse : class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.