LATEST VERSION: 8.2.6 - CHANGELOG
Pivotal GemFire® v8.2

Use the IPdxInstanceFactory to Create IPdxInstances

Use the IPdxInstanceFactory to Create IPdxInstances

You can use the IPdxInstanceFactory to create an IPdxInstance from raw data when the domain class is not available on the server.

This option can be useful when you need an instance of a domain class for plug-in code such as a function or a loader. If you have the raw data for the domain object (the class name and each field's type and data), then you can explicitly create a IPdxInstance. The IPdxInstanceFactory is very similar to the IPdxWriter except that after writing each field, you need to call the create method which returns the created IPdxInstance.

Creating an IPdxInstance with IPdxInstanceFactory

/*
         * The PdxInstance QuickStart Example.
         * This example takes the following steps:
         *
         * This example shows IPdxInstanceFactory and IPdxInstance usage.
         *
         * 1. Create a GemFire Cache.
         * 2. Creates the PdxInstanceFactory for Person class.
         * 3. Then creates instance of PdxInstance
         * 4. It does put.
         * 5. Then it does get and access it fields.
         * 6. Close the Cache.
         *
         */
        // Use standard namespaces
        using System;
        using System.Reflection;
        // Use the GemFire namespace
        using GemStone.GemFire.Cache.Generic;
        namespace GemStone.GemFire.Cache.Generic.QuickStart
        {
          public class Person
          {
            private string name;
            //this is the only field used on server to create hashcode and use in equals method
            [PdxIdentityField]
            private int id;
            private int age;
            public Person() { }
            public Person(string name, int id, int age)
            {
              this.name = name;
              this.id = id;
              this.age = age;
            }
            #region Public Properties
            public string Name
            {
              get { return name; }
            }
            public int ID
            {
              get { return id; }
            }
            public int Age
            {
              get { return age; }
            }
            #endregion  
          }
        // The PdxRemoteQuery QuickStart example.
class PdxInstance
  {
    static void Main(string[] args)
    {
      try {
        CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(null);
        Console.WriteLine("Connected to the GemFire Distributed System");
        // Create a GemFire Cache with the "clientPdxRemoteQuery.xml" Cache XML file.
        // Set SetPdxReadSerialized to true to access PdxInstance
        Cache cache = cacheFactory.Set("cache-xml-file",
"XMLs/clientPdxInstance.xml").Create();
        Console.WriteLine("Created the GemFire Cache");
        // Get the example Region from the Cache which is declared in the
Cache XML file.
        IRegion<string, IPdxInstance> region = cache.GetRegion<string,
IPdxInstance>("Person");
        Console.WriteLine("Obtained the Region from the Cache");
        Person p = new Person("Jack", 7, 21);
        //PdxInstanceFactory for Person class
        IPdxInstanceFactory pif = cache.CreatePdxInstanceFactory("Person");
        pif.WriteString("name", p.Name);
        pif.WriteInt("id", p.ID);
        pif.MarkIdentityField("id");
        pif.WriteInt("age", p.Age);
        IPdxInstance pdxInstance = pif.Create();
        Console.WriteLine("Created PdxInstance for Person class");
        region["Key1"] = pdxInstance;
        Console.WriteLine("Populated PdxInstance Object");
        IPdxInstance retPdxInstance = region["Key1"];
        if((int)retPdxInstance.GetField("id") == p.ID
             && (int)retPdxInstance.GetField("age") == p.Age
               && (string)retPdxInstance.GetField("name") == p.Name
                 && retPdxInstance.IsIdentityField("id") == true)
           Console.WriteLine("PdxInstance returns all fields value
expected");
        else
           Console.WriteLine("PdxInstance doesn't returns all fields value
expected");
        // Close the GemFire Cache.
        cache.Close();
        Console.WriteLine("Closed the GemFire Cache");
        }
        // An exception should not occur
        catch (GemFireException gfex)
        {
          Console.WriteLine("PdxSerializer GemFire Exception: {0}",
          gfex.Message);
        }
     }
  } 
}