Ejemplo n.º 1
0
        Tutorial_01_Implicit_JobApplicant()
        {
            //----------------------------------------------------------------------------------------------------------
            // Create the Stash Client.
            // Our helper class offers various flavors of this.

            StashClient <JobApplicant>
            client = StashConfiguration.GetClient <JobApplicant>();

            //----------------------------------------------------------------------------------------------------------
            // Create the corresponding table or confirm that it exists
            // The table name can be inferred using various methods. In this case it is inferred from the class name,
            // 'JobApplicant'.

            client.CreateTableIfNotExist();

            //----------------------------------------------------------------------------------------------------------
            // Create an instance of the class we want to Stash

            JobApplicant
                dataWritten = new JobApplicant {
                PartitionKey = "A",
                RowKey       = Guid.NewGuid().ToString(),
                Name         = "John Doe",
                Skill_Level  = 1
            };

            //----------------------------------------------------------------------------------------------------------
            // Stash it

            client.Insert(dataWritten);

            //----------------------------------------------------------------------------------------------------------
            // Read back the data, using the partition and row key, no need to use LINQ here.

            JobApplicant
                dataRead = client.Get(dataWritten.PartitionKey, dataWritten.RowKey);

            //----------------------------------------------------------------------------------------------------------
            // Verify we got back what we put in

            Assert.IsTrue(dataWritten.Equals(dataRead));

            //----------------------------------------------------------------------------------------------------------
            // Lets change the SkillLevel and Update

            dataWritten.Skill_Level += 1;

            client.Update(dataWritten);

            //----------------------------------------------------------------------------------------------------------
            // Read back the data but this time lets use LINQ

            dataRead = client.CreateQuery()
                       .Where(imp => imp.PartitionKey == dataWritten.PartitionKey &&
                              imp.RowKey == dataWritten.RowKey)
                       .FirstOrDefault();

            //----------------------------------------------------------------------------------------------------------
            // Again verify that we got back what we put in

            Assert.IsTrue(dataWritten.Equals(dataRead));

            //----------------------------------------------------------------------------------------------------------
            // now delete the entity

            client.Delete(dataWritten);

            //----------------------------------------------------------------------------------------------------------
            // And verify that it was actually deleted
            // by attempting to read back the data

            var queryable = from imp in client.CreateQuery()
                            where   imp.PartitionKey == dataWritten.PartitionKey &&
                            imp.RowKey == dataWritten.RowKey
                            select imp;

            Assert.IsTrue(queryable.ToList().Count == 0);

            //----------------------------------------------------------------------------------------------------------
            // So far so good. This concludes this part of the tutorial. Go Stash!
        }
Ejemplo n.º 2
0
        Tutorial_03_Explicit_Employee()
        {
            //----------------------------------------------------------------------------------------------------------
            // Create the Stash Client.

            StashClient <Employee>
            client = StashConfiguration.GetClient <Employee>();

            //----------------------------------------------------------------------------------------------------------
            // Create the underlying table if it does not already exists

            client.CreateTableIfNotExist();

            //----------------------------------------------------------------------------------------------------------
            // Create an instance of the class

            const
            string departmentDev = "Dev";

            Employee
                dataWritten = new Employee {
                Department  = departmentDev,
                EmployeeId  = Guid.NewGuid().ToString(),
                Name        = "John Doe",
                SkillLevel  = 8,
                DateOfBirth = new DateTime(1990, 1, 1)
            };

            // update the private field
            dataWritten.SetAnnualSalary();

            //----------------------------------------------------------------------------------------------------------
            // Stash it

            client.Insert(dataWritten);

            // Note: On inserts, updates and merges, the ETag is overwritten from the ETag returned to keep the data
            // in sync.

            //----------------------------------------------------------------------------------------------------------
            // And read it back

            Employee
                dataRead = client.Get(dataWritten.Department, dataWritten.EmployeeId);

            //----------------------------------------------------------------------------------------------------------
            // Verify we got back what we put in

            Assert.IsTrue(dataWritten.Equals(dataRead));

            // Verify that the 2 ETags are identical
            Assert.IsTrue(dataRead.ETagInternal == dataWritten.ETagInternal);

            //----------------------------------------------------------------------------------------------------------
            // Change the Skill level and update the Stash

            dataWritten.SkillLevel += 1;

            dataWritten.SetAnnualSalary();

            client.Update(dataWritten);

            //----------------------------------------------------------------------------------------------------------
            // Read back the data but this time lets use LINQ

            Employee
                dataReadUpdated = client.CreateQuery()
                                  .Where(imp => imp.Department == departmentDev &&
                                         imp.EmployeeId == dataWritten.EmployeeId)
                                  .FirstOrDefault();

            //----------------------------------------------------------------------------------------------------------
            // Again verify that we got back what we put in

            Assert.IsTrue(dataWritten.Equals(dataReadUpdated));

            // Verify that the 2 ETags are identical
            Assert.IsTrue(dataWritten.ETagInternal == dataReadUpdated.ETagInternal);

            //----------------------------------------------------------------------------------------------------------
            // now attempt to update the data read the first time around
            // this should fail and throw an exception since the data was updated since the row was last read and
            // so has a stale ETag.
            // Uses the implied update mode here by default. That is, if the StashETag attribute is applied to a
            // member in the class, implies ETag must match,

            bool isSuccess;

            try
            {
                client.Update(dataRead);

                isSuccess = false;
            }
            catch (Exception ex)
            {
                Assert.IsTrue((ex as StashException).Error == StashError.ETagMatchFailed);

                isSuccess = true;
            }

            Assert.IsTrue(isSuccess);

            //----------------------------------------------------------------------------------------------------------
            // attempt to update it again but this time ignore ETag matching. This should succeed.
            // and dataRead has the latest ETag

            client.UpdateUnconditional(dataRead);

            //----------------------------------------------------------------------------------------------------------
            // now attempt to delete the entity using the data written entity
            // this should fail and throw an exception since the data was recently updated and so has a new ETag
            // Uses the implied update mode here by default. That is, if the StashETag attribute is applied to a
            // member in the class, implies ETag must match,

            try
            {
                client.Delete(dataReadUpdated);

                isSuccess = false;
            }
            catch (Exception ex)
            {
                Assert.IsTrue((ex as StashException).Error == StashError.ETagMatchFailed);

                isSuccess = true;
            }

            Assert.IsTrue(isSuccess);

            //----------------------------------------------------------------------------------------------------------
            // now delete the entity unconditionally without the need for ETag Matching.
            // Can instead also use client.DeleteUnconditional here too.

            client.Delete(dataRead, ETagMatch.Unconditional);

            //----------------------------------------------------------------------------------------------------------
            // And verify that it was actually deleted
            // by attempting to read back the data

            var queryable = from imp in client.CreateQuery()
                            where       imp.Department == dataWritten.Department &&
                            imp.EmployeeId == dataWritten.EmployeeId
                            select imp;

            Assert.IsTrue(queryable.ToList().Count == 0);

            //----------------------------------------------------------------------------------------------------------
            // Essentially the ETag gist is this.
            // Define and decorate a member in your type with an ETag if you want to optimistic concurrency support.
            //   Stash will keep you ETag in sync across inserts, updates and merges.
            //   There are multiple ways to implicitly override ETag matching if that is what is wanted.
            // The implicit way to disable optimistic concurrency support is not to define an ETag member.

            //----------------------------------------------------------------------------------------------------------
            // So far so good. This concludes this part of the tutorial. Go Stash!
        }
Ejemplo n.º 3
0
        Tutorial_05_Morphing_EmployeeInfo()
        {
            //----------------------------------------------------------------------------------------------------------
            // Create a Stash Client

            StashClient <EmployeeInfo>
            client = StashConfiguration.GetClient <EmployeeInfo>();

            client.CreateTableIfNotExist();

            //----------------------------------------------------------------------------------------------------------
            // Create an instance of the class we what to Stash

            const
            string departmentDev = "Dev";

            const
            string ssn = "123456789";

            EmployeeInfo
                dataWritten = new EmployeeInfo {
                Department  = departmentDev,
                EmployeeId  = Guid.NewGuid().ToString(),
                Name        = "John Doe",
                SkillLevel  = 8,
                DateOfBirth = new DateTime(1990, 1, 1),
                SSN         = ssn,
                Salutation  = Salutation.Dr
            };

            // update the private field
            dataWritten.SetAnnualSalary();

            //----------------------------------------------------------------------------------------------------------
            // Stash it

            client.Insert(dataWritten);

            //----------------------------------------------------------------------------------------------------------
            // Read back the data

            EmployeeInfo
                dataRead = client.Get(dataWritten.Department, dataWritten.EmployeeId);

            //----------------------------------------------------------------------------------------------------------
            // Verify we got back what we put in

            Assert.IsTrue(dataWritten.Equals(dataRead));

            //----------------------------------------------------------------------------------------------------------
            // Now let us verify exactly what was written to table storage by using the Generic Pool class we wrote
            // earlier.

            // set this up to target the correct table storage entity set
            StashClient <GenericPool>
            clientGenericPool = StashConfiguration.GetClient <GenericPool>(
                new StashClientOptions {
                OverrideEntitySetName          = o => "EmployeeInfo",
                OverrideEntitySetNameIsDynamic = false,
                Feedback = StashConfiguration.TraceFeedback,
            });


            GenericPool
                dataPool = clientGenericPool.Get(
                dataRead.Department,
                dataRead.EmployeeId);

            // validate that the SSN value got morphed in to a byte[]
            Assert.IsTrue(dataPool.Pool["SSN"].GetType() == typeof(byte[]));

            //----------------------------------------------------------------------------------------------------------
            // Change the Skill level and update the Stash

            dataWritten.SkillLevel += 1;

            dataWritten.SetAnnualSalary();

            // force an unconditional update since, dataWritten does not have a ETag.
            // Optionally copy over the ETag from dataRead.
            client.Update(dataWritten, ETagMatch.Unconditional);

            //----------------------------------------------------------------------------------------------------------
            // Read back the data but this time lets use LINQ

            dataRead = client.CreateQuery()
                       .Where(imp => imp.Department == departmentDev &&
                              imp.EmployeeId == dataWritten.EmployeeId)
                       .FirstOrDefault();

            //----------------------------------------------------------------------------------------------------------
            // Again verify that we got back what we put in

            Assert.IsTrue(dataWritten.Equals(dataRead));

            //----------------------------------------------------------------------------------------------------------
            // now delete the entity

            client.Delete(
                departmentDev,
                dataWritten.EmployeeId);

            //----------------------------------------------------------------------------------------------------------
            // And verify that it was actually deleted
            // by attempting to read back the data

            var queryable = from imp in client.CreateQuery()
                            where   imp.Department == dataWritten.Department &&
                            imp.EmployeeId == dataWritten.EmployeeId
                            select imp;

            Assert.IsTrue(queryable.ToList().Count == 0);

            //----------------------------------------------------------------------------------------------------------
            // So far so good. This concludes this part of the tutorial. Go Stash!
        }
Ejemplo n.º 4
0
        Tutorial_04_DictionaryPool()
        {
            //----------------------------------------------------------------------------------------------------------
            // Create a Stash Client for the Employee class

            StashClient <Employee>
            clientEmployee = StashConfiguration.GetClient <Employee>();

            clientEmployee.CreateTableIfNotExist();

            //----------------------------------------------------------------------------------------------------------
            // Create an StashClient for the generic pool class,
            // Since we want to read and write to the Employee table, notice how we setup the correct EntityName in
            // the StashClientOptions. Otherwise the azure table implied would be "GenericPool".
            //
            // The call back method of naming the table has flexible capabilities. It can be used to determine
            // the table name dynamically based on the data in the object instance.
            // In this case it is used statically and called only once.

            StashClient <GenericPool>
            clientGenericPool = StashConfiguration.GetClient <GenericPool>(
                new StashClientOptions {
                OverrideEntitySetName          = o => "Employee",
                OverrideEntitySetNameIsDynamic = false,
                Feedback = StashConfiguration.TraceFeedback,
            });

            //----------------------------------------------------------------------------------------------------------
            // Stash an Employee

            const
            string departmentDev = "Dev";

            Employee
                dataEmployee = new Employee {
                Department  = departmentDev,
                EmployeeId  = Guid.NewGuid().ToString(),
                Name        = "John Doe",
                SkillLevel  = 8,
                DateOfBirth = new DateTime(1990, 1, 1)
            };

            dataEmployee.SetAnnualSalary();

            clientEmployee.Insert(dataEmployee);

            //----------------------------------------------------------------------------------------------------------
            // and read it back thru the GenericPool

            GenericPool
                dataPool = clientGenericPool.Get(
                dataEmployee.Department,
                dataEmployee.EmployeeId);

            //----------------------------------------------------------------------------------------------------------
            // Verify we got back what we put in via the Employee type.

            Assert.IsTrue(
                dataPool.PrimaryKey == dataEmployee.Department &&
                dataPool.SecondaryKey == dataEmployee.EmployeeId &&
                dataPool.Pool["AnnualSalary"].Equals(dataEmployee.GetAnnualSalary()) &&
                dataPool.Pool["Birthday"].Equals(dataEmployee.DateOfBirth) &&
                dataPool.Pool["SkillLevel"].Equals(dataEmployee.SkillLevel) &&
                dataPool.Pool["Name"].Equals(dataEmployee.Name) &&
                dataPool.Pool.Count == 4 + 1);                                                                  // + 1 to the ETag

            //----------------------------------------------------------------------------------------------------------
            // Make a few changes and update

            dataPool.Pool["Name"]   = "Lucifure";                               // name change
            dataPool.Pool["Unique"] = Guid.NewGuid();                           // new field

            clientGenericPool.Update(dataPool);

            // Note: Updates, Merges etc keep the original objects in sync with the latest ETags.
            // Even if an ETag is not specified for the type, if the type supports a Pool,
            // the ETag is maintained and synched in the pool

            //----------------------------------------------------------------------------------------------------------
            // Read back the data

            GenericPool
                dataPoolRead = clientGenericPool.CreateQuery()
                               .Where(imp => imp.PrimaryKey == dataEmployee.Department &&
                                      imp.SecondaryKey == dataEmployee.EmployeeId)
                               .FirstOrDefault();

            //----------------------------------------------------------------------------------------------------------
            // and verify that we got back what we put in

            Assert.IsTrue(
                dataPool.PrimaryKey == dataPoolRead.PrimaryKey &&
                dataPool.SecondaryKey == dataPoolRead.SecondaryKey &&
                StashHelper.DictionaryEquals(
                    dataPool.Pool,
                    dataPoolRead.Pool));

            //----------------------------------------------------------------------------------------------------------
            // Merge can be performed very efficiently with a StashPool. Only include the tables properties
            // to be merge in the dictionary. No need to null out members or defined nullable value type members.

            // Create a new pool with just the objects of interest
            Dictionary <string, object>
            pool = new Dictionary <string, object>();

            // save the current etag (for now and later)
            string
                etagPreMerge = dataPoolRead.Pool[Literal.ETag].ToString();

            pool["Name"]       = "Stash";                                                                               // new value to merge
            pool[Literal.ETag] = etagPreMerge;                                                                          // get the ETag too

            // create a new pool object for merging.
            GenericPool
                dataPoolMerged = new GenericPool {
                PrimaryKey   = dataPoolRead.PrimaryKey,
                SecondaryKey = dataPoolRead.SecondaryKey,
                Pool         = pool
            };

            clientGenericPool.Merge(dataPoolMerged, ETagMatch.Must);                            // force ETag matching.

            // read back and verify
            GenericPool
                dataPoolMergedRead = clientGenericPool.Get(
                dataPoolMerged.PrimaryKey,
                dataPoolMerged.SecondaryKey);

            // validate
            Assert.IsTrue(dataPoolMergedRead.Pool.Count == dataPoolRead.Pool.Count &&
                          dataPoolMergedRead.Pool["Name"] as String == "Stash");

            //----------------------------------------------------------------------------------------------------------
            // attempt to merge again, replacing the current ETag with the old one.

            bool isSuccess;

            try
            {
                // use the defunct etag to exhibit this
                dataPoolMerged.Pool[Literal.ETag] = etagPreMerge;

                clientGenericPool.Merge(dataPoolMerged);

                isSuccess = false;
            }
            catch (Exception ex)
            {
                // Merge fails because the ETag was old and did not match.
                Assert.IsTrue((ex as StashException).Error == StashError.ETagMatchFailed);

                isSuccess = true;
            }

            Assert.IsTrue(isSuccess);

            //----------------------------------------------------------------------------------------------------------
            // attempt to merge yet again, this time by disabling ETag matching.

            clientGenericPool.Merge(dataPoolMerged, ETagMatch.Unconditional);

            //----------------------------------------------------------------------------------------------------------
            // now attempt to delete the entity

            try
            {
                clientGenericPool.Delete(dataPoolMergedRead);                           // Implicit ETag matching, will make this fail too
                // because the last update changed the ETag
                isSuccess = false;
            }
            catch (Exception ex)
            {
                Assert.IsTrue((ex as StashException).Error == StashError.ETagMatchFailed);

                isSuccess = true;
            }

            Assert.IsTrue(isSuccess);

            //----------------------------------------------------------------------------------------------------------
            // Do an unconditional delete

            clientGenericPool.DeleteUnconditional(dataPoolMergedRead);

            //----------------------------------------------------------------------------------------------------------
            // And verify that it was actually deleted
            // by attempting to read back the data

            var queryable = from emp in clientEmployee.CreateQuery()
                            where   emp.Department == dataEmployee.Department &&
                            emp.EmployeeId == dataEmployee.EmployeeId
                            select emp;

            Assert.IsTrue(queryable.ToList().Count == 0);

            //----------------------------------------------------------------------------------------------------------
            // So far so good. This concludes this part of the tutorial. Go Stash!
        }
Ejemplo n.º 5
0
        Tutorial_02_Hybrid_TempHire()
        {
            //----------------------------------------------------------------------------------------------------------
            // Create the Stash Client.

            StashClient <TempHire>
            client = StashConfiguration.GetClient <TempHire>();

            //----------------------------------------------------------------------------------------------------------
            // Create a table with the class name ('TempEmployee') or confirm that it exists

            client.CreateTableIfNotExist();

            //----------------------------------------------------------------------------------------------------------
            // Create an instance of the class we what to Stash

            TempHire
                dataWritten = new TempHire {
                PartitionKey = "B",
                RowKey       = Guid.NewGuid().ToString(),
                Name         = "Jill Doe",
                Skill_Level  = 5,
                DateOfBirth  = new DateTime(1990, 1, 1)
            };

            // update the private field
            dataWritten.SetHourlyPay();

            //----------------------------------------------------------------------------------------------------------
            // Stash it

            client.Insert(dataWritten);

            //----------------------------------------------------------------------------------------------------------
            // Read back the data

            TempHire
                dataRead = client.Get(dataWritten.PartitionKey, dataWritten.RowKey);

            //----------------------------------------------------------------------------------------------------------
            // Verify we got back what we put in

            Assert.IsTrue(dataWritten.Equals(dataRead));

            //----------------------------------------------------------------------------------------------------------
            // Lets change the Skill level and update the Stash

            dataWritten.Skill_Level += 1;

            dataWritten.SetHourlyPay();

            client.Update(dataWritten);

            //----------------------------------------------------------------------------------------------------------
            // Read back the data but this time lets use LINQ

            dataRead = client.CreateQuery()
                       .Where(imp => imp.PartitionKey == dataWritten.PartitionKey &&
                              imp.RowKey == dataWritten.RowKey)
                       .FirstOrDefault();

            //----------------------------------------------------------------------------------------------------------
            // Again verify that we got back what we put in

            Assert.IsTrue(dataWritten.Equals(dataRead));

            //----------------------------------------------------------------------------------------------------------
            // now delete the entity

            client.Delete(
                dataWritten.PartitionKey,
                dataWritten.RowKey);

            //----------------------------------------------------------------------------------------------------------
            // And verify that it was actually deleted
            // by attempting to read back the data

            var queryable = from imp in client.CreateQuery()
                            where       imp.PartitionKey == dataWritten.PartitionKey &&
                            imp.RowKey == dataWritten.RowKey
                            select imp;

            Assert.IsTrue(queryable.ToList().Count == 0);

            //----------------------------------------------------------------------------------------------------------
            // So far so good. This concludes this part of the tutorial. Go Stash!
        }