Skip to end of metadata
Go to start of metadata

Custom transformers can be used with the following IStorageEngine method:

ITable<TKey, TRecord> OpenXTablePortable<TKey, TRecord>(string name, DataType keyDataType, DataType recordDataType, ITransformer<TKey, IData> keyTransformer, ITransformer<TRecord, IData> recordTransformer);

To open a table with custom transformers, along with the user types, we must manually provide 4 things:

  • description of the anonymous key type
  • transformer implementation that transforms each user key to/from anonymous key
  • description of the anonymous record type
  • transformer implementation that transforms each user record to/from anonymous record

In the default transformers, the engine generates key & record descriptions that exactly match the key & user types. This however is not mandatory – with the custom transformers the dataType descriptors can be completely different from the original user types. The important thing here is that the provided descriptions should accurately describe the anonymous instances returned by the provided transformers.

Example:

    DataType keyDataType = DataType.Int64;
    DataType recordDataType = DataType.Slots(DataType.String, DataType.DateTime, DataType.Decimal, DataType.Decimal);
    ITable<long, Tick> table = engine.OpenXTablePortable<long, Tick>("table2", keyDataType, recordDataType, null, new CustomRecordTransformer());

With the above code we just tell the engine the table2 structure, the rest is covered by the transformers - they are responsible for converting the input user data to anonymous objects, that match the specified dataType descriptors. In the above example we decided to provide only a record transformer (the engine will generate a default key transformer).

public class CustomRecordTransformer : ITransformer<Tick, IData>
{
    public IData To(Tick value1)
    {
        Slots<string, DateTime, decimal, decimal> slots = new Slots<string, DateTime, decimal, decimal>();
        slots.Slot0 = value1.Symbol;
        slots.Slot1 = value1.Timestamp;
        slots.Slot2 = (decimal)value1.Bid;
        slots.Slot3 = (decimal)value1.Ask;
        Data<Slots<string, DateTime, decimal, decimal>> data = new Data<Slots<string, DateTime, decimal, decimal>>(slots);
        return data;
    }
    public Tick From(IData value2)
    {
        Data<Slots<string, DateTime, decimal, decimal>> data = (Data<Slots<string, DateTime, decimal, decimal>>)value2;
        Slots<string, DateTime, decimal, decimal> slots = data.Value;
        Tick tick = new Tick();
        tick.Symbol = slots.Slot0;
        tick.Timestamp = slots.Slot1;
        tick.Bid = (double)slots.Slot2;
        tick.Ask = (double)slots.Slot3;
        return tick;
    }
}

Transformers    Go to Home    Default Transformers

  • No labels