Skip to content

Commit f48cfcc

Browse files
committed
Adds TryReadMetadataum extension method for Message
1 parent b3c7738 commit f48cfcc

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Xunit;
2+
3+
namespace ReactiveDomain.Messaging.Tests
4+
{
5+
public class MessageExtensionsTests
6+
{
7+
[Fact]
8+
public void can_write_and_read_metadatum()
9+
{
10+
var metadatum = new CustomMetadata { Data = "Test" };
11+
var message = new TestEvent();
12+
message.WriteMetadatum(metadatum);
13+
14+
var md = message.ReadMetadatum<CustomMetadata>();
15+
Assert.Equal(metadatum.Data, md.Data);
16+
}
17+
18+
[Fact]
19+
public void read_metadatum_throws_if_not_found()
20+
{
21+
var message = new TestEvent();
22+
Assert.Throws<MetadatumNotFoundException>(() => message.ReadMetadatum<CustomMetadata>());
23+
}
24+
25+
[Fact]
26+
public void can_try_read_metadatum()
27+
{
28+
var metadatum = new CustomMetadata { Data = "Test" };
29+
var message = new TestEvent();
30+
message.WriteMetadatum(metadatum);
31+
32+
Assert.True(message.TryReadMetadatum<CustomMetadata>(out var md));
33+
Assert.Equal(metadatum.Data, md.Data);
34+
}
35+
36+
[Fact]
37+
public void try_read_metadatum_reports_if_not_found()
38+
{
39+
var message = new TestEvent();
40+
Assert.False(message.TryReadMetadatum<CustomMetadata>(out var md));
41+
Assert.Equal(default, md);
42+
}
43+
44+
public class TestEvent : Event { }
45+
46+
public class CustomMetadata
47+
{
48+
public string Data;
49+
}
50+
}
51+
}

src/ReactiveDomain.Messaging/Messages/MessageExtensions.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,28 @@ public static void WriteMetadatum<T>(this Message msg, T metadatum)
2121
/// Reads the metadata of the specified type from a <see cref="Message"/>.
2222
/// </summary>
2323
/// <typeparam name="T">The type of the metadata.</typeparam>
24-
/// <param name="msg">The message to update.</param>
25-
/// <returns>The metadata from the message or a default object of the specified type if no metadatum of that type is found on the message.</returns>
24+
/// <param name="msg">The message to read.</param>
25+
/// <returns>The metadata from the message.</returns>
26+
/// <exception cref="MetadatumNotFoundException">Thrown if no metadatum of that type is found on the message.</exception>
2627
public static T ReadMetadatum<T>(this Message msg)
2728
{
2829
var mds = (IMetadataSource)msg;
2930
var md = mds.ReadMetadata() ?? mds.Initialize();
3031
return md.Read<T>();
3132
}
33+
34+
/// <summary>
35+
/// Tries to read the metadata of the specified type from a <see cref="Message"/>.
36+
/// </summary>
37+
/// <typeparam name="T">The type of the metadata.</typeparam>
38+
/// <param name="msg">The message to read.</param>
39+
/// <param name="metadatum">The metadata from the message.</param>
40+
/// <returns>The metadata from the message or a default object of the specified type if no metadatum of that type is found on the message.</returns>
41+
public static bool TryReadMetadatum<T>(this Message msg, out T metadatum)
42+
{
43+
var mds = (IMetadataSource)msg;
44+
var md = mds.ReadMetadata() ?? mds.Initialize();
45+
return md.TryRead(out metadatum);
46+
}
3247
}
3348
}

0 commit comments

Comments
 (0)