• Prism框架中的事件聚合器EventAggregator(上)


    背景

      从这篇文章开始我们来分析Prism框架中的事件聚合器EventAggregator,在开始这篇文章之前我们需要先申明这篇文章针对的是Prism8.x以及后续版本,应该在版本8以后针对这个事件聚合器做了BreakPoint,所以这里在开篇的时候需要特别的说明,如果需要了解之前的版本请参考之前写过的博客C#基于消息发布订阅模型中的上篇下篇来了解更多背景信息。由于整个过程涉及到很多的内容,这个部分将通过多个篇幅来进行说明,阅读的时候需要注意。

    代码分析

     在进行整个代码分析之前我们来看下整个类图,这个有利于对整个结构有一个更加清晰的认识和理解。在了解完整个基础结构后我们再来就其中非常重要的基础概念及接口进行分析。

    1 EventBase 基类

     这个EventBase是整个PubSubEvent的抽象基类,从类图中我们可以看到这里面包含两个重要的属性SubscriptionsSynchronizationContext,前面一个Subscriptions是一个List,这里就涉及到IEventSubscription这个接口,我们首先来看看这个接口。

    ///<summary>
        /// Defines a contract for an event subscription to be used by <see cref="EventBase"/>.
        ///</summary>
        public interface IEventSubscription
        {
            /// <summary>
            /// Gets or sets a <see cref="SubscriptionToken"/> that identifies this <see cref="IEventSubscription"/>.
            /// </summary>
            /// <value>A token that identifies this <see cref="IEventSubscription"/>.</value>
            SubscriptionToken SubscriptionToken { get; set; }
    
            /// <summary>
            /// Gets the execution strategy to publish this event.
            /// </summary>
            /// <returns>An <see cref="Action{T}"/> with the execution strategy, or <see langword="null" /> if the <see cref="IEventSubscription"/> is no longer valid.</returns>
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
            Action<object[]> GetExecutionStrategy();
        }
    

     这个SubscriptionToken 很好理解用于标识唯一的EventSubscription,这里我们重点来理解GetExecutionStrategy()这个方法,我们来看看这个方法的注释:

    Gets the execution strategy to publish this event.

    顾名思义就是获取当前PubSubEvent最终执行方的执行策略,这里我们来看看其泛型方法的实现。

        /// <summary>
        /// Provides a way to retrieve a <see cref="Delegate"/> to execute an action depending
        /// on the value of a second filter predicate that returns true if the action should execute.
        /// </summary>
        public class EventSubscription : IEventSubscription
        {
            private readonly IDelegateReference _actionReference;
    
            ///<summary>
            /// Creates a new instance of <see cref="EventSubscription"/>.
            ///</summary>
            ///<param name="actionReference">A reference to a delegate of type <see cref="System.Action"/>.</param>
            ///<exception cref="ArgumentNullException">When <paramref name="actionReference"/> or <see paramref="filterReference"/> are <see langword="null" />.</exception>
            ///<exception cref="ArgumentException">When the target of <paramref name="actionReference"/> is not of type <see cref="System.Action"/>.</exception>
            public EventSubscription(IDelegateReference actionReference)
            {
                if (actionReference == null)
                    throw new ArgumentNullException(nameof(actionReference));
                if (!(actionReference.Target is Action))
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidDelegateRerefenceTypeException, typeof(Action).FullName), nameof(actionReference));
    
                _actionReference = actionReference;
            }
    
            /// <summary>
            /// Gets the target <see cref="System.Action"/> that is referenced by the <see cref="IDelegateReference"/>.
            /// </summary>
            /// <value>An <see cref="System.Action"/> or <see langword="null" /> if the referenced target is not alive.</value>
            public Action Action
            {
                get { return (Action)_actionReference.Target; }
            }
    
            /// <summary>
            /// Gets or sets a <see cref="SubscriptionToken"/> that identifies this <see cref="IEventSubscription"/>.
            /// </summary>
            /// <value>A token that identifies this <see cref="IEventSubscription"/>.</value>
            public SubscriptionToken SubscriptionToken { get; set; }
    
            /// <summary>
            /// Gets the execution strategy to publish this event.
            /// </summary>
            /// <returns>An <see cref="System.Action"/> with the execution strategy, or <see langword="null" /> if the <see cref="IEventSubscription"/> is no longer valid.</returns>
            /// <remarks>
            /// If <see cref="Action"/>is no longer valid because it was
            /// garbage collected, this method will return <see langword="null" />.
            /// Otherwise it will return a delegate that evaluates the <see cref="EventSubscription{TPayload}.Filter"/> and if it
            /// returns <see langword="true" /> will then call <see cref="InvokeAction"/>. The returned
            /// delegate holds a hard reference to the <see cref="Action"/> target
            /// <see cref="Delegate">delegates</see>. As long as the returned delegate is not garbage collected,
            /// the <see cref="Action"/> references delegates won't get collected either.
            /// </remarks>
            public virtual Action<object[]> GetExecutionStrategy()
            {
                Action action = this.Action;
                if (action != null)
                {
                    return arguments =>
                    {
                        InvokeAction(action);
                    };
                }
                return null;
            }
    
            /// <summary>
            /// Invokes the specified <see cref="System.Action{TPayload}"/> synchronously when not overridden.
            /// </summary>
            /// <param name="action">The action to execute.</param>
            /// <exception cref="ArgumentNullException">An <see cref="ArgumentNullException"/> is thrown if <paramref name="action"/> is null.</exception>
            public virtual void InvokeAction(Action action)
            {
                if (action == null) throw new ArgumentNullException(nameof(action));
    
                action();
            }
        }
    

     看了这段代码也十分清楚就是通过传入一个Action<object[]>委托,当我们调用GetExecutionStrategy()方法的时候就会获得当前委托,只不过这里外部传入的不是直接的Action<object[]>委托,而是在这个基础上面又重新封装的一个称之为IDelegateReference的接口,这里我们直接来看实现来看看这个这个封装层到底有什么作用。

    /// <summary>
        /// Represents a reference to a <see cref="Delegate"/> that may contain a
        /// <see cref="WeakReference"/> to the target. This class is used
        /// internally by the Prism Library.
        /// </summary>
        public class DelegateReference : IDelegateReference
        {
            private readonly Delegate _delegate;
            private readonly WeakReference _weakReference;
            private readonly MethodInfo _method;
            private readonly Type _delegateType;
    
            /// <summary>
            /// Initializes a new instance of <see cref="DelegateReference"/>.
            /// </summary>
            /// <param name="delegate">The original <see cref="Delegate"/> to create a reference for.</param>
            /// <param name="keepReferenceAlive">If <see langword="false" /> the class will create a weak reference to the delegate, allowing it to be garbage collected. Otherwise it will keep a strong reference to the target.</param>
            /// <exception cref="ArgumentNullException">If the passed <paramref name="delegate"/> is not assignable to <see cref="Delegate"/>.</exception>
            public DelegateReference(Delegate @delegate, bool keepReferenceAlive)
            {
                if (@delegate == null)
                    throw new ArgumentNullException("delegate");
    
                if (keepReferenceAlive)
                {
                    this._delegate = @delegate;
                }
                else
                {
                    _weakReference = new WeakReference(@delegate.Target);
                    _method = @delegate.GetMethodInfo();
                    _delegateType = @delegate.GetType();
                }
            }
    
            /// <summary>
            /// Gets the <see cref="Delegate" /> (the target) referenced by the current <see cref="DelegateReference"/> object.
            /// </summary>
            /// <value><see langword="null"/> if the object referenced by the current <see cref="DelegateReference"/> object has been garbage collected; otherwise, a reference to the <see cref="Delegate"/> referenced by the current <see cref="DelegateReference"/> object.</value>
            public Delegate Target
            {
                get
                {
                    if (_delegate != null)
                    {
                        return _delegate;
                    }
                    else
                    {
                        return TryGetDelegate();
                    }
                }
            }
    
            /// <summary>
            /// Checks if the <see cref="Delegate" /> (the target) referenced by the current <see cref="DelegateReference"/> object are equal to another <see cref="Delegate" />.
            /// This is equivalent with comparing <see cref="Target"/> with <paramref name="delegate"/>, only more efficient.
            /// </summary>
            /// <param name="delegate">The other delegate to compare with.</param>
            /// <returns>True if the target referenced by the current object are equal to <paramref name="delegate"/>.</returns>
            public bool TargetEquals(Delegate @delegate)
            {
                if (_delegate != null)
                {
                    return _delegate == @delegate;
                }
                if (@delegate == null)
                {
                    return !_method.IsStatic && !_weakReference.IsAlive;
                }
                return _weakReference.Target == @delegate.Target && Equals(_method, @delegate.GetMethodInfo());
            }
    
            private Delegate TryGetDelegate()
            {
                if (_method.IsStatic)
                {
                    return _method.CreateDelegate(_delegateType, null);
                }
                object target = _weakReference.Target;
                if (target != null)
                {
                    return _method.CreateDelegate(_delegateType, target);
                }
                return null;
            }
        }
    

     看完了这段代码后我们发现这个没有什么特别的,重点是这个这个DelegateReference在构造函数中传入keepReferenceAlive的bool类型参数,然后通过传入true或者false来决定当前的DelegateReference对其引用的方式是直接引用还是弱引用(WeakReference)其它的没有别的作用,这个也是很好理解的。至此整个EventSubscription的作用和脉络我们都逐渐清晰,这个重要的概念的作用就是将特定PubSubEvent的订阅方具体订阅方法通过一个EventSubcription进行包装,当我们执行PubSubEvent的Publish方法的时候我们能够找到缓存在当前PubSubEvent中的订阅方法并执行这个方法从而达到对当前事件的响应,到了这里思路是不是一下子就开阔起来了。
     分析完这个EventSubscription这个基类,我们再来看看从这个基类派生的BackgroundEventSubscriptionDispatcherEventSubscription这两个子类(包括泛型子类)的作用,这里我们重点分析这几个子类中最关键的部分,通过查看代码后我们发现两个子类都是通过重写基类的虚方法达到定制化目的的。

    /// <summary>
            /// Invokes the specified <see cref="System.Action{TPayload}"/> synchronously when not overridden.
            /// </summary>
            /// <param name="action">The action to execute.</param>
            /// <exception cref="ArgumentNullException">An <see cref="ArgumentNullException"/> is thrown if <paramref name="action"/> is null.</exception>
            public virtual void InvokeAction(Action action)
            {
                if (action == null) throw new ArgumentNullException(nameof(action));
    
                action();
            }
    

     在这个BackgroundEventSubscription中最终执行委托是放在Task中进行的,而在DispatcherEventSubscription中则是在调用方当前的同步上下文中进行的(通过SynchronizationContext定义的Post方法实现),这两个扩展实现了当前PubSubEvent的实际订阅者执行的时候是在新的线程中进行还是和调用方的线程上下文保持一致,到了这里整个EventSubscription的作用全部理解完毕,整个过程中最重要的部分也到此完毕。
     鉴于篇幅的原因,本篇文章就到此结束,下篇我们将讲述从EventBase继承的真正的PubSubEvent的具体实现以及最终的EventAggregator的实现,并最终进行一个完整的总结。

  • 相关阅读:
    关于博客园创始人的心路历程,感触很深!
    Django中的ORM相关操作:F查询,Q查询,事物,ORM执行原生SQL
    在Django中运行脚本文件以及打印出SQL语句。
    Django中的跨表查询,多表查询。
    Django中的ORM介绍,字段以及字段的参数。
    9.2安全的 Web API 与 Web API 2.2 个人帐户
    9.1WebApi的身份验证和授权
    3.1创建项目
    2.4使用属性在 ASP.NET Web API 2 路由创建一个 REST API
    2.3属性在 ASP.NET Web API 2 路由
  • 原文地址:https://www.cnblogs.com/seekdream/p/15491072.html
Copyright © 2020-2023  润新知