Former-commit-id: be7ffaf8720ac4d5a1f68653391d7599276698b3
TangShanKaiPing
wanggang 6 years ago
parent 94d86c8f29
commit 1a7748fdef

@ -0,0 +1,12 @@
namespace Infrastructure.Domain
{
public class EntityDeletedEvent<T>
{
public EntityDeletedEvent(T entity)
{
this.Entity = entity;
}
public T Entity { get; }
}
}

@ -0,0 +1,12 @@
namespace Infrastructure.Domain
{
public class EntityInsertedEvent<T>
{
public EntityInsertedEvent(T entity)
{
this.Entity = entity;
}
public T Entity { get; }
}
}

@ -0,0 +1,12 @@
namespace Infrastructure.Domain
{
public class EntityUpdatedEvent<T>
{
public EntityUpdatedEvent(T entity)
{
Entity = entity;
}
public T Entity { get; }
}
}

@ -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>(T eventMessage)
{
var subscribers = _applicationServices.GetServices<IEvent<T>>().ToList();
subscribers.ForEach(subscriber =>
{
try
{
subscriber.Handle(eventMessage);
}
catch (Exception ex)
{
ex.PrintStack();
}
}
);
}
}
}

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Infrastructure.Domain
{
public interface IEvent<T>
{
void Handle(T eventMessage);
}
}

@ -0,0 +1,7 @@
namespace Infrastructure.Domain
{
public interface IEventPublisher
{
void Publish<T>(T eventMessage);
}
}

@ -26,7 +26,7 @@ namespace IoTNode
public override void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<INodeService, NodeService>();
//services.AddSingleton<INodeService, NodeService>();
services.AddSingleton<ClientService>();
//services.AddSingleton<SerialPortService>();
services.AddSingleton<OnvifService>();

@ -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<Data> _dataRepo;
private readonly IRepository<Category> _categoryRepo;
private readonly IRepository<Product> _productRepo;
private readonly IEventPublisher _eventPublisher;
public HomeController(IConfiguration cfg,
IRepository<Node> nodeRepo,
IRepository<Device> deviceRepo,
IRepository<Data> dataRepo,
IRepository<Category> categoryRepo,
IRepository<Product> productRepo)
IRepository<Product> 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<Node>(new Node()));
return View();
}

@ -0,0 +1,13 @@
using Application.Domain.Entities;
using Infrastructure.Domain;
namespace IoTCenter.Services
{
public class EventHandler : IEvent<EntityInsertedEvent<Node>>
{
public void Handle(EntityInsertedEvent<Node> eventMessage)
{
var entity = eventMessage.Entity;
}
}
}

@ -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<IEmailSender, EmptyEmailSender>();
services.AddTransient<ISmsSender, EmptySmsSender>();
services.AddTransient<IEventPublisher, EventPublisher>();
services.AddSingleton<HealthCheckService>();
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);
}

@ -23,7 +23,7 @@ http {
keepalive_timeout 65;
server {
listen 80;
listen 192.168.3.124:80;
server_name localhost;
location ^~ /live/ {

Loading…
Cancel
Save