前言
我想做个简单的消息发布订阅功能,但是发现好像没有现成的工具类。要么就是Mqtt这种消息订阅发布。但是我只想程序内部进行消息订阅发布,进行程序的解耦。那没办法了,只能自己上了
模拟消息订阅发布
在Utils 的MessageHelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NetCore.Utils
{
public class MessageHelper
{
public static List<Message> Messages = new List<Message>();
/// <summary>
/// 消息订阅
/// </summary>
public static void Subscribe(string topic, string key, Action<object> action)
{
var model = Messages.Where(item => item.Topic == topic && item.Key == key).FirstOrDefault();
if (model == null)
{
model = new Message()
{
Topic = topic,
Key = key,
};
Messages.Add(model);
model.Actions.Add(action);
}
else if(model.Actions.Find(item => item.Equals(action)) == null)
{
model.Actions.Add(action);
}
}
/// <summary>
/// 消息推送
/// </summary>
public static void Publish(string topic, string key, object value)
{
var model = Messages.Where(item => item.Topic == topic && item.Key == key).FirstOrDefault();
if (model != null)
{
model.Actions.ForEach(item =>
{
item(value);
});
}
}
}
public class Message
{
public string Topic { get; set; }
public string Key { get; set; }
public List<Action<object>> Actions { get; set; } = new List<Action<object>>();
}
}
使用
static void Main(string[] args)
{
MessageHelper.Subscribe("Topic1", "key1", (res) =>
{
var _res = ((string Name, int Age))res;
Console.WriteLine(_res.ToString());
Console.WriteLine("我被调用了1");
});
MessageHelper.Subscribe("Topic2", "key1", (res) =>
{
Console.WriteLine("我被调用了2");
});
MessageHelper.Subscribe("Topic3", "key1", (res) =>
{
Console.WriteLine("我被调用了3");
});
MessageHelper.Subscribe("Topic4", "key1", (res) =>
{
Console.WriteLine("我被调用了4");
});
MessageHelper.Publish("Topic1","key1",(Name:"嘟嘟",Sex:"12"));
Console.ReadLine();
}
注意事项
这里我用了元祖来进行临时变量的传值。元祖如何使用请看我的另一篇文章。元祖用来做临时变量特别好用
C# 元祖,最佳的临时变量。
为了防止重复注入,我加了判断代码。文章来源:https://www.toymoban.com/news/detail-632707.html
更新 2023年8月11日,添加泛型,简化Topic
public class MessageHelper
{
public static List<Message> Messages = new List<Message>();
/// <summary>
/// 消息订阅
/// </summary>
public static void Subscribe<T_class>( string key, Action<object> action)where T_class : class
{
string topic = typeof(T_class).FullName;
var model = Messages.Where(item => item.Topic == topic && item.Key == key).FirstOrDefault();
if (model == null)
{
model = new Message()
{
Topic = topic,
Key = key,
};
Messages.Add(model);
model.Actions.Add(action);
}
else if (model.Actions.Find(item => item.Equals(action)) == null)
{
model.Actions.Add(action);
}
}
/// <summary>
/// 消息推送
/// </summary>
public static void Publish<T_class>( string key, object value) where T_class : class
{
string topic = typeof(T_class).FullName;
var model = Messages.Where(item => item.Topic == topic && item.Key == key).FirstOrDefault();
if (model != null)
{
model.Actions.ForEach(item =>
{
item(value);
});
}
}
}
public class Message
{
public string Topic { get; set; }
public string Key { get; set; }
public List<Action<object>> Actions { get; set; } = new List<Action<object>>();
}
public static void TestBtn(object value)
{
Console.WriteLine((string)value);
}
static void Main(string[] args)
{
MessageHelper.Subscribe<Program>("Test", TestBtn);
MessageHelper.Subscribe<Program>("Test", TestBtn);
MessageHelper.Subscribe<Program>("Test", TestBtn);
//订阅我做了去重处理
MessageHelper.Publish<Program>("Test","测试文本");
Console.ReadLine();
}
文章来源地址https://www.toymoban.com/news/detail-632707.html
到了这里,关于C# 简单模拟 程序内部 消息订阅发布功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!