Exemplo n.º 1
0
        /// <summary>
        /// Create virtual actor, return current instance or create one
        /// </summary>
        /// <typeparam name="T">actor interface</typeparam>
        /// <param name="context">context</param>
        /// <param name="actorKey">actor key</param>
        /// <returns>actor proxy interface</returns>
        public async Task <T> CreateProxyAsync <T>(IWorkContext context, ActorKey actorKey) where T : IActor
        {
            Verify.Assert(IsRunning, _disposedTestText);
            Verify.IsNotNull(nameof(context), context);
            Verify.IsNotNull(nameof(actorKey), actorKey);
            context = context.WithTag(_tag);

            Type actorType = typeof(T);

            // Lookup instance of actor (type + actorKey)
            IActorRegistration actorRegistration = _actorRepository.Lookup(actorType, actorKey);

            if (actorRegistration != null)
            {
                return(actorRegistration.GetInstance <T>());
            }

            // Create actor (type manager or DI container)
            IActor actorObject = _typeManager.Create <T>(context, actorKey, this);

            //IActor actorObject = Configuration.Container != null ?
            //    Configuration.Container.Resolve<T>(new TypedParameter(typeof(ActorKey), actorKey), new TypedParameter(typeof(IActorManager), this)) :
            //    _typeManager.Create<T>(context, actorKey, this);

            IActorBase actorBase = actorObject as IActorBase;

            if (actorBase == null)
            {
                var ex = new ArgumentException($"Actor {actorObject.GetType().FullName} does not implement IActorBase");
                ActorEventSource.Log.Error(context, "Cannot create", ex);
                throw ex;
            }

            actorRegistration = new ActorRegistration(typeof(T), actorKey, actorBase, ActorProxy <T> .Create(actorBase, this));
            await _actorRepository.SetAsync(context, actorRegistration);

            // Create proxy for interface
            return(actorRegistration.GetInstance <T>());
        }