NCache 4.4 - Online Documentation

Using Compact Serialization

 
To use compact serialization, object types that are to be compact serialized need to be configured through NCManager.  NCache internally handles serialization/deserialization of registered classes and the user doesn’t need to explicitly code for it.
 
However, NCache also allows to explicitly implement ICompactSerializable interface provided in
 
Alachisoft.NCache.Runtime.Serialization.ICompactSerializable
 
To implement this interface, the custom object class needs to implement Serialize/Deserialize methods which will be called by NCache while serializing/deserializing this object. Sample code for implent this interface is given below:
 
//Sample code for custom ICompactSerializable class
public class CustomObject : ICompactSerializable
    {
      private string _attribute1;
      private int _attribute2;
      private bool _attribute3;
 
        public CustomObject()
        { }
 
        public CustomObject(string attrib1, int attrib2, bool attrib3)
        {
            _attribute1 = attrib1;
            _attribute2 = attrib2;
            _attribute3 = attrib3;
        }
 
      #region ICompactSerializable Members
 
        public void Deserialize(Runtime.Serialization.IO.CompactReader reader)
        {
            _attribute1 = reader.ReadObject() as string;
            _attribute2 = reader.ReadInt32();
            _attribute3 = reader.ReadBoolean();
        }
 
        public void Serialize(Runtime.Serialization.IO.CompactWriter writer)
        {
            writer.WriteObject(_attribute1);
            writer.Write(_attribute2);
            writer.Write(_attribute3);
        }
 
        #endregion
    }
 
Care should be taken that class attributes are deserialized in the same order in which they are serialized otherwise serialized stream won’t be deserialized.
 
Implementing a Generic Type Handler
 
NCache also allows registering generic types in compact serialization. For this,  each type needs to be registered through NCManager. There is another way for registering generic compact types through "Generic Type Handler". With Generic Type Handler, the user can add all the generic types at once by implementing the IGenericTypeProvider interface from Alachisoft.NCache.Runtime.GenericTypesProvider. This saves a lot of time and effort in case the user wants to register a large number of types.
 
To implement this, the user needs to implement GetGenericTypes method and return the desired generic types in Type array. After this, deploy this class assembly to NCManager. NCManger will register all types populated in Type array. Sample code is given below:
 
using Alachisoft.NCache.Runtime.GenericTypesProvider;
namespace CompactSerialization
{
    public class CustomGenericType<T,K,V>
    {
    }
    public class SetGenerics : IGenericTypeProvider
    {
        Type[] allowedGenericTypes = new Type[5];
 
        Type[] IGenericTypeProvider.GetGenericTypes()
        {
            allowedGenericTypes[0] = typeof(Dictionary<string, Customer>);
            allowedGenericTypes[1] = typeof(List<int>);
            allowedGenericTypes[2] = typeof(CustomGenericType<Employee, Product, Customer>);
            allowedGenericTypes[3] = typeof(CustomGenericType<Employee, Product, int>);
            allowedGenericTypes[4] = typeof(CustomGenericType<Employee, string, byte>);
            return allowedGenericTypes;
        }
    }
}
 
As can be seen in the above example, a list of types are specified which are to be allowed in the generic types for compact serialization.
 
 
See Also