最近写了一个Windows Phone微博 客户端。 自己写了一个 MVVM 现在把它开源出来。http://reactivemvvm.codeplex.com/

最近写了一个Windows Phone微博 客户端。 自己写了一个 MVVM 现在把它开源出来。

http://reactivemvvm.codeplex.com/ 此MVVM 使用Rx framework 实现的, 能轻松的实现多线程编程。

能够轻松灵活的扩展。 自我感觉比现有的好使。

给大家看看用响应式框架 实现的消息机制:

复制代码
using System;
using System.Reactive.Subjects;
using System.Reactive.Linq;
using System.Reactive.Concurrency;
using System.Collections.Generic;
using ReactiveMVVM.Logging;

namespace ReactiveMVVM.Messaging
{
///<summary>
/// The reactive messager.
/// Creator: Houjun Zhou
///</summary>
public class Messenger : IMessenger
{
private readonly Subject<object> _subject = new Subject<object>();
private static readonly Messenger _default = new Messenger();

Dictionary<object, List<IDisposable>> disTypeDic = new Dictionary<object, List<IDisposable>>();

///<summary>
/// the default messager.
///</summary>
public static Messenger Default
{
get { return _default; }
}

public IDisposable Subscribe(IObserver<object> observer)
{
return _subject.Subscribe(observer);
}

///<summary>
/// register the message ation to do.
///</summary>
///<param name="action">ation do with recipient</param>
///<param name="scheduler">set the one scheduler</param>
///<param name="predicate">when the recipient match some condition, can do the action</param>
public void Register<T>(Type registorType, Action<T> action, Func<T, bool> predicate = null, IScheduler scheduler = null) where T : IMessage
{
IDisposable dis = null;
if (predicate == null)
{
if (scheduler == null)
dis = this.OfType<T>().Where(a => a.Sender.GetType() == registorType).Subscribe(action);
else
dis = this.OfType<T>().Where(a => a.Sender.GetType() == registorType).ObserveOn(scheduler).Subscribe(action);
}
else
{
if (scheduler == null)
dis = this.OfType<T>().Where(a => a.Sender.GetType() == registorType).Where(predicate).Subscribe(action);
else
dis = this.OfType<T>().Where(a => a.Sender.GetType() == registorType).Where(predicate).ObserveOn(scheduler).Subscribe(action);
}

if (disTypeDic.ContainsKey(registorType))
{
var lst = disTypeDic[registorType];
lst.Add(dis);
disTypeDic[registorType] = lst;
}
else
{
disTypeDic.Add(registorType, new List<IDisposable> { dis });
}
}

///<summary>
/// register the message ation to do.
///</summary>
///<param name="registor"></param>
///<param name="action">ation do with recipient</param>
///<param name="scheduler">set the one scheduler</param>
///<param name="predicate">when the recipient match some condition, can do the action</param>
public void Register<T>(object registor, Action<T> action, Func<T, bool> predicate = null, IScheduler scheduler = null) where T: IMessage
{
IDisposable dis = null;
if (predicate == null)
{
if (scheduler == null)
dis = this.OfType<T>().Where(a => a.Sender == registor).Subscribe(action);
else
dis = this.OfType<T>().Where(a => a.Sender == registor).ObserveOn(scheduler).Subscribe(action);
}
else
{
if (scheduler == null)
dis = this.OfType<T>().Where(a => a.Sender == registor).Where(predicate).Subscribe(action);
else
dis = this.OfType<T>().Where(a => a.Sender == registor).Where(predicate).ObserveOn(scheduler).Subscribe(action);
}

if (disTypeDic.ContainsKey(registor))
{
var lst = disTypeDic[registor];
lst.Add(dis);
disTypeDic[registor] = lst;
}
else
{
disTypeDic.Add(registor, new List<IDisposable> { dis });
}
}

///<summary>
/// register the message ation to do.
///</summary>
///<param name="name">the name of the message.</param>
///<param name="action">ation do with recipient</param>
///<param name="scheduler">set the one scheduler</param>
///<param name="predicate">when the recipient match some condition, can do the action</param>
public void Register<T>(string name, Action<T> action, Func<T, bool> predicate = null, IScheduler scheduler = null) where T : ICertainMessage
{
IDisposable dis = null;
if (predicate == null)
{
if (scheduler == null)
dis = this.OfType<T>().Where(a => a.Name == name).Subscribe(action);
else
dis = this.OfType<T>().Where(a => a.Name == name).ObserveOn(scheduler).Subscribe(action);
}
else
{
if (scheduler == null)
dis = this.OfType<T>().Where(a => a.Name == name).Where(predicate).Subscribe(action);
else
dis = this.OfType<T>().Where(a => a.Name == name).Where(predicate).ObserveOn(scheduler).Subscribe(action);
}

if (disTypeDic.ContainsKey(name))
{
var lst = disTypeDic[name];
lst.Add(dis);
disTypeDic[name] = lst;
}
else
{
disTypeDic.Add(name, new List<IDisposable> { dis });
}
}

///<summary>
/// clear the action have registed.
///</summary>
public void Unregister(Type type)
{
if (disTypeDic.ContainsKey(type))
{
var list = disTypeDic[type];
disTypeDic.Remove(type);
foreach(var item in list)
item.Dispose();
}
}

///<summary>
/// clear the action have registed.
///</summary>
public void Unregister(object registor)
{
if (disTypeDic.ContainsKey(registor))
{
var list = disTypeDic[registor];
disTypeDic.Remove(registor);
foreach (var item in list)
item.Dispose();
}
}

///<summary>
/// clear the action have registed.
///</summary>
///<param name="name">The name of the message.</param>
public void Unregister(string name)
{
if (disTypeDic.ContainsKey(name))
{
var list = disTypeDic[name];
disTypeDic.Remove(name);
foreach (var item in list)
item.Dispose();
}
}

///<summary>
/// send the message.
///</summary>
///<typeparam name="T">type of the recipient.</typeparam>
///<param name="message">the message to send.</param>
public void Send(IMessage message)
{
if (message == null) return;
try
{
_subject.OnNext(message);
}
catch (Exception e)
{
this.Log().Error(e);
_subject.OnError(e);
}
}


}
}
复制代码

 

posted @   木吉他-.-  阅读(1794)  评论(1编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示