This project is read-only.


How It Works

Cassandraemon uses its built-in classes to serialize and deserialize objects to and from Cassandra rows.
During the serializations, Cassandraemon asks CassandraSerializerFactory.Current for an instance of CassandraSerializer for each type of objects. This instance then will be responsible for both serializations and deserializations for the type.


CassandraSerializerFactory is a very simple abstract class with one method and one static property.
    public abstract class CassandraSerializerFactory
        public static CassandraSerializerFactory Current { get; set; }
        public abstract CassandraSerializer GetSerializer(Type type);


Cassandraemon provides one implementation of CassandraSerializerFactory in CompiledCassandraSerializerFactory. CassandraSerializerFactory.Current is set to an instance of CompiledCassandraSerializerFactory by default.
CompiledCassandraSerializerFactory creates CassandraSerializer instances that works as follows:
  • A subclass of CassandraSerializer is created and compiled dynamically for each type for high-performance serializations.
  • All public properties with public getters are serialized. Setters are also required, but they are not necessarily public.
  • Property names should match to column names.

Customizing Serializations

Cassandraemon provides three levels of customization, and you can also combine them to match to your needs.

Ignore Specific Properties for A Type

You need to create a subclass of CassandraSerializer and register its instance to CassandraSerializerFactory.
To create a subclass of CassandraSerializer, CompiledCassandraSerializerFactory class has a couple of methods to help you. The following example shows how to ignore properties that have IgnoreDataMemberAttribute for a type.
            var properties = type.GetProperties()
                .Where(p => !p.GetCustomAttributes(
                    typeof(IgnoreDataMemberAttribute), true).Any());
            var factory = CompiledCassandraSerializerFactory.Default;
            var serializer = factory.CreateSerializer(type, properties);
            factory.SetSerializer(type, serializer);
This code snippet creates a subclass of CassandraSerializer and create its instance. At the last line, it register the instance as the serializer for the class.

Change Column Name Mappings for A Type

CompiledCassandraSerializerFactory.CreateSerializer has a variation that accepts a list of PropertyInfo and column name.
            var properties = type.GetProperties();
            var factory = CompiledCassandraSerializerFactory.Default;
            var serializer = factory.CreateSerializer(type, properties
                .Select(p => Tuple.Create(p, columnNameByPropertyName[p.Name]));
            factory.SetSerializer(type, serializer);
If you set null to the column name, CompiledCassandraSerializerFactory uses the property name. The code below shows how CreateSerializer(Type, IEnumerable<PropertyInfo>) is implemented.
        public CassandraSerializer CreateSerializer(
            Type type,
            IEnumerable<PropertyInfo> properties)
            return this.CreateSerializer(type,
                properties.Select(p => Tuple.Create(p, (byte[])null)));

Change How CassandraSerializerFactory creates CassandraSerializer

If you prefer creating CassandraSerializer when needed rather than pre-populating, you can create your own subclass of CassandraSerializerFactory. There are two base classes you can choose.
  • CachedCassandraSerializerFactory provides a caching mechanism. It implements GetSerializer(Type) method to return a cached instance if one exists, or call CreateSerializer(Type) method to create a new one which you can override.
  • If you need full control, you can subclass from CassandraSerializerFactory. In this case you need to override GetSerializer(Type) method. This method is called everytime Cassandraemon needs serializers.
Once you created your own CassandraSerializerFactory class, create an instance and set it to CassandraSerializerFactory.Current as in the example below.
            CassandraSerializerFactory.Current = new MyOwnCassandraSerializerFactory();
After this point forward, Cassandraemon calls MyOwnCassandraSerializerFactory.GetSerializer(Type) to get serializers.
While creating CassandraSerializer instances in your factory class, you can use methods mentioned above.

Last edited Dec 19, 2011 at 6:29 AM by kojiishi, version 9


No comments yet.