public async ValueTask WorkWithCachedSpecification() { const string dbName = "FakeDatabase"; using (TrainingDb dbContext = this.testFixture.CreateInMemoryDbContext(dbName)) { dbContext.Database.EnsureCreated(); IOptions <MemoryCacheOptions> memCacheOptions = Options.Create <MemoryCacheOptions>(new MemoryCacheOptions()); var memoryCache = new MemoryCache(memCacheOptions); IReadonlyRepository <Login> repo = new CachedRepository <Login, TrainingDb>(dbContext, memoryCache); ISpecification <Login> spec = this.specificationFactories.For <Login>() .Where(l => l.Id == 1) .Include(l => l.Student) .Not() .AsCached(TimeSpan.FromHours(12), 1); System.Collections.Generic.IEnumerable <Login> logins = await repo.ListAsync(spec); logins.Should().HaveCount(2); logins.Select(l => l.Student).Should().NotBeNull(); // Let's create a new repository instance with the same cache // I would expect in this case that the repository returns the same instances // if they have been retrieved from the cache. repo = new CachedRepository <Login, TrainingDb>(dbContext, memoryCache); System.Collections.Generic.IEnumerable <Login> cachedLogins = await repo.ListAsync(spec); cachedLogins.Should().HaveCount(2); cachedLogins.Should().BeSameAs(logins); } }