How to get rid of event firing boiler plate code?

喜欢而已 提交于 2019-12-08 12:39:43

问题


I am developing an application which is event driven and runs mulit-threaded and so I have a lot of events getting fired and to fire them "in a save way" I do this the following way:

public static void OnEVENT(EventArgs args)
{
    var theEvent = EVENT;
    if (theEvent != null)
        theEvent(this, args);
}

This pattern is repeated multiple times all over my application.

Is there a way to get rid of the repetion of this pattern and implent this in a generic way?


回答1:


I created a Helper class containing multiple extensions, so I do not need the OnEVENT methods at all and can simply call EVENT.Raise(this, args);:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;

namespace System
{
    /// <summary>Collection of common extensions.</summary>
    public static class EventExtensions
    {
        /// <summary>Raises the specified event.</summary>
        /// <param name="theEvent">The event.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="EventArgs"/> instance containing the event data.</param>
        public static void Raise(this EventHandler theEvent, object sender, EventArgs args = null)
        {
            if (theEvent != null)
                theEvent(sender, args);
        }
        /// <summary>Raises the specified event.</summary>
        /// <typeparam name="T">The type of the event argument.</typeparam>
        /// <param name="theEvent">The event.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The arguments.</param>
        public static void Raise<T>(this EventHandler<T> theEvent, object sender, T args) where T : EventArgs
        {
            if (theEvent != null)
                theEvent(sender, args);
        }
        /// <summary>Raises the specified event.</summary>
        /// <typeparam name="T">The value type contained in the EventArgs.</typeparam>
        /// <param name="theEvent">The event.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The arguments.</param>
        public static void Raise<T>(this EventHandler<EventArgs<T>> theEvent, object sender, T args)
        {
            if (theEvent != null)
                theEvent(sender, new EventArgs<T>(args));
        }
        /// <summary>Raises the specified event.</summary>
        /// <typeparam name="T">The value type contained in the EventArgs.</typeparam>
        /// <param name="theEvent">The event.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="newValue">The new value.</param>
        /// <param name="oldValue">The old value.</param>
        public static void Raise<T>(this EventHandler<ValueChangedEventArgs<T>> theEvent, object sender, T newValue, T oldValue)
        {
            if (theEvent != null)
                theEvent(sender, new ValueChangedEventArgs<T>(newValue, oldValue));
        }

        /// <summary>Raises the specified event for every handler on its own thread.</summary>
        /// <typeparam name="T">The value type contained in the EventArgs.</typeparam>
        /// <param name="theEvent">The event.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The arguments.</param>
        public static void RaiseAsync<T>(this EventHandler<EventArgs<T>> theEvent, object sender, T args)
        {
            if (theEvent != null)
            {
                var eventArgs = new EventArgs<T>(args);
                foreach (EventHandler<EventArgs<T>> action in theEvent.GetInvocationList())
                    action.BeginInvoke(sender, eventArgs, null, null);
            }
        }
    }
}

and the ValueChangedEventArgs class used in there:

/// <summary>EventArgs for notifying about changed values.</summary>
/// <typeparam name="T">The type of the contained value.</typeparam>
public class ValueChangedEventArgs<T> : EventArgs
{
    /// <summary>Initializes a new instance of the <see cref="ValueChangedEventArgs{T}" /> class.</summary>
    /// <param name="newValue">The new value.</param>
    /// <param name="oldValue">The old value.</param>
    public ValueChangedEventArgs(T newValue, T oldValue)
    {
        NewValue = newValue;
        OldValue = oldValue;
    }

    /// <summary>Gets the new value.</summary>
    /// <value>The new value.</value>
    public T NewValue { get; private set; }
    /// <summary>Gets the old value.</summary>
    /// <value>The old value.</value>
    public T OldValue { get; private set; }
}


来源:https://stackoverflow.com/questions/23247384/how-to-get-rid-of-event-firing-boiler-plate-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!