Пример #1
0
        public static async Task <Grain> New(Type concreteType, AbstractKey key, IGrainRuntime grainRuntime, IServiceProvider services, StorageCell storage, FakeSerializer serializer)
        {
            var creator = new GrainCreator(grainRuntime, services);

            var stateType = GetStateType(concreteType);

            var grain = stateType != null
                            ? creator.CreateGrainInstance(concreteType, key, stateType, new DummyStorageProvider()) //IStorage will be hackily assigned below      // new StorageProviderAdaptor(key, store))
                            : creator.CreateGrainInstance(concreteType, key);

            if (stateType != null)
            {
                var fnStateExtractor = _dStateExtractors.GetOrAdd(concreteType, t => BuildStateExtractor(t));
                var fnStorageAssign  = _dStorageAssigners.GetOrAdd(concreteType, t => BuildStorageAssigner(t));

                var grainState = fnStateExtractor(grain);

                var bridge = new GrainStorageBridge(storage, serializer, grainState);
                fnStorageAssign(grain, bridge);

                await bridge.ReadStateAsync();
            }

            return(grain);
        }
Пример #2
0
        private static T CreateGrain <T>(IGrainIdentity identity) where T : Grain
        {
            Grain grain;

            //Check to see if the grain is stateful
            if (IsSubclassOfRawGeneric(typeof(Grain <>), typeof(T)))
            {
                var grainGenericArgs = typeof(T).BaseType?.GenericTypeArguments;

                if (grainGenericArgs == null || grainGenericArgs.Length == 0)
                {
                    throw new Exception($"Unable to get grain state type info for {typeof(T)}");
                }

                //Get the state type
                var stateType = grainGenericArgs[0];

                //Create a new stateful grain
                grain = GrainCreator.CreateGrainInstance(typeof(T), identity, stateType, StorageProvider);
            }
            else
            {
                //Create a stateless grain
                grain = GrainCreator.CreateGrainInstance(typeof(T), identity) as T;
            }

            if (grain == null)
            {
                return(null);
            }

            //Emulate the grain's lifecycle
            grain.OnActivateAsync();

            return(grain as T);
        }