Ejemplo n.º 1
0
        /// <summary>
        /// Constructor with the intialSize and maxSize. <see cref="ArgumentException"/> is thrown when <paramref name="initialSize"/> is larger than <paramref name="maxSize"/>
        /// <see cref="IPooledObjectFactory{T}.Create"/> is called to create objects with the number of <paramref name="initialSize"/>. If it returns the object with null, it is not
        /// added to the pool.
        /// </summary>
        /// <param name="initialSize">The initial object number of the pool.</param>
        /// <param name="maxSize">The max object number of the pool.</param>
        /// <param name="factory">The factory create and destroy the pooled object.</param>
        /// <param name="validator">Validator instance. Can be <see langword="null"/>.</param>
        public GenericObjectPool(int initialSize, int maxSize, IPooledObjectFactory <T> factory, IPooledObjectValidator <T> validator, int acquiredInvalidLimit)
        {
            if (initialSize < 0)
            {
                throw new ArgumentException("The pool initial size is invalid");
            }
            if (maxSize != -1 && maxSize < initialSize)
            {
                throw new ArgumentException("The pool max size is invalid");
            }
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            this.initialSize          = initialSize;
            this.maxSize              = maxSize;
            this.factory              = factory;
            this.validator            = validator ?? new NeverValidateValidator();
            this.acquiredInvalidLimit = acquiredInvalidLimit;

            createdCount   = 0;
            objectReturned = new AutoResetEvent(false);
            locker         = new ReaderWriterLockSlim();
            objQueue       = new ConcurrentQueue <T>();
            idleObjects    = new Dictionary <T, bool>();
            for (var i = 0; i < initialSize; i++)
            {
                objQueue.Enqueue(factory.Create());
                createdCount++;
            }
        }
 public IPoolBuilder <T> WithValidator(IPooledObjectValidator <T> validator)
 {
     this.validator = validator;
     return(this);
 }
Ejemplo n.º 3
0
 public IPoolDescriptor <T> WithValidator(IPooledObjectValidator <T> validator)
 {
     this.validator = validator;
     return(this);
 }
Ejemplo n.º 4
0
 public void ValidateWith(IPooledObjectValidator <T> validator)
 {
     ObjectValidator = validator;
 }