diff --git a/sample/Util.Samples.Webs/Configs/ServiceRegister.cs b/sample/Util.Samples.Webs/Configs/ServiceRegister.cs index fcab408c..758818dc 100644 --- a/sample/Util.Samples.Webs/Configs/ServiceRegister.cs +++ b/sample/Util.Samples.Webs/Configs/ServiceRegister.cs @@ -1,5 +1,4 @@ -using System; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; using Util.Dependency; using Util.Domains.Sessions; diff --git a/sample/Util.Samples.Webs/Controllers/HomeController.cs b/sample/Util.Samples.Webs/Controllers/HomeController.cs index d059a028..c4cbfa3b 100644 --- a/sample/Util.Samples.Webs/Controllers/HomeController.cs +++ b/sample/Util.Samples.Webs/Controllers/HomeController.cs @@ -1,56 +1,12 @@ using Microsoft.AspNetCore.Mvc; -using Util.Datas.Queries; -using Util.Dependency; -using Util.Events; -using Util.Events.Handlers; -using Util.Logs; -using Util.Logs.Extensions; -using Util.Reflections; namespace Util.Samples.Webs.Controllers { public class HomeController : Controller { - private IEventBus _eventBus; - public HomeController( IEventBus eventBus ) { - _eventBus = eventBus; + public HomeController() { } public IActionResult Index() { - _eventBus.Publish( Event.Create( new TestEventData { Name = "World" } ) ); return View(); } } - - public class TestEventData { - public string Name { get; set; } - } - - public class TestEventData2 { - public string Name { get; set; } - } - - public class TestHandler : IEventHandler { - - private ILog _log; - - public TestHandler( ILog log ) { - _log = log; - } - - public void Handle( Event @event ) { - _log.Caption( $"Hello,{@event.Data.Name}" ).Fatal(); - } - } - - public class TestHandler2 : IEventHandler { - - private ILog _log; - - public TestHandler2( ILog log ) { - _log = log; - } - - public void Handle( Event @event ) { - _log.Caption( $"Hello,{@event.Data.Name}-2" ).Info(); - } - } } diff --git a/sample/Util.Samples.Webs/Properties/launchSettings.json b/sample/Util.Samples.Webs/Properties/launchSettings.json index 8ad76fee..eef76da4 100644 --- a/sample/Util.Samples.Webs/Properties/launchSettings.json +++ b/sample/Util.Samples.Webs/Properties/launchSettings.json @@ -3,15 +3,15 @@ "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:2264/", - "sslPort": 44365 + "applicationUrl": "http://localhost:5210/", + "sslPort": 44394 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "https://localhost:44365/", + "launchUrl": "https://localhost:44394/", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -25,4 +25,4 @@ "applicationUrl": "http://localhost:2265/" } } -} +} \ No newline at end of file diff --git a/sample/Util.Samples.Webs/Startup.cs b/sample/Util.Samples.Webs/Startup.cs index 2d265903..a9944e91 100644 --- a/sample/Util.Samples.Webs/Startup.cs +++ b/sample/Util.Samples.Webs/Startup.cs @@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Util.Events.Memory; +using Util.Events.Default; using Util.Logs.Extensions; namespace Util.Samples.Webs { @@ -40,7 +40,7 @@ namespace Util.Samples.Webs { // config.ApiKey = "oGBxMBfTQhdRJm1npjGgN1kNJvR6eYSWIpws8pvm"; //} ); - //添加默认事件总线服务 + //添加事件总线服务 services.AddEventBus(); //添加Util基础设施服务 diff --git a/sample/Util.Samples.Webs/Util.Samples.Webs.csproj b/sample/Util.Samples.Webs/Util.Samples.Webs.csproj index 8baafd58..66690933 100644 --- a/sample/Util.Samples.Webs/Util.Samples.Webs.csproj +++ b/sample/Util.Samples.Webs/Util.Samples.Webs.csproj @@ -3,10 +3,10 @@ netcoreapp2.0 aspnet-Util.Samples.Webs-D0551EBC-0589-40AD-ADC2-A88CA4609632 - 何镇汐;dotnetcore - 何镇汐;dotnetcore - Util应用框架以MIT开源发布,可随意使用 - https://github.com/dotnetcore/Util + 何镇汐 + 何镇汐 + + Util应用框架Web示例项目 true Latest @@ -72,8 +72,7 @@ - - + diff --git a/sample/Util.Samples.Webs/Util.Samples.Webs.csproj.user b/sample/Util.Samples.Webs/Util.Samples.Webs.csproj.user index 118f429d..e72f7357 100644 --- a/sample/Util.Samples.Webs/Util.Samples.Webs.csproj.user +++ b/sample/Util.Samples.Webs/Util.Samples.Webs.csproj.user @@ -3,5 +3,9 @@ false 0.0 + IIS Express + + + ProjectDebugger \ No newline at end of file diff --git a/src/Util.Events/Cap/Extensions.Service.cs b/src/Util.Events/Cap/Extensions.Service.cs new file mode 100644 index 00000000..ea66fe9f --- /dev/null +++ b/src/Util.Events/Cap/Extensions.Service.cs @@ -0,0 +1,25 @@ +using System; +using DotNetCore.CAP; +using Microsoft.Extensions.DependencyInjection; +using Util.Events.Default; +using Util.Events.Handlers; +using Util.Events.Messages; + +namespace Util.Events.Cap { + /// + /// 事件总线扩展 + /// + public static class Extensions { + /// + /// 注册事件总线服务 + /// + /// 服务集合 + /// 配置操作 + public static void AddEventBus( this IServiceCollection services,Action action ) { + services.AddCap( action ); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + } + } +} diff --git a/src/Util.Events/Cap/EventBus.cs b/src/Util.Events/Cap/MessageEventBus.cs similarity index 51% rename from src/Util.Events/Cap/EventBus.cs rename to src/Util.Events/Cap/MessageEventBus.cs index 674b0444..1dbb3b5d 100644 --- a/src/Util.Events/Cap/EventBus.cs +++ b/src/Util.Events/Cap/MessageEventBus.cs @@ -1,29 +1,31 @@ -using DotNetCore.CAP; +using System; +using DotNetCore.CAP; +using Util.Events.Messages; namespace Util.Events.Cap { /// - /// Cap事件总线 + /// Cap消息事件总线 /// - public class EventBus : IEventBus { + public class MessageEventBus : IMessageEventBus { /// /// Cap事件发布器 /// private readonly ICapPublisher _publisher; /// - /// 初始化Cap事件总线 + /// 初始化Cap消息事件总线 /// /// Cap事件发布器 - public EventBus( ICapPublisher publisher ) { - _publisher = publisher; + public MessageEventBus( ICapPublisher publisher ) { + _publisher = publisher ?? throw new ArgumentNullException( nameof( publisher ) ); } /// /// 发布事件 /// - /// 事件数据类型 + /// 事件类型 /// 事件 - public void Publish( Event @event ) { + public void Publish( TEvent @event ) where TEvent : IMessageEvent { _publisher.Publish( @event.Target, @event.Data, @event.Callback ); } } diff --git a/src/Util.Events/Default/EventBus.cs b/src/Util.Events/Default/EventBus.cs new file mode 100644 index 00000000..7484070d --- /dev/null +++ b/src/Util.Events/Default/EventBus.cs @@ -0,0 +1,60 @@ +using Util.Events.Handlers; +using Util.Events.Messages; +using Util.Logs.Aspects; + +namespace Util.Events.Default { + /// + /// 事件总线 + /// + public class EventBus : IEventBus { + /// + /// 初始化事件总线 + /// + /// 事件处理器服务 + /// 消息事件总线 + public EventBus( IEventHandlerManager manager, IMessageEventBus messageEventBus = null ) { + Manager = manager; + MessageEventBus = messageEventBus; + } + + /// + /// 事件处理器服务 + /// + public IEventHandlerManager Manager { get; set; } + + /// + /// 消息事件总线 + /// + public IMessageEventBus MessageEventBus { get; set; } + + /// + /// 发布事件 + /// + /// 事件类型 + /// 事件 + [TraceLog] + public void Publish( TEvent @event ) where TEvent : IEvent { + SyncHandle( @event ); + if( @event is IMessageEvent messageEvent ) + AsyncHandle( messageEvent ); + } + + /// + /// 同步处理 - 在当前线程处理 + /// + private void SyncHandle( TEvent @event ) where TEvent : IEvent { + var handlers = Manager.GetHandlers(); + if( handlers == null ) + return; + foreach( var handler in handlers ) + handler.Handle( @event ); + } + + /// + /// 异步处理 - 发送到消息中间件 + /// + private void AsyncHandle( IMessageEvent messageEvent ) { + MessageEventBus?.Publish( messageEvent ); + } + } +} diff --git a/src/Util.Events/Memory/EventHandlerManager.cs b/src/Util.Events/Default/EventHandlerManager.cs similarity index 56% rename from src/Util.Events/Memory/EventHandlerManager.cs rename to src/Util.Events/Default/EventHandlerManager.cs index 42ddcbd1..adcceca6 100644 --- a/src/Util.Events/Memory/EventHandlerManager.cs +++ b/src/Util.Events/Default/EventHandlerManager.cs @@ -2,7 +2,7 @@ using Util.Events.Handlers; using Util.Helpers; -namespace Util.Events.Memory { +namespace Util.Events.Default { /// /// 事件处理器服务 /// @@ -10,9 +10,9 @@ namespace Util.Events.Memory { /// /// 获取事件处理器列表 /// - /// 事件数据类型 - public List> GetHandlers() { - return Ioc.CreateList>(); + /// 事件类型 + public List> GetHandlers() where TEvent : IEvent { + return Ioc.CreateList>(); } } } diff --git a/src/Util.Events/Memory/Extensions.Service.cs b/src/Util.Events/Default/Extensions.Service.cs similarity index 75% rename from src/Util.Events/Memory/Extensions.Service.cs rename to src/Util.Events/Default/Extensions.Service.cs index 1fb1c535..6ef44569 100644 --- a/src/Util.Events/Memory/Extensions.Service.cs +++ b/src/Util.Events/Default/Extensions.Service.cs @@ -1,18 +1,18 @@ using Microsoft.Extensions.DependencyInjection; using Util.Events.Handlers; -namespace Util.Events.Memory { +namespace Util.Events.Default { /// /// 事件总线扩展 /// public static class Extensions { /// - /// 注册事件总线服务 - 内存同步事件 + /// 注册事件总线服务 /// /// 服务集合 public static void AddEventBus( this IServiceCollection services ) { services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(); } } } diff --git a/src/Util.Events/Memory/EventBus.cs b/src/Util.Events/Memory/EventBus.cs deleted file mode 100644 index 74dcb860..00000000 --- a/src/Util.Events/Memory/EventBus.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Util.Events.Handlers; -using Util.Logs.Aspects; - -namespace Util.Events.Memory { - /// - /// 事件总线 - /// - public class EventBus : IEventBus { - /// - /// 初始化事件总线 - /// - /// 事件处理器服务 - public EventBus( IEventHandlerManager manager ) { - Manager = manager; - } - - /// - /// 事件处理器服务 - /// - public IEventHandlerManager Manager { get; set; } - - /// - /// 发布事件 - /// - /// 事件数据类型 - /// 事件 - [TraceLog] - public void Publish( Event @event ) { - var handlers = Manager.GetHandlers(); - if( handlers == null ) - return; - foreach( var handler in handlers ) - handler.Handle( @event ); - } - } -} diff --git a/src/Util/Events/Event.cs b/src/Util/Events/Event.cs index abf49e11..37040a77 100644 --- a/src/Util/Events/Event.cs +++ b/src/Util/Events/Event.cs @@ -5,64 +5,34 @@ namespace Util.Events { /// /// 事件 /// - /// 事件数据类型 - public class Event { + public class Event : IEvent { /// /// 初始化事件 /// - public Event( TData data ) { - Timestamp = DateTime.Now; - Data = data; + public Event() { + Id = Guid.NewGuid().ToString(); + Time = DateTime.Now; } + /// + /// 事件标识 + /// + public string Id { get; set; } + /// /// 事件时间 /// - public DateTime Timestamp { get; } - - /// - /// 事件数据 - /// - public TData Data { get; set; } - - /// - /// 发送目标 - /// - public string Target { get; set; } - - /// - /// 回调 - /// - public string Callback { get; set; } + public DateTime Time { get; } /// /// 输出日志 /// public override string ToString() { StringBuilder result = new StringBuilder(); - result.AppendLine( $"事件时间:{Timestamp.ToMillisecondString()}" ); - if( string.IsNullOrWhiteSpace( Target ) == false ) - result.AppendLine( $"发送目标:{Target}" ); - if( string.IsNullOrWhiteSpace( Callback ) == false ) - result.AppendLine( $"回调:{Callback}" ); - result.Append( $"事件数据:{Util.Helpers.Json.ToJson( Data )}" ); + result.AppendLine( $"事件标识: {Id}" ); + result.AppendLine( $"事件时间: {Time.ToMillisecondString()}" ); + result.Append( $"事件数据:{Util.Helpers.Json.ToJson( this )}" ); return result.ToString(); } } - - /// - /// 事件 - /// - public static class Event { - /// - /// 创建事件 - /// - /// 事件数据类型 - /// 事件数据 - /// 发送目标 - /// 回调 - public static Event Create( TData data, string target = null, string callback = null ) { - return new Event( data ) { Target = target, Callback = callback }; - } - } } diff --git a/src/Util/Events/Handlers/IEventHandler.cs b/src/Util/Events/Handlers/IEventHandler.cs index aea5ac3a..46ca7c0a 100644 --- a/src/Util/Events/Handlers/IEventHandler.cs +++ b/src/Util/Events/Handlers/IEventHandler.cs @@ -8,12 +8,12 @@ /// /// 事件处理器 /// - /// 事件数据类型 - public interface IEventHandler : IEventHandler { + /// 事件类型 + public interface IEventHandler : IEventHandler where TEvent : IEvent { /// /// 处理事件 /// /// 事件 - void Handle( Event @event ); + void Handle( TEvent @event ) ; } } diff --git a/src/Util/Events/Handlers/IEventHandlerManager.cs b/src/Util/Events/Handlers/IEventHandlerManager.cs index 83b31a0f..a9b6fb5e 100644 --- a/src/Util/Events/Handlers/IEventHandlerManager.cs +++ b/src/Util/Events/Handlers/IEventHandlerManager.cs @@ -8,7 +8,7 @@ namespace Util.Events.Handlers { /// /// 获取事件处理器列表 /// - /// 事件数据类型 - List> GetHandlers(); + /// 事件类型 + List> GetHandlers() where TEvent : IEvent; } } diff --git a/src/Util/Events/IEvent.cs b/src/Util/Events/IEvent.cs new file mode 100644 index 00000000..cb7c02a0 --- /dev/null +++ b/src/Util/Events/IEvent.cs @@ -0,0 +1,18 @@ +using System; + +namespace Util.Events { + /// + /// 事件 + /// + public interface IEvent { + /// + /// 事件标识 + /// + string Id { get; set; } + + /// + /// 事件时间 + /// + DateTime Time { get; } + } +} diff --git a/src/Util/Events/IEventBus.cs b/src/Util/Events/IEventBus.cs index 8aa8e69d..b77feee3 100644 --- a/src/Util/Events/IEventBus.cs +++ b/src/Util/Events/IEventBus.cs @@ -6,8 +6,8 @@ /// /// 发布事件 /// - /// 事件数据类型 + /// 事件类型 /// 事件 - void Publish( Event @event ); + void Publish( TEvent @event ) where TEvent : IEvent; } } diff --git a/src/Util/Events/Messages/IMessageEvent.cs b/src/Util/Events/Messages/IMessageEvent.cs new file mode 100644 index 00000000..e6f6d09c --- /dev/null +++ b/src/Util/Events/Messages/IMessageEvent.cs @@ -0,0 +1,21 @@ +namespace Util.Events.Messages { + /// + /// 消息事件 + /// + public interface IMessageEvent : IEvent { + /// + /// 事件数据 + /// + object Data { get; set; } + + /// + /// 发送目标 + /// + string Target { get; set; } + + /// + /// 回调 + /// + string Callback { get; set; } + } +} diff --git a/src/Util/Events/Messages/IMessageEventBus.cs b/src/Util/Events/Messages/IMessageEventBus.cs new file mode 100644 index 00000000..2fda9a41 --- /dev/null +++ b/src/Util/Events/Messages/IMessageEventBus.cs @@ -0,0 +1,13 @@ +namespace Util.Events.Messages { + /// + /// 消息事件总线 + /// + public interface IMessageEventBus { + /// + /// 发布事件 + /// + /// 事件类型 + /// 事件 + void Publish( TEvent @event ) where TEvent : IMessageEvent; + } +} diff --git a/src/Util/Events/Messages/MessageEvent.cs b/src/Util/Events/Messages/MessageEvent.cs new file mode 100644 index 00000000..6087666d --- /dev/null +++ b/src/Util/Events/Messages/MessageEvent.cs @@ -0,0 +1,38 @@ +using System.Text; + +namespace Util.Events.Messages { + /// + /// 消息事件 + /// + public class MessageEvent : Event, IMessageEvent { + /// + /// 事件数据 + /// + public object Data { get; set; } + + /// + /// 发送目标 + /// + public string Target { get; set; } + + /// + /// 回调 + /// + public string Callback { get; set; } + + /// + /// 输出日志 + /// + public override string ToString() { + StringBuilder result = new StringBuilder(); + result.AppendLine( $"事件标识: {Id}" ); + result.AppendLine( $"事件时间:{Time.ToMillisecondString()}" ); + if( string.IsNullOrWhiteSpace( Target ) == false ) + result.AppendLine( $"发送目标:{Target}" ); + if( string.IsNullOrWhiteSpace( Callback ) == false ) + result.AppendLine( $"回调:{Callback}" ); + result.Append( $"事件数据:{Util.Helpers.Json.ToJson( Data )}" ); + return result.ToString(); + } + } +} diff --git a/test/Util.Tests/Events/EventBusTest.cs b/test/Util.Tests/Events/EventBusTest.cs index d9099f19..eec43d1f 100644 --- a/test/Util.Tests/Events/EventBusTest.cs +++ b/test/Util.Tests/Events/EventBusTest.cs @@ -1,7 +1,6 @@ using NSubstitute; -using Util.Events; +using Util.Events.Default; using Util.Events.Handlers; -using Util.Events.Memory; using Util.Tests.Samples; using Xunit; @@ -13,18 +12,18 @@ namespace Util.Tests.Events { /// /// 事件处理器 /// - private readonly IEventHandler _handler; + private readonly IEventHandler _handler; /// /// 事件处理器2 /// - private readonly IEventHandler _handler2; + private readonly IEventHandler _handler2; /// /// 测试初始化 /// public EventBusTest() { - _handler = Substitute.For>(); - _handler2 = Substitute.For>(); + _handler = Substitute.For>(); + _handler2 = Substitute.For>(); } /// @@ -34,7 +33,7 @@ namespace Util.Tests.Events { public void TestPublish() { var manager = new EventHandlerManagerSample( _handler ); var eventBus = new EventBus( manager ); - var @event = Event.Create( new EventDataSample {Name = "a"} ); + var @event = new EventSample { Name = "a" }; eventBus.Publish( @event ); _handler.Received( 1 ).Handle( @event ); } @@ -46,7 +45,7 @@ namespace Util.Tests.Events { public void TestPublish_2() { var manager = new EventHandlerManagerSample( _handler , _handler2 ); var eventBus = new EventBus( manager ); - var @event = Event.Create( new EventDataSample { Name = "a" } ); + var @event = new EventSample { Name = "a" }; eventBus.Publish( @event ); _handler.Received( 1 ).Handle( @event ); _handler2.Received( 1 ).Handle( @event ); diff --git a/test/Util.Tests/Samples/EventSample.cs b/test/Util.Tests/Samples/EventSample.cs index 8712c95f..b1a0c68e 100644 --- a/test/Util.Tests/Samples/EventSample.cs +++ b/test/Util.Tests/Samples/EventSample.cs @@ -5,9 +5,9 @@ using Util.Events.Handlers; namespace Util.Tests.Samples { /// - /// 事件数据样例 + /// 事件样例 /// - public class EventDataSample { + public class EventSample : Event{ /// /// 名称 /// @@ -34,9 +34,9 @@ namespace Util.Tests.Samples { /// /// 获取事件处理器列表 /// - /// 事件数据类型 - public List> GetHandlers() { - return _handlers.Select( t => t as IEventHandler ).ToList(); + /// 事件类型 + public List> GetHandlers() where TEvent : IEvent { + return _handlers.Select( t => t as IEventHandler ).ToList(); } } }