Lind.DDD.Messaging框架通讯组件介绍

简介:

大 家好,今天有时间来介绍一下Lind.DDD框架里的消息机制,消息发送这块一般的实现方法是将Email,SMS等集成到一个公用类库里,而本身 Email和SMS没什么关系,它们也不会有什么接口约定,即你想实现某种消息的多态发送,不需要程序代码,基本不可能实现,而在Lind.DDD里面, 大叔将它进行了抽象,消息有自己的统一接口,而对于email和sms只是一种实现而以,这样,就可以发挥面向对象的特性,在sms,email甚至是 rtx上进行消息的灵活切换了,说到这样,您心动了吧!

Lind.DDD.Messaging框架图

接口规范

    /// <summary>
    /// Message Interface
    /// Author:Garrett /// </summary> public interface IMessageManager { /// <summary> /// Sends a message to a channel using a content item as the recipient /// </summary> /// <param name="recipient">接收者</param> /// <param name="subject">主题</param> /// <param name="body">消息主体</param> /// <param name="serverVirtualPath">本参数可以没有,服务端模块级路径,只在xmpp中有意义</param> void Send(string recipient, string subject, string body, string serverVirtualPath = null); /// <summary> /// Sends a message to a channel using a set of content items as the recipients /// </summary> /// <param name="recipients">A set of content items to send the message to. Only one message may be sent if the channel manages it.</param> /// <param name="type">A custom string specifying what type of message is sent. Used in even handlers to define the message.</param> /// <param name="service">The name of the channel to use, e.g. "email"</param> /// <param name="properties">A set of specific properties for the channel.</param> void Send(IEnumerable<string> recipients, string subject, string body, string serverVirtualPath = null); /// <summary> /// Async Sends a message to a channel using a set of content items as the recipients /// </summary> /// <param name="recipients">A set of content items to send the message to. Only one message may be sent if the channel manages it.</param> /// <param name="type">A custom string specifying what type of message is sent. Used in even handlers to define the message.</param> /// <param name="service">The name of the channel to use, e.g. "email"</param> /// <param name="properties">A set of specific properties for the channel.</param> /// <param name="isAsync">is Async</param> void Send(IEnumerable<string> recipients, string subject, string body, bool isAsync, string serverVirtualPath = null); }

从接口定义上,我们看到了异步的影子,大叔把异步这块写在了参数上,当然,一般情况来说,消息应该都是异步的。

消息上下文

 /// <summary>
    /// 消息实体
    /// </summary> public class MessageContext { /// <summary> /// 消息类型 /// </summary> public MessageType Type { get; set; } /// <summary> /// 消息头 /// </summary> public string Subject { get; set; } /// <summary> /// 消息正文 /// </summary> public string Body { get; set; } /// <summary> /// 接受方地址列表 /// </summary> public IEnumerable<string> Addresses { get; set; } /// <summary> /// 是否处于准备发送状态 /// </summary> public bool MessagePrepared { get; set; } public MessageContext() { Addresses = Enumerable.Empty<string>();//这时Addresses!=null,使用Addresses.ToList().ForEach(i => Console.WriteLine(i));不会引发异常  } }

消息上下文就是消息的对象,类型于EF里的DataContext数据上下文或者HttpContext上下文,都是指实现某些功能的数据对象

消息生产者

/// <summary>
    /// 消息生产者
    /// 具体消息生产者是单例,如Email,SMS,Rtx等 /// </summary> public sealed class MessageFactory { /// <summary> /// 消息工厂 /// </summary> public static IMessageManager GetService(MessageType messageType) { switch (messageType) { case MessageType.Email: return EmailMessageManager.Instance; case MessageType.SMS: return SMSMessageManager.Instance; case MessageType.RTX: return RTXMessageManager.Instance; case MessageType.XMPP: return XMPPMessageManager.Instance; default: throw new NotImplementedException("消息生产者未被识别..."); } } }

从生产者代码上可以看出,在一个领域项目里,你可以通过GetService来使用不同的消息,这是对象的,这前大叔的设计存在一些局限性,一个项目只能用一种消息机制,这对于项目来说,是不能满足了,所以,大叔在Lind.DDD框架里对它进行了改善!

Email实现者

    /// <summary>
    ///Email消息服务
    /// </summary> internal class EmailMessageManager : IMessageManager { #region Singleton private static object lockObj = new object(); public static EmailMessageManager Instance; static string email_Address = ConfigurationManager.AppSettings["Email_Address"]; static string email_DisplayName = ConfigurationManager.AppSettings["Email_DisplayName"]; static string email_Host = ConfigurationManager.AppSettings["Email_Host"]; static string email_Password = ConfigurationManager.AppSettings["Email_Password"]; static int email_Port = Convert.ToInt32(ConfigurationManager.AppSettings["Email_Port"] ?? "21"); static string email_UserName = ConfigurationManager.AppSettings["Email_UserName"]; static EmailMessageManager() { lock (lockObj) { if (Instance == null) Instance = new EmailMessageManager(); } } private EmailMessageManager() { } #endregion #region IMessageManager 成员 public void Send(string recipient, string subject, string body, string serverVirtualPath = null) { Send(new List<string> { recipient }, subject, body); } public void Send(IEnumerable<string> recipients, string subject, string body, string serverVirtualPath = null) { Send(recipients, subject, body, false); } public void Send(IEnumerable<string> recipients, string subject, string body, bool isAsync, string serverVirtualPath = null) { try { if (recipients != null && recipients.Any()) { using (SmtpClient client = new SmtpClient() { Host = email_Host, Port = email_Port, Credentials = new NetworkCredential(email_UserName, email_Password), EnableSsl = false,//设置为true会出现"服务器不支持安全连接的错误" DeliveryMethod = SmtpDeliveryMethod.Network, }) { #region Send Message var mail = new MailMessage { From = new MailAddress(email_Address, email_DisplayName), Subject = subject, Body = body, IsBodyHtml = true, }; MailAddressCollection mailAddressCollection = new MailAddressCollection(); recipients.ToList().ForEach(i => { //email有效性验证 if (new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$").IsMatch(i)) mail.To.Add(i); }); if (isAsync) { client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted); client.SendAsync(mail, recipients); } else { client.Send(mail); } #endregion } } } catch (Exception ex) { LoggerFactory.Instance.Logger_Info(ex.Message); } } void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { string arr = null; (e.UserState as List<string>).ToList().ForEach(i => { arr += i; }); //发送完成后要做的事件,可能是写日志  } #endregion }

通过代码可以看它自已是个internal的,即对外不公开,对外只能通过消息生成者进行访问,它与SMS,RTX形成了一种策略模式的概念。

SMS实现者,请看Lind.DDD源码

RTX实现者,请看Lind.DDD源码

XMPP实现者,请看Lind.DDD源码

本文转自博客园张占岭(仓储大叔)的博客,原文链接:Lind.DDD.Messaging框架通讯组件介绍,如需转载请自行联系原博主。

目录
相关文章
|
Java Go Docker
docker 安装flowable/flowable-ui
docker 安装flowable/flowable-ui
1929 0
|
12月前
|
NoSQL Linux 调度
C 语言高效学习:从入门到嵌入式的科学路径
C语言是底层与嵌入式开发的基石。科学学习路径分四阶:入门打基础,深化攻指针内存,实战练项目,进阶接硬件。掌握GCC、GDB、Makefile,拓展至STM32、FreeRTOS与SQLite,实现从语法到工程、从初级到资深的跃迁,筑牢“软件-硬件”桥梁。
599 0
|
12月前
|
新零售 人工智能 算法
咖啡机器人技术深度解析与主流商业解决方案评测
咖啡机器人正从营销工具蜕变为商业基础设施,广泛应用于高端商务、交通枢纽等场景。依托高精度控制、视觉感知与人机协作技术,实现24小时稳定出品。猎户星空智咖大师以AI算法与仿生设计平衡性能与成本,ABB YuMi展现极致工艺,越疆、节卡、川崎则各具开放性、灵活性与稳定性优势,推动服务机器人迈向智能化新阶段。(238字)
|
XML 数据可视化 Java
【数字IC手撕代码】Verilog小数分频|题目|原理|设计|仿真
【数字IC手撕代码】Verilog小数分频|题目|原理|设计|仿真
【数字IC手撕代码】Verilog小数分频|题目|原理|设计|仿真
|
机器学习/深度学习 数据采集 数据挖掘
特征工程在营销组合建模中的应用:基于因果推断的机器学习方法优化渠道效应估计
因果推断方法为特征工程提供了一个更深层次的框架,使我们能够区分真正的因果关系和简单的统计相关性。这种方法在需要理解干预效果的领域尤为重要,如经济学、医学和市场营销。
1017 1
特征工程在营销组合建模中的应用:基于因果推断的机器学习方法优化渠道效应估计
|
图形学
【unity实战】实现蓄力丢手榴弹、烟雾弹、燃烧弹的效果
【unity实战】实现蓄力丢手榴弹、烟雾弹、燃烧弹的效果
781 0
|
前端开发 数据库 JavaScript
基于Flowable的流程挂接自定义业务表单的设计与实践
文章讨论了如何在Flowable流程引擎中挂接自定义业务表单,以及相关设计和实践的步骤。文章中包含了一些前后端代码示例,如Vue组件的模板和脚本部分,这些代码用于实现与Flowable流程引擎交互的界面。例如,有一个按钮组件用于提交申请,点击后会触发applySubmit方法,该方法会与后端API进行交互,处理流程启动、查询关联流程等逻辑。
55388 11
|
SQL 存储 关系型数据库
C#一分钟浅谈:使用 ADO.NET 进行数据库访问
【9月更文挑战第3天】在.NET开发中,与数据库交互至关重要。ADO.NET是Microsoft提供的用于访问关系型数据库的类库,包含连接数据库、执行SQL命令等功能。本文从基础入手,介绍如何使用ADO.NET进行数据库访问,并提供示例代码,同时讨论常见问题及其解决方案,如连接字符串错误、SQL注入风险和资源泄露等,帮助开发者更好地利用ADO.NET提升应用的安全性和稳定性。
1092 6
|
数据库 测试技术 Java
阿里技术专家详解DDD系列 第二弹 - 应用架构
应用架构,指软件系统中固定不变的代码结构、设计模式、规范和组件间的通信方式。在应用开发中架构之所以是最重要的第一步,因为一个好的架构能让系统安全、稳定、快速迭代。但是今天我们在做业务研发时,更多会关注一些宏观的架构,而忽略了应用内部的架构设计,希望能通过案例分析和重构,推演出一套高质量的DDD架构。
61203 25
阿里技术专家详解DDD系列 第二弹 - 应用架构