public void GetCacheKey_GivenSqlCommandWithoutParameters_ReturnsKey()
        {
            // arrange
            var command = new SqlCommand();
            command.CommandText = "SELECT * FROM table WHERE Column1 = 'AStringValue'";

            // act
            var key = command.GetCacheKey();

            // assert
            Assert.That(key, Is.EqualTo(command.CommandText));
        }
        public void GetCacheKey_GivenSqlCommandWithParameters_ReturnsKey()
        {
            // arrange
            var command = new SqlCommand();
            command.CommandText = "SELECT * FROM table WHERE Column1 = @Parameter1 AND Column2 = @Parameter2";

            command.Parameters.Add(new SqlParameter("@Parameter1", "AStringValue"));
            command.Parameters.Add(new SqlParameter("@Parameter2", 12));

            var expected = "SELECT * FROM table WHERE Column1 = @Parameter1 AND Column2 = @Parameter2 -- {@Parameter1: AStringValue} {@Parameter2: 12}";

            // act
            var key = command.GetCacheKey();

            // assert
            Assert.That(key, Is.EqualTo(expected));
        }
        public DbDataReader GetDataReader(SqlCommand command, Func<DbDataReader> query)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var key = command.GetCacheKey();
            if (storage.ContainsKey(key))
            {
                return (DbDataReader)storage.Get(key);
            }

            CacheableDataReader cacheableReader = null;
            try
            {
                var results = query();
                cacheableReader = new CacheableDataReader(results);
                storage.Add(key, cacheableReader);

                return cacheableReader;
            }
            catch
            {
                if (cacheableReader != null)
                {
                    cacheableReader.Dispose();
                }

                throw;
            }
        }