/src/NServiceBus.Core/Routing/MessageDrivenSubscriptions/MessageType.cs

https://github.com/MikeEast/NServiceBus
C# | 147 lines | 92 code | 16 blank | 39 comment | 7 complexity | 8d62c4f887530f7b9adfed5e7f4f79c9 MD5 | raw file
  1. namespace NServiceBus.Unicast.Subscriptions
  2. {
  3. using System;
  4. using System.Linq;
  5. /// <summary>
  6. /// Representation of a message type that clients can be subscribed to.
  7. /// </summary>
  8. public class MessageType
  9. {
  10. /// <summary>
  11. /// Initializes the message type from the given type.
  12. /// </summary>
  13. public MessageType(Type type)
  14. {
  15. Guard.AgainstNull(nameof(type), type);
  16. Version = type.Assembly.GetName().Version;
  17. TypeName = type.FullName;
  18. }
  19. /// <summary>
  20. /// Initializes the message type from the given string.
  21. /// </summary>
  22. public MessageType(string messageTypeString)
  23. {
  24. Guard.AgainstNullAndEmpty(nameof(messageTypeString), messageTypeString);
  25. var parts = messageTypeString.Split(',');
  26. Version = ParseVersion(messageTypeString);
  27. TypeName = parts.First();
  28. }
  29. /// <summary>
  30. /// Initializes the message type from the given string.
  31. /// </summary>
  32. public MessageType(string typeName, string versionString)
  33. {
  34. Guard.AgainstNullAndEmpty(nameof(typeName), typeName);
  35. Guard.AgainstNullAndEmpty(nameof(versionString), versionString);
  36. Version = ParseVersion(versionString);
  37. TypeName = typeName;
  38. }
  39. /// <summary>
  40. /// Initializes the message type from the given string.
  41. /// </summary>
  42. public MessageType(string typeName, Version version)
  43. {
  44. Guard.AgainstNullAndEmpty(nameof(typeName), typeName);
  45. Guard.AgainstNull(nameof(version), version);
  46. Version = version;
  47. TypeName = typeName;
  48. }
  49. /// <summary>
  50. /// TypeName of the message.
  51. /// </summary>
  52. public string TypeName { get; }
  53. /// <summary>
  54. /// Version of the message.
  55. /// </summary>
  56. public Version Version { get; }
  57. Version ParseVersion(string versionString)
  58. {
  59. const string version = "Version=";
  60. var index = versionString.IndexOf(version);
  61. if (index >= 0)
  62. {
  63. versionString = versionString.Substring(index + version.Length)
  64. .Split(',').First();
  65. }
  66. return Version.Parse(versionString);
  67. }
  68. /// <summary>
  69. /// Overridden to append Version along with Type Name.
  70. /// </summary>
  71. public override string ToString()
  72. {
  73. return TypeName + ", Version=" + Version;
  74. }
  75. /// <summary>
  76. /// Equality only compares the <see cref="TypeName"/> and ignores versions.
  77. /// </summary>
  78. public bool Equals(MessageType other)
  79. {
  80. if (other is null)
  81. {
  82. return false;
  83. }
  84. if (ReferenceEquals(this, other))
  85. {
  86. return true;
  87. }
  88. return Equals(other.TypeName, TypeName);
  89. }
  90. /// <summary>
  91. /// Equality only compares the <see cref="TypeName"/> and ignores versions.
  92. /// </summary>
  93. public override bool Equals(object obj)
  94. {
  95. if (obj is null)
  96. {
  97. return false;
  98. }
  99. if (ReferenceEquals(this, obj))
  100. {
  101. return true;
  102. }
  103. if (obj.GetType() != typeof(MessageType))
  104. {
  105. return false;
  106. }
  107. return Equals((MessageType)obj);
  108. }
  109. /// <summary>
  110. /// Gets Hash Code.
  111. /// </summary>
  112. public override int GetHashCode()
  113. {
  114. return TypeName.GetHashCode();
  115. }
  116. /// <summary>
  117. /// Equality.
  118. /// </summary>
  119. public static bool operator ==(MessageType left, MessageType right)
  120. {
  121. return Equals(left, right);
  122. }
  123. /// <summary>
  124. /// Equality.
  125. /// </summary>
  126. public static bool operator !=(MessageType left, MessageType right)
  127. {
  128. return !Equals(left, right);
  129. }
  130. }
  131. }