deploy-358 - reduce allocations in CancellableEventArgs

This commit is contained in:
Stephan
2017-08-31 17:57:01 +02:00
parent 5ba801386c
commit 0babb7f28a

View File

@@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Security.Permissions;
using Umbraco.Core.Models.PublishedContent;
namespace Umbraco.Core.Events
{
@@ -13,13 +12,15 @@ namespace Umbraco.Core.Events
public class CancellableEventArgs : EventArgs, IEquatable<CancellableEventArgs>
{
private bool _cancel;
private Dictionary<string, object> _eventState;
private static readonly ReadOnlyDictionary<string, object> EmptyAdditionalData = new ReadOnlyDictionary<string, object>(new Dictionary<string, object>());
public CancellableEventArgs(bool canCancel, EventMessages messages, IDictionary<string, object> additionalData)
{
CanCancel = canCancel;
Messages = messages;
AdditionalData = new ReadOnlyDictionary<string, object>(additionalData);
EventState = new Dictionary<string, object>();
}
public CancellableEventArgs(bool canCancel, EventMessages eventMessages)
@@ -27,8 +28,7 @@ namespace Umbraco.Core.Events
if (eventMessages == null) throw new ArgumentNullException("eventMessages");
CanCancel = canCancel;
Messages = eventMessages;
AdditionalData = new ReadOnlyDictionary<string, object>(new Dictionary<string, object>());
EventState = new Dictionary<string, object>();
AdditionalData = EmptyAdditionalData;
}
public CancellableEventArgs(bool canCancel)
@@ -36,19 +36,16 @@ namespace Umbraco.Core.Events
CanCancel = canCancel;
//create a standalone messages
Messages = new EventMessages();
AdditionalData = new ReadOnlyDictionary<string, object>(new Dictionary<string, object>());
EventState = new Dictionary<string, object>();
AdditionalData = EmptyAdditionalData;
}
public CancellableEventArgs(EventMessages eventMessages)
: this(true, eventMessages)
{
}
{ }
public CancellableEventArgs()
: this(true)
{
}
{ }
/// <summary>
/// Flag to determine if this instance will support being cancellable
@@ -98,16 +95,19 @@ namespace Umbraco.Core.Events
/// In some cases raised evens might need to contain additional arbitrary readonly data which can be read by event subscribers
/// </summary>
/// <remarks>
/// This allows for a bit of flexibility in our event raising - it's not pretty but we need to maintain backwards compatibility
/// This allows for a bit of flexibility in our event raising - it's not pretty but we need to maintain backwards compatibility
/// so we cannot change the strongly typed nature for some events.
/// </remarks>
public ReadOnlyDictionary<string, object> AdditionalData { get; private set; }
/// <summary>
/// This can be used by event subscribers to store state in the event args so they easily deal with custom state data between a starting ("ing")
/// This can be used by event subscribers to store state in the event args so they easily deal with custom state data between a starting ("ing")
/// event and an ending ("ed") event
/// </summary>
public IDictionary<string, object> EventState { get; private set; }
/// </summary>
public IDictionary<string, object> EventState
{
get { return _eventState ?? (_eventState = new Dictionary<string, object>()); }
}
public bool Equals(CancellableEventArgs other)
{
@@ -120,13 +120,13 @@ namespace Umbraco.Core.Events
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
if (obj.GetType() != GetType()) return false;
return Equals((CancellableEventArgs) obj);
}
public override int GetHashCode()
{
return (AdditionalData != null ? AdditionalData.GetHashCode() : 0);
return AdditionalData != null ? AdditionalData.GetHashCode() : 0;
}
public static bool operator ==(CancellableEventArgs left, CancellableEventArgs right)
@@ -136,7 +136,7 @@ namespace Umbraco.Core.Events
public static bool operator !=(CancellableEventArgs left, CancellableEventArgs right)
{
return !Equals(left, right);
return Equals(left, right) == false;
}
}
}