Skip to content

Commit

Permalink
feat: Adds raw Payload to the CronusMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
mynkow committed Apr 28, 2023
2 parents 0e90e7c + e68a3a9 commit 816644c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/Elders.Cronus/CronusMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public class CronusMessage : IEquatable<CronusMessage>
Headers = new Dictionary<string, string>();
}

public CronusMessage(byte[] message, Type messageType, IDictionary<string, string> headers) : this()
{
Id = Guid.NewGuid();
PayloadRaw = message;
Headers = headers;
Headers.TryAdd(MessageHeader.MessageType, messageType.GetContractId());
}

public CronusMessage(IMessage message, IDictionary<string, string> headers) : this()
{
Id = Guid.NewGuid();
Expand All @@ -29,6 +37,18 @@ public CronusMessage(IMessage message, IDictionary<string, string> headers) : th
[DataMember(Order = 3)]
public IDictionary<string, string> Headers { get; private set; }

[DataMember(Order = 4)]
public byte[] PayloadRaw { get; private set; }

public Type GetMessageType()
{
Type messageType = Payload is not null
? Payload.GetType()
: Headers[MessageHeader.MessageType].GetTypeByContract();

return messageType;
}

public string MessageId { get { return GetHeader(MessageHeader.MessageId); } }

public string CausationId { get { return GetHeader(MessageHeader.CausationId); } }
Expand Down Expand Up @@ -79,15 +99,15 @@ bool HasHeader(string key)

public override bool Equals(System.Object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (!typeof(CronusMessage).IsAssignableFrom(obj.GetType())) return false;
return Equals((CronusMessage)obj);
}

public virtual bool Equals(CronusMessage other)
{
if (ReferenceEquals(null, other)) return false;
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return this.Id == other.Id;
}
Expand All @@ -102,8 +122,8 @@ public override int GetHashCode()

public static bool operator ==(CronusMessage left, CronusMessage right)
{
if (ReferenceEquals(null, left) && ReferenceEquals(null, right)) return true;
if (ReferenceEquals(null, left))
if (left is null && right is null) return true;
if (left is null)
return false;
else
return left.Equals(right);
Expand Down
2 changes: 2 additions & 0 deletions src/Elders.Cronus/MessageHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public static class MessageHeader

public const string MessageId = "messageid";

public const string MessageType = "messagetype";

public const string Tenant = "tenant";

public const string BoundedContext = "bounded_context";
Expand Down

0 comments on commit 816644c

Please sign in to comment.