CIPl Versioning

This is a brief overview of how CIPl handles versioing of items in a CIPl collection (as opposed to versioning of CIPl itself, or versioning of schemas)

CIPl will version instances of a type if the type has a property of type CIP4ItemVersionsType. For example
       [CIP4Property(DontSerialize = true)]
       public CIP4ItemVersionsType Versions { get; set; }

You can give the property any name you like.

When you store an instance of such a type using the StrongWrapper Save function, CIPl will compute a set of values that indicate how the instance differs from previous versions. When you create and save an item for the first time, there will be no differences. When you get an object, the state of the object at get time will be saved (i.e. CIPl makes a copy of it) and attached to the Versions property. At save time, this state will be used to calculate the set of values that indicate what has changed.

The following is an example of what the change state looks like through a sequence of changes for an object.

Start by creating and saving an object

                   ProductOfferHistory po =
                       new ProductOfferHistory
                       {
                           Id = "1234",
                           Price =
                               new PriceData
                                   {
                                       Price = (decimal)9.99,
                                       AsOfDate = DateTime.Now,
                                       Description = "the price"
                                   },
                           Description = "5678",
                           Name = "9101112",
                           Sku = "abc",
                           Keywords = new List<string> { "he", "haw" },
                           NameHistory =
                               new Dictionary<string, string>
                                   {
                                       12345", "junk1, 67890", "junk2
                                   },
                           PriceHistory =
                               new List<PriceData>
                                   {
                                    new PriceData
                                       {
                                           Price = (decimal)8.99,
                                           AsOfDate = new DateTime(2011, 1, 1),
                                           Description = "the old price"
                                       },
                                       }
                       };

                   wrapper.SaveItems(new List<ProductOfferHistory> { po });

At which point the object looks like this
{
 "PO" : {
   "d" : "5678",
   "Id" : "1234",
   "k" : ["he", "haw"],
   "n" : "9101112",
   "h" : {
     "12345" : "junk1",
     "67890" : "junk2"
   },
   "p" : {
     "P" : {
       "a" : "2011-09-02T15:24:12",
       "d" : "the price",
       "p" : "9.99"
     }
   },
   "ph" : [{
       "P" : {
         "a" : "2011-01-01T00:00:00",
         "d" : "the old price",
         "p" : "8.99"
       }
     }],
   "s" : "abc"
 },
 "_id" : "1234"
}
Get the object again, make some changes and then save it.and get it again
                   ProductOfferHistory po2 = wrapper.GetViaExternalID(new ExternalIDType("1234"));

                   po2.Description = "5678-new";

                   po2.Keywords.Add("ha");

                   po2.PriceHistory.Add(
                       new PriceData
                           {
                               Price = po2.Price.Price,
                               AsOfDate = po2.Price.AsOfDate,
                               Description = po2.Price.Description
                           });

                   po2.Price.Price = (decimal)10.99;

                   po2.Keywords.RemoveAt(1);

                   wrapper.SaveItems(new List<ProductOfferHistory> { po2 });

                   ProductOfferHistory po3 = wrapper.GetViaExternalID(new ExternalIDType("1234"));
At which point po3 looks like this:
{
 "PO" : {
   "d" : "5678-new",
   "Id" : "1234",
   "k" : ["he", "ha"],
   "n" : "9101112",
   "h" : {
     "12345" : "junk1",
     "67890" : "junk2"
   },
   "p" : {
     "P" : {
       "a" : "2011-09-02T15:24:12",
       "d" : "the price",
       "p" : "10.99",
       "Versions" : [{
           "p" : "9.99",
           "_version" : 634505992104083068
         }]
     }
   },
   "ph" : [{
       "P" : {
         "a" : "2011-01-01T00:00:00",
         "d" : "the old price",
         "p" : "8.99"
       }
     }, {
       "P" : {
         "a" : "2011-09-02T15:24:12",
         "d" : "the price",
         "p" : "9.99"
       }
     }],
   "s" : "abc",
   "Versions" : [{
       "d" : "5678",
       "k" : ["haw"],
       "k_added" : [1],
       "k_deleteThese" : [1],
       "ph_deleteThese" : [1],
       "_version" : 634505992104083068
     }]
 },
 "_id" : "1234"
}
Note that there are two sets of version information. One for the outer ProductOfferHistory type and one for the inner PriceData type. Also note that lists and dictionaries changes are maintained using indexes, add and delete operations, rather than copies of the entire property value.

Previous versions can be accessed like this:
                   SortedDictionary<DateTime, ProductOfferHistory> poVersions =
                       po3.Versions.GetItemVersions<ProductOfferHistory>(serializer);

                   foreach (KeyValuePair<DateTime, ProductOfferHistory> poh in poVersions)
                   {
// poh.Key is the timestamp for the version, poh.Value is the instance
// as it was at that time
                   }

Last edited Sep 5, 2011 at 9:39 PM by PatrickThompson, version 1

Comments

No comments yet.