Пример #1
0
        protected internal TablePartition(CloudStorageAccount storageAccount, string tableName, string partitionKey, Expression <Func <T, string> > rowKey)
        {
            TableName = tableName ?? TablePartition.GetDefaultTableName <T>();
            partitionKey ??= TablePartition.GetDefaultPartitionKey <T>();
            PartitionKey = partitionKey;

            repository = new TableRepository <T>(storageAccount,
                                                 TableName,
                                                 _ => partitionKey,
                                                 rowKey ?? RowKeyAttribute.CreateAccessor <T>());
        }
Пример #2
0
        public async Task CanGetAll()
        {
            var account = CloudStorageAccount.DevelopmentStorageAccount;
            var repo    = TableRepository.Create <Book>(account, nameof(CanGetAll), x => x.Author, x => x.ISBN);

            await LoadBooksAsync(repo);

            var result = await repo.CreateQuery().AsAsyncEnumerable().ToListAsync();

            var all = await repo.EnumerateAsync().ToListAsync();

            Assert.Equal(all.Count, result.Count);
        }
Пример #3
0
        public static IDocumentRepository <T> Create <T>(
            CloudStorageAccount storageAccount,
            string?tableName = default,
            Func <T, string>?partitionKey  = default,
            Func <T, string>?rowKey        = default,
            IDocumentSerializer?serializer = default) where T : class
        {
            tableName ??= TableRepository.GetDefaultTableName <T>();
            partitionKey ??= PartitionKeyAttribute.CreateCompiledAccessor <T>();
            rowKey ??= RowKeyAttribute.CreateCompiledAccessor <T>();
            serializer ??= DocumentSerializer.Default;

            return(new DocumentRepository <T>(storageAccount, tableName, partitionKey, rowKey, serializer));
        }
Пример #4
0
        public async Task CanTake()
        {
            var account = CloudStorageAccount.DevelopmentStorageAccount;
            var repo    = TableRepository.Create <Book>(account, nameof(CanTake), x => "Book", x => x.ISBN);

            await LoadBooksAsync(repo);

            var query = from book in repo.CreateQuery()
                        where book.Format == BookFormat.Hardback && book.IsPublished
                        select new { book.ISBN, book.Title };

            var result = await query.Take(2).AsAsyncEnumerable().ToListAsync();

            Assert.Equal(2, result.Count);
        }
Пример #5
0
        public async Task CanFilterByColumn()
        {
            var account = CloudStorageAccount.DevelopmentStorageAccount;

            await LoadBooksAsync(TableRepository.Create <Book>(account, nameof(CanFilterByColumn), x => x.Author, x => x.ISBN));

            var repo = TablePartition.Create <Book>(account, nameof(CanFilterByRowKey), "Rick Riordan", x => x.ISBN);

            // Get specific set of books from one particular publisher/country combination
            // in this case, 978-[English-speaking country, 1][Disney Editions, 4231]
            // See https://en.wikipedia.org/wiki/List_of_group-1_ISBN_publisher_codes
            var query = from book in repo.CreateQuery()
                        where book.Pages >= 1000 && book.Pages <= 1500
                        select new { book.ISBN, book.Title };

            var result = await query.AsAsyncEnumerable().ToListAsync();

            Assert.Single(result);
        }
Пример #6
0
        public async Task RunAsync()
        {
            var repo = TableRepository.Create <Product>(
                CloudStorageAccount.DevelopmentStorageAccount,
                tableName: "Products",
                partitionKey: p => p.Category,
                rowKey: p => p.Id);

            await repo.PutAsync(new Product("book", "9781473217386")
            {
                Title = "Neuromancer",
                Price = 7.32
            });

            var docs = DocumentRepository.Create <Product>(
                CloudStorageAccount.DevelopmentStorageAccount,
                tableName: "Documents",
                partitionKey: p => p.Category,
                rowKey: p => p.Id);

            await docs.PutAsync(new Product("book", "9781473217386")
            {
                Title = "Neuromancer",
                Price = 7.32
            });

            var bin = DocumentRepository.Create <Product>(
                CloudStorageAccount.DevelopmentStorageAccount,
                tableName: "Documents",
                partitionKey: p => p.Category,
                rowKey: p => p.Id,
                serializer: BsonDocumentSerializer.Default);

            await docs.PutAsync(new Product("book", "9781473217386")
            {
                Title = "Neuromancer",
                Price = 7.32
            });
        }
Пример #7
0
        public async Task BooksAsync()
        {
            var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;

            // We lay out the parameter names for clarity only.
            var repo = TableRepository.Create <Book>(storageAccount,
                                                     tableName: "Books",
                                                     partitionKey: p => p.Author,
                                                     rowKey: p => p.ISBN);

            //await LoadBooksAsync(repo);

            // Create a new Book and save it
            await repo.PutAsync(
                new Book("9781473217386", "Neuromancer", "William Gibson", BookFormat.Paperback, 320));

            await foreach (var book in from b in repo.CreateQuery()
                           where b.Author == "William Gibson" && b.Format == BookFormat.Paperback
                           select new { b.Title })
            {
                Console.WriteLine(book.Title);
            }
        }
Пример #8
0
        public async Task CanProjectPartition()
        {
            var account = CloudStorageAccount.DevelopmentStorageAccount;

            // Load with author + isbn as keys
            await LoadBooksAsync(TableRepository.Create <Book>(account, nameof(CanProjectPartition), x => x.Author, x => x.ISBN));

            // Query single author by scoping to partition key
            var repo = TablePartition.Create <Book>(account, nameof(CanProjectPartition), "Rick Riordan", x => x.ISBN);

            var hasResults = false;

            await foreach (var info in from book in repo.CreateQuery()
                           where book.Format == BookFormat.Hardback && book.IsPublished
                           select new { book.ISBN, book.Title })
            {
                hasResults = true;
                Assert.NotNull(info.ISBN);
                Assert.NotNull(info.Title);
            }

            Assert.True(hasResults);
        }