NotificationData.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using Abp.Collections.Extensions;
  4. using Abp.Json;
  5. namespace Abp.Notifications
  6. {
  7. /// <summary>
  8. /// Used to store data for a notification.
  9. /// It can be directly used or can be derived.
  10. /// </summary>
  11. [Serializable]
  12. public class NotificationData
  13. {
  14. /// <summary>
  15. /// Gets notification data type name.
  16. /// It returns the full class name by default.
  17. /// </summary>
  18. public virtual string Type => GetType().FullName;
  19. /// <summary>
  20. /// Shortcut to set/get <see cref="Properties"/>.
  21. /// </summary>
  22. public object this[string key]
  23. {
  24. get { return Properties.GetOrDefault(key); }
  25. set { Properties[key] = value; }
  26. }
  27. /// <summary>
  28. /// Can be used to add custom properties to this notification.
  29. /// </summary>
  30. public Dictionary<string, object> Properties
  31. {
  32. get { return _properties; }
  33. set
  34. {
  35. if (value == null)
  36. {
  37. throw new ArgumentNullException(nameof(value));
  38. }
  39. /* Not assign value, but add dictionary items. This is required for backward compability. */
  40. foreach (var keyValue in value)
  41. {
  42. if (!_properties.ContainsKey(keyValue.Key))
  43. {
  44. _properties[keyValue.Key] = keyValue.Value;
  45. }
  46. }
  47. }
  48. }
  49. private readonly Dictionary<string, object> _properties;
  50. /// <summary>
  51. /// Createa a new NotificationData object.
  52. /// </summary>
  53. public NotificationData()
  54. {
  55. _properties = new Dictionary<string, object>();
  56. }
  57. public override string ToString()
  58. {
  59. return this.ToJsonString();
  60. }
  61. }
  62. }