Supported types in Lex.DB

(previous post)

Lex.DB supports following data member CLR types of out of box:

  • string (System.String)
  • byte (System.Byte)
  • int (System.Int32)
  • long (System.Int64)
  • double (System.Double)
  • float (System.Single)
  • decimal (System.Decimal)
  • bool (System.Boolean)
  • System.DateTime
  • System.DateTimeOffset
  • System.TimeSpan
  • System.Guid
  • Enums based on int, long and byte types

In addition to types listed above, Lex.DB supports derivative nullable types:

  • byte?
  • int?
  • long?
  • double?
  • float?
  • decimal?
  • bool?
  • DateTime?
  • DateTimeOffset?
  • TimeSpan?
  • Guid?
  • Nullable Enums

as well as collection based types like:

  • List<T>
  • HashSet<T>
  • T[] (Arrays)
  • Dictionary<TKey, TValue>

where T, TKey, TValue are supported non-array types.

Serialization of custom types

Knowing that the combination of types above could never be enough, Lex.DB will soon allow extension of database type system (starting from next release).

However, there is a catch:

  • Each new registered type must have unique identifier inside your data universe, collision will corrupt the data
  • Custom types have to be registered before any Lex.DB instance initialization
  • Once written in database, types must ALWAYS be registered (even if your app has evolved and there are no data members of this type anymore), otherwise data will become unreadable
  • Registration type identifier (short) < 1000 are reserved for internal and future use

Let’s define a custom type we’d like to support:

public struct Point
  public float X { get; set; }
  public float Y { get; set; }

To register this type with Lex.DB we need to provide serialization extension. It will look like this:

public static class CustomTypesSerializers
  public static Point ReadPoint(DataReader reader)
     return new Point { X = reader.ReadDouble(), Y = reader.ReadDouble() };

  public static void WritePoint(DataWriter writer, Point value)

Please notice, that we read the data in the same order as we write it into the stream. Read and Write methods are public and static. They have naming pattern like ReadTYPENAME and WriteTYPENAME, where TYPENAME is name of your custom type without namespace.

DataReader and DataWriter are subclasses of BinaryReader and BinaryWriter accordingly, so all reading and writing methods are available to you.

Now here is how to register it. This method must be called somewhere inside initialization section of your app. I would recommend put it in static constructor of your Application class. It guarantees that it will be called only once and before instance constructor of your app.

Serialization.Extender.RegisterType<Point, CustomTypesSerializers>(1000);

That’s it for today! Stay tuned.

(to be continued)

14 thoughts on “Supported types in Lex.DB”

  1. Does the built in serialiser work with inherited classes? In the process of migrating from SQLite to Lex.DB and the class that I am storing is based upon another class. Great work btw. Cheers Rupert.

    1. Using manual mapping it is definitely supported.

      On WinRT platform MapAll/AutoMap methods do not gather inherited members in version 1.1.2. It will be fixed in next minor release.

    1. What kind of complex types are you interested in?
      Nested objects, one-to-many, many-to-many associations, dynamic objects?

  2. I’m getting a compile error when trying to register my custom type (using your sample code):

    static types cannot be used as type arguments,

    was it your intention to make your example class static? If I remove static from the class definition it compile but I’m not sure what the intent is.

  3. Static attribute can be safely removed. I didn’t compile the snippet, no wonder that it doesn’t compile.

  4. Hi, just got hands on Lex.DB, looks good!
    Question: let’s say I have a POCO with an IEnumerable property, where some_type is a “custom” type of my own Is it just possible with Lex.DB?
    I thought using the RegisterType extension method, as described on this page, would do the trick, but it just doesn’t work.
    Any advice, please?

    1. Lex.DB supports blobs. So you may define a field/property of type byte[] and read/write your image data from/to.
      It is also possible to define custom serialization for images. I’ll provide the HowTo article as soon as I get some spare time.

  5. Great! I’m developing an app in Windows Phone 8 (Nokia Lumina 925). You think I can use Lex.Db as my database? Can I copy the database to my SkyDrive and connect to it from my phone?

  6. Lex,

    Can you please help with following, I am developing cross platform app using LexDb and like it a lot. But now i am getting NullReferenceException while Initialize method on DbInstance. Tried with most basic class (2 string properties only), but no luck.
    Any idea what can be a reason?


    1. Lex,

      Sorry, just found what was the reason. I am using PCL with LexDb provider and had to add LexDb to every project in a solution.
      Thank you.


Leave a Reply

Your email address will not be published. Required fields are marked *