思考一种好的架构(十一)

 

事件总线的使用

短信服务(SMS Server)

微信公众号(WeChat Server)

事件回溯服务(TracingSource)

 

短信服务:

 

 

 public interface ISMSOperation
    {
        void SendSMS(string message);
    }
 public class SMSOperation : ISMSOperation
    {
        public void SendSMS(string message)
        {
            Console.WriteLine($"发送短信:{message}");
        }
    }

上面就是短信自己的业务服务

它并没有引用中介者,也没把接口放在中介者,因为它有更加偏向于普通服务,虽然到最后可能也会做短信消息持久化,不过那个是NoSql的保存而不是主要业务库

 

 public class StartExecute : BaseStartExecute
    {
        public override void Execute()
        {
            EventBusPost.CreateEvent(typeof(Startup));
        }
    }
 public static class Startup
    {
        public static void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<ISMSOperation, SMSOperation>();

        }

        public static void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
         
        }
    }

 

StartExecute 留在后续再说明
这里的代码表示服务最开始启动时候就通过注入的方式通知EventBus扫描这个程序集,以便发现事件处理对象

最后来到事件(消息)处理

  class OrderGenerationEventHandler:INotificationHandler<OrderGenerationEvent>
    {
        ISMSOperation operation;
        public OrderGenerationEventHandler(ISMSOperation operation) {
            this.operation = operation;
        }
        public Task Handle(OrderGenerationEvent notification, CancellationToken cancellationToken)
        {
            operation.SendSMS($"恭喜你下单成功,下单时间:{notification.GenerationTimer},数量{notification.Number}");
            return Task.CompletedTask;
        }
    }

 

非常简单,监听订单生成事件,((OrderGenerationEvent)订单生成事件是在EventBus下定义的)

然后获取ISMSOperation 功能,Handle处理时发送短信,简单明了

 

微信公众号服务

 

 

 

 

 

 

现在很简单,就一个模拟发送消息的功能

它可能会需要引用到中介者服务中,对微信公众号的接入可能会导致它的服务被大量调用,但是它依旧属于普通服务,后续它可能会变得异常庞大,那那时就需要小心了,拆分的时候一定要保证服务最基本的原则

 public interface IWeChatNoticeOperation
    {
        void SendSMS(string message);
    }
 public void SendSMS(string message)
        {
            Console.WriteLine($"发送微信公众号消息:{message}");
        }

 

 public class StartExecute : BaseStartExecute
    {
        public override void Execute()
        {
            EventBusPost.CreateEvent(typeof(Startup));
        }
    }
public static class Startup
    {
        public static void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<IWeChatNoticeOperation, WeChatNoticeOperation>();
        }

        public static void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
         
        }
    }

 

class OrderGenerationEventHandler:INotificationHandler<OrderGenerationEvent>
    {
        IWeChatNoticeOperation operation;
        public OrderGenerationEventHandler(IWeChatNoticeOperation operation) {
            this.operation = operation;
        }
        public Task Handle(OrderGenerationEvent notification, CancellationToken cancellationToken)
        {
            operation.SendSMS($"恭喜你下单成功,下单时间:{notification.GenerationTimer},数量{notification.Number}");
            return Task.CompletedTask;
        }
    }

 

很简单,就是上面那一套的翻版

 

这节说了下事件总线的应用,下一节也附带了使用事件总线

 

posted @ 2020-04-01 14:27  AnAng  阅读(171)  评论(0编辑  收藏  举报