Skip to content

Commit

Permalink
Fix IGrainFactory.GetGrain<T> where T is a generic interface with no …
Browse files Browse the repository at this point in the history
…implementations (#8301)
  • Loading branch information
ReubenBond authored Feb 1, 2023
1 parent 58b072e commit 8cf2732
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/Orleans.Core/Core/GrainInterfaceTypeToGrainTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,21 @@ public GrainType GetGrainType(GrainInterfaceType interfaceType, string prefix)
/// </summary>
public GrainType GetGrainType(GrainInterfaceType interfaceType)
{
GrainType result = default;
if (!TryGetGrainType(interfaceType, out var result))
{
throw new ArgumentException($"Could not find an implementation for interface {interfaceType}");
}

return result;
}

/// <summary>
/// Resolves a <see cref="GrainType"/> which implements the provided <see cref="GrainInterfaceType"/>, returning <see langword="true"/> if an implementation was found; otherwise <see langword="false"/>.
/// </summary>
/// <returns><see langword="true"/> if an implementation was found; otherwise <see langword="false"/>.</returns>
public bool TryGetGrainType(GrainInterfaceType interfaceType, out GrainType result)
{
result = default;
var cache = GetCache();
if (cache.Map.TryGetValue(interfaceType, out var entry))
{
Expand All @@ -122,34 +136,32 @@ public GrainType GetGrainType(GrainInterfaceType interfaceType)
else if (_genericMapping.TryGetValue(interfaceType, out result))
{
}
else if (GenericGrainInterfaceType.TryParse(interfaceType, out var genericInterface))
else if (GenericGrainInterfaceType.TryParse(interfaceType, out var genericInterface) && genericInterface.IsConstructed)
{
var unconstructedInterface = genericInterface.GetGenericGrainType();
var unconstructed = GetGrainType(unconstructedInterface.Value);
if (GenericGrainType.TryParse(unconstructed, out var genericGrainType))
if (TryGetGrainType(unconstructedInterface.Value, out var unconstructed))
{
if (genericGrainType.IsConstructed)
if (GenericGrainType.TryParse(unconstructed, out var genericGrainType))
{
result = genericGrainType.GrainType;
if (genericGrainType.IsConstructed)
{
result = genericGrainType.GrainType;
}
else
{
result = genericGrainType.GrainType.GetConstructed(genericInterface.Value);
}
}
else
{
result = genericGrainType.GrainType.GetConstructed(genericInterface.Value);
result = unconstructed;
}
}
else
{
result = unconstructed;
}
_genericMapping[interfaceType] = result;
}

if (result.IsDefault)
{
throw new ArgumentException($"Could not find an implementation for interface {interfaceType}");
_genericMapping[interfaceType] = result;
}

return result;
return !result.IsDefault;
}

/// <summary>
Expand Down

0 comments on commit 8cf2732

Please sign in to comment.