/// <summary> /// Get actor, if already exist, will return or create new one /// </summary> /// <typeparam name="T">actor type</typeparam> /// <param name="actorKey">actor key</param> /// <returns>actor instance</returns> public T GetActor <T>(ActorKey actorKey) where T : IActor { actorKey.VerifyNotNull(nameof(actorKey)); Type actoryType = typeof(T); lock (_lock) { if (_actorCollection.TryGetValue(actoryType, actorKey, out ActorInstance actorInstance)) { return((T)actorInstance.GetInstance <T>()); } // Create actor object IActorBase actorBase = _registry.Create <T>(actorKey, this); // Create proxy T actorInterface = ActorProxy <T> .Create(actorBase, this); actorInstance = new ActorInstance(typeof(T), actorKey, actorBase, actorInterface); // Add to actor collection _actorCollection.Set(actorInstance); // Create proxy for interface return(actorInstance.GetInstance <T>()); } }
/// <summary> /// Get actor, if already exist, will return or create new one /// </summary> /// <typeparam name="T">actor type</typeparam> /// <param name="actorKey">actor key</param> /// <returns>actor instance</returns> public T GetActor <T>(ActorKey actorKey) where T : IActor { actorKey.VerifyNotNull(nameof(actorKey)); Type actoryType = typeof(T); lock (_lock) { if (_actorCollection.TryGetValue(actoryType, actorKey, out ActorInstance actorInstance)) { _logger.LogInformation($"{nameof(GetActor)}: found instance, actorKey={actorKey}, actorInstance.ActorKey={actorInstance.ActorKey}"); return((T)actorInstance.GetInstance <T>()); } // Create actor object IActorBase actorBase = _registry.Create <T>(actorKey, this); (actorKey == actorBase.ActorKey).VerifyAssert(x => x, $"{nameof(GetActor)}: CREATED-Error Actor key !=, actorKey={actorKey}, actorBase.ActorKey={actorBase.ActorKey}"); _logger.LogTrace($"{nameof(GetActor)}: CREATED actorBase.ActorKey={actorBase.ActorKey}"); // Create proxy T actorInterface = ActorProxy <T> .Create(actorBase, this); actorInstance = new ActorInstance(typeof(T), actorKey, actorBase, actorInterface); // Add to actor collection _actorCollection.Set(actorInstance); // Create proxy for interface return(actorInstance.GetInstance <T>()); } }