Exemplo n.º 1
0
        /// <summary>
        /// Returns a reference to the provided grain.
        /// </summary>
        /// <param name="grain">The grain to create a reference for.</param>
        /// <returns>A reference to the provided grain.</returns>
        internal static GrainReference AsReference(this IAddressable grain)
        {
            if (grain is null)
            {
                ThrowGrainNull();
            }

            // When called against an instance of a grain reference class, do nothing
            var reference = grain as GrainReference;

            if (reference != null)
            {
                return(reference);
            }

            var context = grain switch
            {
                Grain grainBase => grainBase.GrainContext,
                IGrainBase activation => activation.GrainContext,
                ISystemTargetBase systemTarget => systemTarget,
                _ => throw new ArgumentException(GetWrongGrainTypeErrorMessage(grain), nameof(grain))
            };

            if (context?.GrainReference is not {
            } grainRef)
            {
                throw new ArgumentException(WRONG_GRAIN_ERROR_MSG, nameof(grain));
            }

            return(grainRef);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the grain id corresponding to the provided grain.
        /// </summary>
        /// <param name="grain">The grain</param>
        /// <returns>The grain id corresponding to the provided grain.</returns>
        /// <exception cref="ArgumentException">The provided value has the wrong type or has no id.</exception>
        public static GrainId GetGrainId(this IAddressable grain)
        {
            var grainId = grain switch
            {
                Grain grainBase => grainBase.GrainId,
                GrainReference grainReference => grainReference.GrainId,
                IGrainBase grainActivation => grainActivation.GrainContext.GrainId,
                ISystemTargetBase systemTarget => systemTarget.GrainId,
                _ => throw new ArgumentException(GetWrongGrainTypeErrorMessage(grain), nameof(grain))
            };

            if (grainId.IsDefault)
            {
                throw new ArgumentException(WRONG_GRAIN_ERROR_MSG, nameof(grain));
            }

            return(grainId);
        }