Skip to content

Commit b18a584

Browse files
willg1983Will GunaratneReubenBond
authored
Add GetGrain() overload for IdSpan grain identity to IGrainFactory (#9539)
* Move most IGrainFactory.GetGrain overloads to extension methods Add IGrainFactory.GetGrainType and IGrainFactory.GetGrainInterfaceType * Revert "Move most IGrainFactory.GetGrain overloads to extension methods" This reverts commit 3223e68. * add GetGrainInterfaceType() and GetGrainType() * smaller scope of changes * split into two methods instead of one with optional parameter add tests --------- Co-authored-by: Will Gunaratne <will@avius.com> Co-authored-by: Reuben Bond <203839+ReubenBond@users.noreply.github.com>
1 parent f1fe6fd commit b18a584

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-2
lines changed

src/Orleans.Core.Abstractions/Core/IGrainFactory.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,40 @@
44

55
namespace Orleans
66
{
7+
public static class GrainFactoryExtensions
8+
{
9+
/// <summary>
10+
/// Returns a reference for the provided grain id which implements the specified interface type.
11+
/// </summary>
12+
/// <param name="grainFactory">The grain factory.</param>
13+
/// <param name="grainPrimaryKey">The primary key of the grain</param>
14+
/// <param name="grainClassNamePrefix">An optional class name prefix used to find the runtime type of the grain.</param>
15+
/// <typeparam name="TGrainInterface">The grain interface type which the returned grain reference must implement.</typeparam>
16+
/// <returns>
17+
/// A reference for the provided grain id which implements the specified interface type.
18+
/// </returns>
19+
public static TGrainInterface GetGrain<TGrainInterface>(this IGrainFactory grainFactory, IdSpan grainPrimaryKey, string grainClassNamePrefix) where TGrainInterface : IGrain
20+
{
21+
ArgumentNullException.ThrowIfNull(grainFactory);
22+
return grainFactory.GetGrain(typeof(TGrainInterface), grainPrimaryKey, grainClassNamePrefix).AsReference<TGrainInterface>();
23+
}
24+
25+
/// <summary>
26+
/// Returns a reference for the provided grain id which implements the specified interface type.
27+
/// </summary>
28+
/// <param name="grainFactory">The grain factory.</param>
29+
/// <param name="grainPrimaryKey">The primary key of the grain</param>
30+
/// <typeparam name="TGrainInterface">The grain interface type which the returned grain reference must implement.</typeparam>
31+
/// <returns>
32+
/// A reference for the provided grain id which implements the specified interface type.
33+
/// </returns>
34+
public static TGrainInterface GetGrain<TGrainInterface>(this IGrainFactory grainFactory, IdSpan grainPrimaryKey) where TGrainInterface : IGrain
35+
{
36+
ArgumentNullException.ThrowIfNull(grainFactory);
37+
return grainFactory.GetGrain(typeof(TGrainInterface), grainPrimaryKey).AsReference<TGrainInterface>();
38+
}
39+
}
40+
741
/// <summary>
842
/// Functionality for creating references to grains.
943
/// </summary>
@@ -190,5 +224,26 @@ public interface IGrainFactory
190224
/// A reference for the provided grain id which implements the specified interface type.
191225
/// </returns>
192226
IAddressable GetGrain(GrainId grainId, GrainInterfaceType interfaceType);
227+
228+
/// <summary>
229+
/// Returns a reference for the provided grain id which implements the specified interface type.
230+
/// </summary>
231+
/// <param name="interfaceType">The grain interface type which the returned grain reference must implement.</param>
232+
/// <param name="grainKey">The primary key of the grain</param>
233+
/// <param name="grainClassNamePrefix">A class name prefix used to find the runtime type of the grain.</param>
234+
/// <returns>
235+
/// A reference for the provided grain id which implements the specified interface type.
236+
/// </returns>
237+
IAddressable GetGrain(Type interfaceType, IdSpan grainKey, string grainClassNamePrefix);
238+
239+
/// <summary>
240+
/// Returns a reference for the provided grain id which implements the specified interface type.
241+
/// </summary>
242+
/// <param name="interfaceType">The grain interface type which the returned grain reference must implement.</param>
243+
/// <param name="grainKey">The primary key of the grain</param>
244+
/// <returns>
245+
/// A reference for the provided grain id which implements the specified interface type.
246+
/// </returns>
247+
IAddressable GetGrain(Type interfaceType, IdSpan grainKey);
193248
}
194249
}

src/Orleans.Core/Core/ClusterClient.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ public object Cast(IAddressable grain, Type outputGrainInterfaceType)
153153
public IAddressable GetGrain(GrainId grainId, GrainInterfaceType interfaceType)
154154
=> _runtimeClient.InternalGrainFactory.GetGrain(grainId, interfaceType);
155155

156+
/// <inheritdoc />
157+
public IAddressable GetGrain(Type interfaceType, IdSpan grainKey, string grainClassNamePrefix)
158+
=> _runtimeClient.InternalGrainFactory.GetGrain(interfaceType, grainKey, grainClassNamePrefix);
159+
160+
/// <inheritdoc />
161+
public IAddressable GetGrain(Type interfaceType, IdSpan grainKey)
162+
=> _runtimeClient.InternalGrainFactory.GetGrain(interfaceType, grainKey);
163+
156164
[LoggerMessage(
157165
Level = LogLevel.Information,
158166
Message = "Client shutting down."
@@ -164,5 +172,6 @@ public IAddressable GetGrain(GrainId grainId, GrainInterfaceType interfaceType)
164172
Message = "Client shutdown completed."
165173
)]
166174
private static partial void LogClientShutdownCompleted(ILogger logger);
175+
167176
}
168-
}
177+
}

src/Orleans.Core/Core/GrainFactory.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ public IAddressable GetGrain(GrainId grainId, GrainInterfaceType interfaceType)
190190
return this.referenceActivator.CreateReference(grainId, interfaceType);
191191
}
192192

193+
/// <inheritdoc />
194+
public IAddressable GetGrain(Type interfaceType, IdSpan grainKey) => GetGrain(interfaceType, grainKey, null);
195+
193196
/// <summary>
194197
/// Gets a grain reference which implements the specified grain interface type and has the specified grain key, without specifying the grain type directly.
195198
/// </summary>
@@ -201,8 +204,10 @@ public IAddressable GetGrain(GrainId grainId, GrainInterfaceType interfaceType)
201204
/// <param name="grainKey">The <see cref="GrainId.Key"/> portion of the grain id.</param>
202205
/// <param name="grainClassNamePrefix">An optional grain class name prefix.</param>
203206
/// <returns>A grain reference which implements the provided interface.</returns>
204-
private IAddressable GetGrain(Type interfaceType, IdSpan grainKey, string grainClassNamePrefix)
207+
public IAddressable GetGrain(Type interfaceType, IdSpan grainKey, string grainClassNamePrefix = null)
205208
{
209+
ArgumentNullException.ThrowIfNull(interfaceType);
210+
206211
var grainInterfaceType = this.interfaceTypeResolver.GetGrainInterfaceType(interfaceType);
207212

208213
GrainType grainType;

src/Orleans.Runtime/Core/InternalClusterClient.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,22 @@ public IGrain GetGrain(Type grainInterfaceType, long grainPrimaryKey, string key
146146
return this.grainFactory.GetGrain(grainInterfaceType, grainPrimaryKey);
147147
}
148148

149+
/// <inheritdoc />
149150
public IAddressable GetGrain(GrainId grainId, GrainInterfaceType interfaceId)
150151
{
151152
return this.grainFactory.GetGrain(grainId, interfaceId);
152153
}
154+
155+
/// <inheritdoc />
156+
public IAddressable GetGrain(Type interfaceType, IdSpan grainKey, string grainClassNamePrefix)
157+
{
158+
return this.grainFactory.GetGrain(interfaceType, grainKey, grainClassNamePrefix);
159+
}
160+
161+
/// <inheritdoc />
162+
public IAddressable GetGrain(Type interfaceType, IdSpan grainKey)
163+
{
164+
return this.grainFactory.GetGrain(interfaceType, grainKey);
165+
}
153166
}
154167
}

test/DefaultCluster.Tests/GrainFactoryTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,33 @@ public async Task GetGuidGrain()
225225
var g = this.GrainFactory.GetGrain<IGuidGrain>(Guid.NewGuid());
226226
Assert.True(await g.Foo());
227227
}
228+
229+
[Fact, TestCategory("BVT"), TestCategory("Factory"), TestCategory("GetGrain")]
230+
public async Task GetGrainIdSpanGeneric()
231+
{
232+
var g = GrainFactory.GetGrain<IDerivedFromBase>(GrainIdKeyExtensions.CreateIntegerKey(0));
233+
await g.Foo();
234+
}
235+
236+
[Fact, TestCategory("BVT"), TestCategory("Factory"), TestCategory("GetGrain")]
237+
public async Task GetGrainIdSpan()
238+
{
239+
var g = GrainFactory.GetGrain(typeof(IDerivedFromBase), GrainIdKeyExtensions.CreateIntegerKey(0));
240+
await g.AsReference<IBase>().Foo();
241+
}
242+
243+
[Fact, TestCategory("BVT"), TestCategory("Factory"), TestCategory("GetGrain")]
244+
public async Task GetGrainIdSpanGenericPrefix()
245+
{
246+
var g = GrainFactory.GetGrain<IBase>(GrainIdKeyExtensions.CreateIntegerKey(0), BaseGrain.GrainPrefix);
247+
await g.Foo();
248+
}
249+
250+
[Fact, TestCategory("BVT"), TestCategory("Factory"), TestCategory("GetGrain")]
251+
public async Task GetGrainIdSpanPrefix()
252+
{
253+
var g = GrainFactory.GetGrain(typeof(IBase), GrainIdKeyExtensions.CreateIntegerKey(0), BaseGrain.GrainPrefix);
254+
await g.AsReference<IBase>().Foo();
255+
}
228256
}
229257
}

0 commit comments

Comments
 (0)