/// <summary>
        /// 将容器中所有 ICommand 对象实例化并缓存到字典
        /// </summary>
        public void Pool()
        {
            // 获取所有执行过注入后的 ICommand 对象实例
            var resolvedCommands = container.ResolveAll <ICommand>();

            for (var i = 0; i < resolvedCommands.Length; i++)
            {
                // 获取类型和实例
                var command     = resolvedCommands[i];
                var commandType = command.GetType();

                // 如果字典中已经有了就直接进行下次循环
                if (commands.ContainsKey(commandType))
                {
                    continue;
                }
                // 如果是单例类型就直接进行添加
                if (command.singleton)
                {
                    commands.Add(commandType, command);
                }
                else
                {
                    // 否则用 list 来作为对象池
                    var commandPool = new List <ICommand>(command.preloadPoolSize);

                    // 将当前元素添加到 list 中
                    commandPool.Add(command);

                    // 如果对象池初始化数量大于1就继续进行实例化并添加到 list
                    if (command.preloadPoolSize > 1)
                    {
                        for (int n = 1; n < command.preloadPoolSize; n++)
                        {
                            commandPool.Add((ICommand)container.Resolve(commandType));
                        }
                    }
                    // 将 list 添加到字典
                    commands.Add(commandType, commandPool);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Pools all commands.
        /// </summary>
        public void Pool()
        {
            var resolvedCommands = container.ResolveAll <ICommand>();

            for (var cmdIndex = 0; cmdIndex < resolvedCommands.Length; cmdIndex++)
            {
                var command     = resolvedCommands[cmdIndex];
                var commandType = command.GetType();

                //If the type already exists in the pool, goes to the next type.
                if (this.commands.ContainsKey(commandType))
                {
                    continue;
                }

                if (command.singleton)
                {
                    this.commands.Add(commandType, command);
                }
                else
                {
                    var commandPool = new List <ICommand>(command.preloadPoolSize);

                    //Adds the currently resolved command.
                    commandPool.Add(command);

                    //Adds other commands until matches preloadPoolSize.
                    if (command.preloadPoolSize > 1)
                    {
                        for (int itemIndex = 1; itemIndex < command.preloadPoolSize; itemIndex++)
                        {
                            commandPool.Add((ICommand)container.Resolve(commandType));
                        }
                    }

                    this.commands.Add(commandType, commandPool);
                }
            }
        }
 public IEnumerable <object> GetServices(Type serviceType)
 {
     return(_container.ResolveAll(serviceType));
 }