コード例 #1
0
 public static IServiceCollection AddEntityTableClient <T>(this IServiceCollection services,
                                                           Action <EntityTableClientOptions> optionsAction,
                                                           Action <EntityTableConfig <T> > configAction)
     where T : class, new()
 {
     return(services.AddTransient(_ => EntityTableClient.Create(optionsAction, configAction)));
 }
コード例 #2
0
        public async Task Should_Set_Primary_Key_On_InsertOrUpdate()
        {
            var person = Fakers.CreateFakedPerson().Generate();

            person.AccountId = Guid.NewGuid().ToString();
            var tableEntity = EntityTableClient.Create <PersonEntity>(_optionsAction, c =>
            {
                c.SetPartitionKey(p => p.AccountId);
                c.SetPrimaryKey(p => p.PersonId);
            });

            await tableEntity.InsertOrReplaceAsync(person);

            var created = await tableEntity.GetByIdAsync(person.AccountId, person.PersonId);

            created.Should().BeEquivalentTo(person);
        }
コード例 #3
0
        public async Task Should_Gey_By_Indexed_Prop_With_Filter()
        {
            var partitionName = Guid.NewGuid().ToString();
            var persons       = Fakers.CreateFakedPerson().Generate(50);

            persons.ForEach(p => p.AccountId = partitionName);
            IEntityTableClient <PersonEntity> tableEntity = EntityTableClient.Create <PersonEntity>(_optionsAction
                                                                                                    ,
                                                                                                    c =>
            {
                c
                .SetPartitionKey(p => p.AccountId)
                .SetPrimaryKey(p => p.PersonId)
                .AddIndex(p => p.LastName)
                .AddIndex(p => p.Created);
            });
            await tableEntity.InsertOrReplaceAsync(persons);

            var person = persons.First();
            //get all entities both primary and projected
            var result = await tableEntity.GetByAsync(person.AccountId, p => p.Created, person.Created, filter : p => p.Where(p => p.Rank).Equal(person.Rank));

            result.Should().BeEquivalentTo(person);
        }
コード例 #4
0
        public static async Task Run()
        {
            var entityClient = EntityTableClient.Create <PersonEntity>(
                options =>
            {
                options
                .SetConnectionString(ConnectionString)
                .SetTableName($"{nameof(PersonEntity)}Table")
                .SetAutoCreateTable(true)
                .SetMaxItemsPerInsertion(1000)
                .SetMaxBatchedInsertionTasks(10);
            }

                , config =>
            {
                config

                //Partition key could be composed with any string based values
                .SetPartitionKey(p => p.AccountId)

                //Define an entity prop as primary key
                .SetPrimaryKey(p => p.PersonId)

                //Add additionnal indexes
                .AddIndex(p => p.LastName)
                .AddIndex(p => p.Distance)
                .AddIndex(p => p.Enabled)

                //props couldbe ignored (for both read and write operations)
                .AddIgnoredProp(p => p.Created)

                //Add computed props, computed on each updates.
                .AddComputedProp("_IsInFrance", p => (p.Address.State == "France"))
                .AddComputedProp("_MoreThanOneAddress", p => (p.OtherAddress.Count > 1))
                .AddComputedProp("_CreatedNext6Month", p => (p.Created > DateTimeOffset.UtcNow.AddMonths(-6)))
                .AddComputedProp("_FirstLastName3Chars", p => p.LastName.ToLower().Substring(0, 3))

                //Native props values could be overrided by computed props
                .AddComputedProp(nameof(PersonEntity.FirstName), p => p.FirstName.ToUpperInvariant())

                //Add index for any entity or computed props
                .AddIndex("_FirstLastName3Chars");
            });

            var faker = Fakers.CreateFakedPerson();

            Console.Write($"Generate faked {ENTITY_COUNT} entities...");
            Console.WriteLine("Ok");
            var persons = faker.Generate(ENTITY_COUNT);

            var counters = new PerfCounters(nameof(TableEntityBinderTests));

            using (var mesure = counters.Mesure($"{ENTITY_COUNT} insertions"))
            {
                await entityClient.InsertOrReplaceAsync(persons);
            }

            using (var mesure = counters.Mesure($"{ENTITY_COUNT} merged"))
            {
                await entityClient.InsertOrMergeAsync(persons);
            }

            counters.Clear();

            var person = persons.FirstOrDefault();

            using (var mesure = counters.Mesure("1. Get By Id"))
            {
                _ = await entityClient.GetByIdAsync(person.AccountId, person.PersonId);
            }

            using (var mesure = counters.Mesure("2. Get By LastName"))
            {
                _ = await entityClient.GetAsync(
                    person.AccountId,
                    w => w.Where(p => p.LastName).Equal(person.LastName));
            }

            using (var mesure = counters.Mesure("3. Get By LastName (indexed)"))
            {
                _ = await entityClient.GetByAsync(
                    person.AccountId,
                    p => p.LastName,
                    person.LastName);
            }

            using (var mesure = counters.Mesure("4. Get LastName start with 'arm'"))
            {
                _ = await entityClient.GetAsync(
                    person.AccountId,
                    w => w.Where("_FirstLastName3Chars").Equal("arm"));
            }

            using (var mesure = counters.Mesure("5. Get by LastName start with 'arm' (indexed)"))
            {
                _ = await entityClient.GetByAsync(
                    person.AccountId,
                    "_FirstLastName3Chars", "arm");
            }

            Console.WriteLine("====================================");
            foreach (var counter in counters.Get().OrderBy(c => c.Key))
            {
                WriteLineDuration($"{counter.Key} ", counter.Value);
            }
            Console.WriteLine("====================================");

            //Iterate all entities from the table without memory pressure

            //long count = 0;
            //Console.WriteLine("Get all entities");
            //await foreach (var page in entityClient.GetAllAsync())
            //{
            //    count += page.Count();
            //    Console.WriteLine($"{count} entities readed");
            //}
        }