Exemple #1
0
        public void ObjectPoolAdapterForPooledObject_GetAndReturnObject_SameInstance()
        {
            // Arrange
            var pool = ObjectPoolAdapter.CreateForPooledObject(new ObjectPool <MyPooledObject>());

            var pooledObject1 = pool.Get();

            pool.Return(pooledObject1);

            // Act
            var pooledObject2 = pool.Get();

            // Assert
            pooledObject1.ShouldBe(pooledObject2);
        }
Exemple #2
0
        /// <summary>
        ///   Example usages of ObjectPool.
        /// </summary>
        private static void Main()
        {
            // Creating a pool with a maximum size of 25, using custom Factory method to create and
            // instance of ExpensiveResource.
            var pool = new ObjectPool <ExpensiveResource>(25, () => new ExpensiveResource(/* resource specific initialization */));

            using (var resource = pool.GetObject())
            {
                // Using the resource...
                resource.DoStuff();
            } // Exiting the using scope will return the object back to the pool.

            // Creating a pool with wrapper object for managing external resources, that is, classes
            // which cannot inherit from PooledObject.
            var newPool = new ObjectPool <PooledObjectWrapper <ExternalExpensiveResource> >(() =>
                                                                                            new PooledObjectWrapper <ExternalExpensiveResource>(CreateNewResource())
            {
                OnReleaseResources = ExternalResourceReleaseResource,
                OnResetState       = ExternalResourceResetState
            });

            using (var wrapper = newPool.GetObject())
            {
                // wrapper.InternalResource contains the object that you pooled.
                wrapper.InternalResource.DoOtherStuff();
            } // Exiting the using scope will return the object back to the pool.

            // Creates a pool where objects which have not been used for over 2 seconds will be
            // cleaned up by a dedicated thread.
            var timedPool = new TimedObjectPool <ExpensiveResource>(TimeSpan.FromSeconds(2));

            using (var resource = timedPool.GetObject())
            {
                // Using the resource...
                resource.DoStuff();
            } // Exiting the using scope will return the object back to the pool and record last usage.

            Console.WriteLine($"Timed pool size after 0 seconds: {timedPool.ObjectsInPoolCount}"); // Should be 1
            Thread.Sleep(TimeSpan.FromSeconds(4));
            Console.WriteLine($"Timed pool size after 4 seconds: {timedPool.ObjectsInPoolCount}"); // Should be 0

            // Adapts a timed pool to Microsoft Extensions abstraction.
            var mPool = ObjectPoolAdapter.CreateForPooledObject(timedPool);

            // Sample usage of Microsoft pool.
            var mResource = mPool.Get();

            Debug.Assert(mResource is ExpensiveResource);
            mPool.Return(mResource);

            // Adapts a new pool to Microsoft Extensions abstraction. This example shows how to adapt
            // when object type does not extend PooledObject.
            var mPool2 = ObjectPoolAdapter.Create(new ObjectPool <PooledObjectWrapper <MemoryStream> >(() => PooledObjectWrapper.Create(new MemoryStream())));

            // Sample usage of second Microsoft pool.
            var mResource2 = mPool2.Get();

            Debug.Assert(mResource2 is MemoryStream);
            mPool2.Return(mResource2);

            Console.Read();
        }