Skip to content

Commit e39c594

Browse files
author
Daniel Marbach
authored
Merge pull request #298 from Particular/containerinformation-instance-backport
Add overload to provide a ContainerInformation instance
2 parents 5ccdcb8 + 102bcab commit e39c594

File tree

6 files changed

+104
-46
lines changed

6 files changed

+104
-46
lines changed

src/NServiceBus.Persistence.CosmosDB.Tests/ApprovalFiles/APIApprovals.Approve.approved.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ namespace NServiceBus.Persistence.CosmosDB
9696
public class TransactionInformationConfiguration
9797
{
9898
public TransactionInformationConfiguration() { }
99+
public void ExtractContainerInformationFromHeader(string headerKey, NServiceBus.ContainerInformation containerInformation) { }
99100
public void ExtractContainerInformationFromHeader(string headerKey, System.Func<string, NServiceBus.ContainerInformation> extractor) { }
100101
public void ExtractContainerInformationFromHeader<TArg>(string headerKey, System.Func<string, TArg, NServiceBus.ContainerInformation> extractor, TArg extractorArgument) { }
101102
public void ExtractContainerInformationFromHeaders(NServiceBus.Persistence.CosmosDB.IContainerInformationFromHeadersExtractor extractor) { }
102103
public void ExtractContainerInformationFromHeaders(System.Func<System.Collections.Generic.IReadOnlyDictionary<string, string>, NServiceBus.ContainerInformation?> extractor) { }
103104
public void ExtractContainerInformationFromHeaders<TArg>(System.Func<System.Collections.Generic.IReadOnlyDictionary<string, string>, TArg, NServiceBus.ContainerInformation?> extractor, TArg extractorArgument) { }
104105
public void ExtractContainerInformationFromMessage(NServiceBus.Persistence.CosmosDB.IContainerInformationFromMessagesExtractor extractor) { }
106+
public void ExtractContainerInformationFromMessage<TMessage>(NServiceBus.ContainerInformation containerInformation) { }
105107
public void ExtractContainerInformationFromMessage<TMessage>(System.Func<TMessage, NServiceBus.ContainerInformation> extractor) { }
106108
public void ExtractContainerInformationFromMessage<TMessage>(System.Func<TMessage, System.Collections.Generic.IReadOnlyDictionary<string, string>, NServiceBus.ContainerInformation> extractor) { }
107109
public void ExtractContainerInformationFromMessage<TMessage, TArg>(System.Func<TMessage, TArg, NServiceBus.ContainerInformation> extractor, TArg extractorArgument) { }

src/NServiceBus.Persistence.CosmosDB.Tests/Transaction/ContainerInformationExtractorTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ public void Should_extract_from_header_with_first_match_winning()
5050
Assert.That(partitionKey, Is.Not.Null.And.EqualTo(new ContainerInformation("HeaderValue", fakePartitionKeyPath)));
5151
}
5252

53+
[Test]
54+
public void Should_extract_from_header_with_fixed_container()
55+
{
56+
extractor.ExtractContainerInformationFromHeader("HeaderKey", new ContainerInformation("FixedValue", fakePartitionKeyPath));
57+
58+
var headers = new Dictionary<string, string> { { "HeaderKey", "DOES NOT MATTER" } };
59+
60+
var wasExtracted = extractor.TryExtract(headers, out var containerInformation);
61+
62+
Assert.That(wasExtracted, Is.True);
63+
Assert.That(containerInformation, Is.Not.Null.And.EqualTo(new ContainerInformation("FixedValue", fakePartitionKeyPath)));
64+
}
65+
5366
[Test]
5467
public void Should_extract_from_header_with_key_and_extractor()
5568
{
@@ -210,6 +223,19 @@ public void Should_extract_from_message()
210223
Assert.That(partitionKey, Is.Not.Null.And.EqualTo(new ContainerInformation("SomeValue", fakePartitionKeyPath)));
211224
}
212225

226+
[Test]
227+
public void Should_extract_from_message_with_fixed_container()
228+
{
229+
extractor.ExtractContainerInformationFromMessage<MyMessage>(new ContainerInformation("FixedValue", fakePartitionKeyPath));
230+
231+
var message = new MyMessage { SomeId = "SomeValue" };
232+
233+
var wasExtracted = extractor.TryExtract(message, new Dictionary<string, string>(), out var partitionKey);
234+
235+
Assert.That(wasExtracted, Is.True);
236+
Assert.That(partitionKey, Is.Not.Null.And.EqualTo(new ContainerInformation("FixedValue", fakePartitionKeyPath)));
237+
}
238+
213239
[Test]
214240
public void Should_extract_from_message_with_argument()
215241
{

src/NServiceBus.Persistence.CosmosDB/Transaction/ContainerInformationExtractor.Headers.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public bool TryExtract(IReadOnlyDictionary<string, string> headers, out Containe
2727
return false;
2828
}
2929

30+
public void ExtractContainerInformationFromHeader(string headerKey, ContainerInformation containerInformation) =>
31+
// When moving to CSharp 9 these can be static lambdas
32+
ExtractContainerInformationFromHeader(headerKey, (_, container) => container, containerInformation);
33+
3034
public void ExtractContainerInformationFromHeader(string headerKey, Func<string, ContainerInformation> converter) =>
3135
// When moving to CSharp 9 these can be static lambdas
3236
ExtractContainerInformationFromHeader(headerKey, (headerValue, invoker) => invoker(headerValue), converter);

src/NServiceBus.Persistence.CosmosDB/Transaction/ContainerInformationExtractor.Messages.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public bool TryExtract(object message, IReadOnlyDictionary<string, string> heade
2727
return false;
2828
}
2929

30+
public void ExtractContainerInformationFromMessage<TMessage>(ContainerInformation containerInformation) =>
31+
// When moving to CSharp 9 these can be static lambdas
32+
ExtractContainerInformationFromMessage<TMessage, ContainerInformation>((_, container) => container, containerInformation);
33+
3034
public void ExtractContainerInformationFromMessage<TMessage>(Func<TMessage, ContainerInformation> extractor) =>
3135
// When moving to CSharp 9 these can be static lambdas
3236
ExtractContainerInformationFromMessage<TMessage, Func<TMessage, ContainerInformation>>((msg, _, invoker) => invoker(msg), extractor);

src/NServiceBus.Persistence.CosmosDB/Transaction/TransactionInformationConfiguration.ContainerInformation.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
/// </summary>
99
public partial class TransactionInformationConfiguration
1010
{
11+
/// <summary>
12+
/// Adds an extraction rule that provides the same container information when the given <paramref name="headerKey"/> exists.
13+
/// </summary>
14+
/// <param name="headerKey">The header key.</param>
15+
/// <param name="containerInformation">The container information to be used for the specified <paramref name="headerKey"/>.</param>
16+
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
17+
public void ExtractContainerInformationFromHeader(string headerKey, ContainerInformation containerInformation) =>
18+
ContainerInformationExtractor.ExtractContainerInformationFromHeader(headerKey, containerInformation);
19+
1120
/// <summary>
1221
/// Adds an extraction rule that extracts the container information from a given header represented by <paramref name="headerKey"/>.
1322
/// </summary>
@@ -49,10 +58,20 @@ public void ExtractContainerInformationFromHeaders<TArg>(Func<IReadOnlyDictionar
4958
/// <summary>
5059
/// Adds an instance of <see cref="IContainerInformationFromHeadersExtractor"/> to the list of header extractors.
5160
/// </summary>
61+
/// <param name="extractor">The custom extractor.</param>
5262
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
5363
public void ExtractContainerInformationFromHeaders(IContainerInformationFromHeadersExtractor extractor) =>
5464
ContainerInformationExtractor.ExtractContainerInformationFromHeaders(extractor);
5565

66+
/// <summary>
67+
/// Adds an extraction rule that provides the same container information for a given message type <typeparamref name="TMessage"/>
68+
/// </summary>
69+
/// <param name="containerInformation">The container information to be used for the specified <typeparamref name="TMessage"/>.</param>
70+
/// <typeparam name="TMessage">The message type to match against.</typeparam>
71+
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
72+
public void ExtractContainerInformationFromMessage<TMessage>(ContainerInformation containerInformation) =>
73+
ContainerInformationExtractor.ExtractContainerInformationFromMessage<TMessage>(containerInformation);
74+
5675
/// <summary>
5776
/// Adds an extraction rule that extracts the container information from a given message type <typeparamref name="TMessage"/>
5877
/// </summary>
@@ -96,6 +115,7 @@ public void ExtractContainerInformationFromMessage<TMessage, TArg>(Func<TMessage
96115
/// <summary>
97116
/// Adds an instance of <see cref="IContainerInformationFromMessagesExtractor"/> to the list of message extractors.
98117
/// </summary>
118+
/// <param name="extractor">The custom extractor.</param>
99119
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
100120
public void ExtractContainerInformationFromMessage(IContainerInformationFromMessagesExtractor extractor) =>
101121
ContainerInformationExtractor.ExtractContainerInformationFromMessage(extractor);

src/NServiceBus.Persistence.CosmosDB/Transaction/TransactionInformationConfiguration.PartitionKey.cs

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,10 @@ public partial class TransactionInformationConfiguration
1212
/// <summary>
1313
/// Adds an instance of <see cref="IPartitionKeyFromHeadersExtractor"/> to the list of header extractors.
1414
/// </summary>
15+
/// <param name="extractor">The custom extractor.</param>
1516
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
1617
public void ExtractPartitionKeyFromHeaders(IPartitionKeyFromHeadersExtractor extractor) => PartitionKeyExtractor.ExtractPartitionKeyFromHeaders(extractor);
1718

18-
/// <summary>
19-
/// Adds an instance of <see cref="IPartitionKeyFromMessageExtractor"/> to the list of header extractors.
20-
/// </summary>
21-
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
22-
public void ExtractPartitionKeyFromMessages(IPartitionKeyFromMessageExtractor extractor) => PartitionKeyExtractor.ExtractPartitionKeyFromMessages(extractor);
23-
24-
/// <summary>
25-
/// Adds an extraction rule that extracts the partition key from a given message type <typeparamref name="TMessage"/>
26-
/// </summary>
27-
/// <param name="extractor">The extraction function.</param>
28-
/// <typeparam name="TMessage">The message type to match against.</typeparam>
29-
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
30-
public void ExtractPartitionKeyFromMessage<TMessage>(Func<TMessage, PartitionKey> extractor) =>
31-
PartitionKeyExtractor.ExtractPartitionKeyFromMessage(extractor);
32-
33-
/// <summary>
34-
/// Adds an extraction rule that extracts the partition key from a given message type <typeparamref name="TMessage"/>
35-
/// </summary>
36-
/// <param name="extractor">The extraction function.</param>
37-
/// <param name="extractorArgument">The argument passed as state to the <paramref name="extractor"/></param>
38-
/// <typeparam name="TMessage">The message type to match against.</typeparam>
39-
/// <typeparam name="TArg">The argument passed as state to the <paramref name="extractor"/></typeparam>
40-
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
41-
public void ExtractPartitionKeyFromMessage<TMessage, TArg>(Func<TMessage, TArg, PartitionKey> extractor, TArg extractorArgument) =>
42-
PartitionKeyExtractor.ExtractPartitionKeyFromMessage(extractor, extractorArgument);
43-
44-
/// <summary>
45-
/// Adds an extraction rule that extracts the partition key from a given message type <typeparamref name="TMessage"/>
46-
/// </summary>
47-
/// <param name="extractor">The extraction function.</param>
48-
/// <typeparam name="TMessage">The message type to match against.</typeparam>
49-
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
50-
public void ExtractPartitionKeyFromMessage<TMessage>(Func<TMessage, IReadOnlyDictionary<string, string>, PartitionKey> extractor) =>
51-
PartitionKeyExtractor.ExtractPartitionKeyFromMessage(extractor);
52-
53-
/// <summary>
54-
/// Adds an extraction rule that extracts the partition key from a given message type <typeparamref name="TMessage"/>
55-
/// </summary>
56-
/// <param name="extractor">The extraction function.</param>
57-
/// <param name="extractorArgument">The argument passed as state to the <paramref name="extractor"/></param>
58-
/// <typeparam name="TMessage">The message type to match against.</typeparam>
59-
/// <typeparam name="TArg">The argument passed as state to the <paramref name="extractor"/></typeparam>
60-
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
61-
public void ExtractPartitionKeyFromMessage<TMessage, TArg>(Func<TMessage, IReadOnlyDictionary<string, string>, TArg, PartitionKey> extractor, TArg extractorArgument) =>
62-
PartitionKeyExtractor.ExtractPartitionKeyFromMessage(extractor, extractorArgument);
63-
6419
/// <summary>
6520
/// Adds an extraction rule that extracts the partition key from a given header represented by <paramref name="headerKey"/>.
6621
/// </summary>
@@ -127,6 +82,53 @@ public void ExtractPartitionKeyFromHeader(string headerKey, Func<string, Partiti
12782
public void ExtractPartitionKeyFromHeader<TArg>(string headerKey, Func<string, TArg, PartitionKey> extractor, TArg extractorArgument) =>
12883
PartitionKeyExtractor.ExtractPartitionKeyFromHeader(headerKey, extractor, extractorArgument);
12984

85+
/// <summary>
86+
/// Adds an instance of <see cref="IPartitionKeyFromMessageExtractor"/> to the list of header extractors.
87+
/// </summary>
88+
/// <param name="extractor">The custom extractor.</param>
89+
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
90+
public void ExtractPartitionKeyFromMessages(IPartitionKeyFromMessageExtractor extractor) => PartitionKeyExtractor.ExtractPartitionKeyFromMessages(extractor);
91+
92+
/// <summary>
93+
/// Adds an extraction rule that extracts the partition key from a given message type <typeparamref name="TMessage"/>
94+
/// </summary>
95+
/// <param name="extractor">The extraction function.</param>
96+
/// <typeparam name="TMessage">The message type to match against.</typeparam>
97+
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
98+
public void ExtractPartitionKeyFromMessage<TMessage>(Func<TMessage, PartitionKey> extractor) =>
99+
PartitionKeyExtractor.ExtractPartitionKeyFromMessage(extractor);
100+
101+
/// <summary>
102+
/// Adds an extraction rule that extracts the partition key from a given message type <typeparamref name="TMessage"/>
103+
/// </summary>
104+
/// <param name="extractor">The extraction function.</param>
105+
/// <param name="extractorArgument">The argument passed as state to the <paramref name="extractor"/></param>
106+
/// <typeparam name="TMessage">The message type to match against.</typeparam>
107+
/// <typeparam name="TArg">The argument passed as state to the <paramref name="extractor"/></typeparam>
108+
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
109+
public void ExtractPartitionKeyFromMessage<TMessage, TArg>(Func<TMessage, TArg, PartitionKey> extractor, TArg extractorArgument) =>
110+
PartitionKeyExtractor.ExtractPartitionKeyFromMessage(extractor, extractorArgument);
111+
112+
/// <summary>
113+
/// Adds an extraction rule that extracts the partition key from a given message type <typeparamref name="TMessage"/>
114+
/// </summary>
115+
/// <param name="extractor">The extraction function.</param>
116+
/// <typeparam name="TMessage">The message type to match against.</typeparam>
117+
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
118+
public void ExtractPartitionKeyFromMessage<TMessage>(Func<TMessage, IReadOnlyDictionary<string, string>, PartitionKey> extractor) =>
119+
PartitionKeyExtractor.ExtractPartitionKeyFromMessage(extractor);
120+
121+
/// <summary>
122+
/// Adds an extraction rule that extracts the partition key from a given message type <typeparamref name="TMessage"/>
123+
/// </summary>
124+
/// <param name="extractor">The extraction function.</param>
125+
/// <param name="extractorArgument">The argument passed as state to the <paramref name="extractor"/></param>
126+
/// <typeparam name="TMessage">The message type to match against.</typeparam>
127+
/// <typeparam name="TArg">The argument passed as state to the <paramref name="extractor"/></typeparam>
128+
/// <remarks>Explicitly added extractors and extraction rules are executed before extractors registered on the container.</remarks>
129+
public void ExtractPartitionKeyFromMessage<TMessage, TArg>(Func<TMessage, IReadOnlyDictionary<string, string>, TArg, PartitionKey> extractor, TArg extractorArgument) =>
130+
PartitionKeyExtractor.ExtractPartitionKeyFromMessage(extractor, extractorArgument);
131+
130132
internal PartitionKeyExtractor PartitionKeyExtractor { get; } = new PartitionKeyExtractor();
131133
}
132134
}

0 commit comments

Comments
 (0)