Creating GrainId #7617
-
I'm creating a library where I want the consumer to choose what the primary key type is - I see I can use GrainId for this, is there a better way of instiating one? I'm doing the following currently: public interface IMyLibraryGrain : IGrain
{
Task Foo();
}
// and user can create a a GrainId using an extension method something like below..
public static GrainId CreateMyLibraryGrainId(this IGrainFactory grainFactory, Guid|long|string key)
{
return grainFactory.GetGrain(typeof(IMyLibraryGrain), key).GetGrainId();
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I'm not entirely sure what you're after, but perhaps I can supply a bit of detail: A
The trickier part is the grain type. If you know what the name of the grain type you need is (eg, because it's using the default convention-based grain type where
Both of these types can be injected. So, a public class GrainIdFactory
{
private readonly GrainInterfaceTypeResolver _grainInterfaceTypeResolver;
private readonly GrainInterfaceTypeToGrainTypeResolver _grainInterfaceTypeToGrainTypeResolver;
public GrainIdFactory(
GrainInterfaceTypeResolver interfaceTypeResolver,
GrainInterfaceTypeToGrainTypeResolver interfaceToTypeResolver)
{
_grainInterfaceTypeResolver = interfaceTypeResolver;
_grainInterfaceTypeToGrainTypeResolver = interfaceToTypeResolver;
}
public GrainId CreateGrainId<TInterface>(Guid grainKey) where TInterface : IAddressable
{
GrainType grainType = ResolveGrainType(typeof(TInterface));
GrainId grainId = GrainId.Create(grainType, GrainIdKeyExtensions.CreateGuidKey(grainKey));
return grainId;
}
public GrainId CreateGrainId<TInterface>(long grainKey) where TInterface : IAddressable
{
GrainType grainType = ResolveGrainType(typeof(TInterface));
GrainId grainId = GrainId.Create(grainType, GrainIdKeyExtensions.CreateIntegerKey(grainKey));
return grainId;
}
public GrainId CreateGrainId<TInterface>(string grainKey) where TInterface : IAddressable
{
GrainType grainType = ResolveGrainType(typeof(TInterface));
GrainId grainId = GrainId.Create(grainType, grainKey);
return grainId;
}
private GrainType ResolveGrainType(Type interfaceType)
{
GrainInterfaceType grainInterfaceType = _grainInterfaceTypeResolver.GetGrainInterfaceType(interfaceType);
GrainType grainType = _grainInterfaceTypeToGrainTypeResolver.GetGrainType(grainInterfaceType);
return grainType;
}
} One important thing to note: the mapping from Hope that helps. |
Beta Was this translation helpful? Give feedback.
I'm not entirely sure what you're after, but perhaps I can supply a bit of detail:
A
GrainId
consists of two components,Type
andKey
, both are essentially strings.Orleans treats the
Key
component as opaque.The
Type
components has some meaning because it maps to grain implementation classes and if those classes are generic then theType
component will contain the encoded generic parameters (eg,my-grain`2[[int],[string]]
).Orleans.Runtime.GrainIdKeyExtensions
is a static class with methods for creatingGuid
&long
keys (encoded as strings)You can use this to create
Guid
/long
keys. For string keys, you can pass the string directly toGrainId.Create(...)
. These keys have no special meani…