Example #1
0
        /// <summary>
        /// Create actor from either lambda or activator
        /// </summary>
        /// <typeparam name="T">actor interface</typeparam>
        /// <param name="context">context</param>
        /// <param name="actorKey">actor key</param>
        /// <param name="actorHost">actor manager</param>
        /// <returns>instance of actor implementation</returns>
        public IActorBase Create <T>(ActorKey actorKey, ActorHost actorHost) where T : IActor
        {
            actorKey.VerifyNotNull(nameof(actorKey));
            actorHost.VerifyNotNull(nameof(actorHost));

            Type actorType = typeof(T)
                             .VerifyAssert(x => x.IsInterface, $"{typeof(T)} must be an interface");

            ActorRegistration typeRegistration = GetTypeRegistration(actorType);

            IActor actorObject = typeRegistration.CreateImplementation();

            // Set actor key and manager
            ActorBase?actorBase = actorObject as ActorBase;

            if (actorBase == null)
            {
                string failureMsg = $"Created actor type {actorObject.GetType()} does not derive from ActorBase";
                _logger.LogError(failureMsg);
                throw new InvalidOperationException(failureMsg);
            }

            actorBase.ActorKey  = actorKey;
            actorBase.ActorHost = actorHost;
            actorBase.ActorType = actorType;

            return((IActorBase)actorObject);
        }
Example #2
0
        public void Register(Type type, Func <IActor> createImplementation)
        {
            type
            .VerifyNotNull(nameof(type))
            .IsInterface.VerifyAssert(x => x == true, $"{type} must be an interface");

            createImplementation.VerifyNotNull(nameof(createImplementation));

            _typeRegistry[type] = new ActorRegistration(type, createImplementation);
        }