From 1a7748fdefb26bf00f59cf7706ca139fcee21ec5 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Fri, 13 Sep 2019 15:17:35 +0800 Subject: [PATCH] update Former-commit-id: be7ffaf8720ac4d5a1f68653391d7599276698b3 --- .../Domain/EntityDeletedEvent.cs | 12 +++++++ .../Domain/EntityInsertedEvent.cs | 12 +++++++ .../Domain/EntityUpdatedEvent.cs | 12 +++++++ .../Infrastructure/Domain/EventPublisher.cs | 34 +++++++++++++++++++ projects/Infrastructure/Domain/IEvent.cs | 11 ++++++ .../Infrastructure/Domain/IEventPublisher.cs | 7 ++++ projects/IoT/IoTNode/Startup.cs | 2 +- .../IoTCenter/Controllers/HomeController.cs | 7 +++- projects/IoTCenter/Services/EventHandler.cs | 13 +++++++ projects/IoTCenter/Startup.cs | 12 +++++++ tools/nginx-1.16.0/conf/nginx.conf | 2 +- 11 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 projects/Infrastructure/Domain/EntityDeletedEvent.cs create mode 100644 projects/Infrastructure/Domain/EntityInsertedEvent.cs create mode 100644 projects/Infrastructure/Domain/EntityUpdatedEvent.cs create mode 100644 projects/Infrastructure/Domain/EventPublisher.cs create mode 100644 projects/Infrastructure/Domain/IEvent.cs create mode 100644 projects/Infrastructure/Domain/IEventPublisher.cs create mode 100644 projects/IoTCenter/Services/EventHandler.cs diff --git a/projects/Infrastructure/Domain/EntityDeletedEvent.cs b/projects/Infrastructure/Domain/EntityDeletedEvent.cs new file mode 100644 index 00000000..90bad1bd --- /dev/null +++ b/projects/Infrastructure/Domain/EntityDeletedEvent.cs @@ -0,0 +1,12 @@ +namespace Infrastructure.Domain +{ + public class EntityDeletedEvent + { + public EntityDeletedEvent(T entity) + { + this.Entity = entity; + } + + public T Entity { get; } + } +} \ No newline at end of file diff --git a/projects/Infrastructure/Domain/EntityInsertedEvent.cs b/projects/Infrastructure/Domain/EntityInsertedEvent.cs new file mode 100644 index 00000000..df18f1a0 --- /dev/null +++ b/projects/Infrastructure/Domain/EntityInsertedEvent.cs @@ -0,0 +1,12 @@ +namespace Infrastructure.Domain +{ + public class EntityInsertedEvent + { + public EntityInsertedEvent(T entity) + { + this.Entity = entity; + } + + public T Entity { get; } + } +} \ No newline at end of file diff --git a/projects/Infrastructure/Domain/EntityUpdatedEvent.cs b/projects/Infrastructure/Domain/EntityUpdatedEvent.cs new file mode 100644 index 00000000..2fecad38 --- /dev/null +++ b/projects/Infrastructure/Domain/EntityUpdatedEvent.cs @@ -0,0 +1,12 @@ +namespace Infrastructure.Domain +{ + public class EntityUpdatedEvent + { + public EntityUpdatedEvent(T entity) + { + Entity = entity; + } + + public T Entity { get; } + } +} \ No newline at end of file diff --git a/projects/Infrastructure/Domain/EventPublisher.cs b/projects/Infrastructure/Domain/EventPublisher.cs new file mode 100644 index 00000000..2e4d43c5 --- /dev/null +++ b/projects/Infrastructure/Domain/EventPublisher.cs @@ -0,0 +1,34 @@ +using Infrastructure.Extensions; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Linq; + +namespace Infrastructure.Domain +{ + public class EventPublisher : IEventPublisher + { + private readonly IServiceProvider _applicationServices; + + public EventPublisher(IServiceProvider applicationServices) + { + this._applicationServices = applicationServices; + } + + public virtual void Publish(T eventMessage) + { + var subscribers = _applicationServices.GetServices>().ToList(); + subscribers.ForEach(subscriber => + { + try + { + subscriber.Handle(eventMessage); + } + catch (Exception ex) + { + ex.PrintStack(); + } + } + ); + } + } +} \ No newline at end of file diff --git a/projects/Infrastructure/Domain/IEvent.cs b/projects/Infrastructure/Domain/IEvent.cs new file mode 100644 index 00000000..1f518a35 --- /dev/null +++ b/projects/Infrastructure/Domain/IEvent.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Infrastructure.Domain +{ + public interface IEvent + { + void Handle(T eventMessage); + } +} \ No newline at end of file diff --git a/projects/Infrastructure/Domain/IEventPublisher.cs b/projects/Infrastructure/Domain/IEventPublisher.cs new file mode 100644 index 00000000..08076b9f --- /dev/null +++ b/projects/Infrastructure/Domain/IEventPublisher.cs @@ -0,0 +1,7 @@ +namespace Infrastructure.Domain +{ + public interface IEventPublisher + { + void Publish(T eventMessage); + } +} \ No newline at end of file diff --git a/projects/IoT/IoTNode/Startup.cs b/projects/IoT/IoTNode/Startup.cs index da34a634..5d2461a8 100644 --- a/projects/IoT/IoTNode/Startup.cs +++ b/projects/IoT/IoTNode/Startup.cs @@ -26,7 +26,7 @@ namespace IoTNode public override void ConfigureServices(IServiceCollection services) { - services.AddSingleton(); + //services.AddSingleton(); services.AddSingleton(); //services.AddSingleton(); services.AddSingleton(); diff --git a/projects/IoTCenter/Controllers/HomeController.cs b/projects/IoTCenter/Controllers/HomeController.cs index ecc7ba09..48d3a56a 100644 --- a/projects/IoTCenter/Controllers/HomeController.cs +++ b/projects/IoTCenter/Controllers/HomeController.cs @@ -1,5 +1,6 @@ using Application.Domain.Entities; using Infrastructure.Data; +using Infrastructure.Domain; using Infrastructure.Extensions; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -22,13 +23,15 @@ namespace IoTCenter.Controllers private readonly IRepository _dataRepo; private readonly IRepository _categoryRepo; private readonly IRepository _productRepo; + private readonly IEventPublisher _eventPublisher; public HomeController(IConfiguration cfg, IRepository nodeRepo, IRepository deviceRepo, IRepository dataRepo, IRepository categoryRepo, - IRepository productRepo) + IRepository productRepo, + IEventPublisher eventPublisher) { this._cfg = cfg; this._nodeRepo = nodeRepo; @@ -36,11 +39,13 @@ namespace IoTCenter.Controllers this._dataRepo = dataRepo; this._categoryRepo = categoryRepo; this._productRepo = productRepo; + this._eventPublisher = eventPublisher; } [Authorize] public IActionResult Index() { + this._eventPublisher.Publish(new EntityInsertedEvent(new Node())); return View(); } diff --git a/projects/IoTCenter/Services/EventHandler.cs b/projects/IoTCenter/Services/EventHandler.cs new file mode 100644 index 00000000..1795221e --- /dev/null +++ b/projects/IoTCenter/Services/EventHandler.cs @@ -0,0 +1,13 @@ +using Application.Domain.Entities; +using Infrastructure.Domain; + +namespace IoTCenter.Services +{ + public class EventHandler : IEvent> + { + public void Handle(EntityInsertedEvent eventMessage) + { + var entity = eventMessage.Entity; + } + } +} \ No newline at end of file diff --git a/projects/IoTCenter/Startup.cs b/projects/IoTCenter/Startup.cs index 5906645b..7c6aa6e7 100644 --- a/projects/IoTCenter/Startup.cs +++ b/projects/IoTCenter/Startup.cs @@ -1,4 +1,5 @@ using Application.Domain.Entities; +using Infrastructure.Domain; using Infrastructure.Email; using Infrastructure.Sms; using Infrastructure.UI; @@ -12,6 +13,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; +using System.Linq; +using System.Reflection; using System.Threading.Tasks; namespace IoTCenter @@ -26,7 +29,16 @@ namespace IoTCenter { services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddSingleton(); + Assembly.GetExecutingAssembly() + .GetTypes() + .Where(t => t.GetInterfaces().Any(o => o.IsGenericType && o.GetGenericTypeDefinition() == typeof(IEvent<>))) + .ToList() + .ForEach(t => + { + services.AddTransient(t.GetInterfaces().Where(o => o.IsGenericType && o.GetGenericTypeDefinition() == typeof(IEvent<>)).First(), t); + }); base.ConfigureServices(services); } diff --git a/tools/nginx-1.16.0/conf/nginx.conf b/tools/nginx-1.16.0/conf/nginx.conf index e036c8ed..529d2a31 100644 --- a/tools/nginx-1.16.0/conf/nginx.conf +++ b/tools/nginx-1.16.0/conf/nginx.conf @@ -23,7 +23,7 @@ http { keepalive_timeout 65; server { - listen 80; + listen 192.168.3.124:80; server_name localhost; location ^~ /live/ {