oysteinkrog/SQLite.Net-PCL — pull request #30 by jamesottaway (2014-05-14 22:05:45+00:00)
Title: Serialize and deserialize all types, not just BLOBs

I was recently trying to use [tiny types](http://darrenhobbs.com/2007/04/11/tiny-types/) to improve the method signatures in my Xamarin app. Obviously `SQLite.Net` couldn't know what to do with my custom types (which were wrapping value types like `string` or `int`) out of the box.

I didn't want to serialize them as BLOBs using `BlobSerializerDelegate` (or another implementation of `IBlobSerializer`) so I've introduced a generic interface called `ISerializable` to allow custom classes and structs to define how `SQLite.Net` can switch them back and forward from their nested types.

For example, I might have a `FirstName` struct which implements `ISerializable` and looks like this:

```
public struct FirstName : ISerializable
{
private string _value;

public FirstName(string value)
{
_value = value;
}

public string Serialize()
{
return _value;
}
}
```

The key parts here are the `Serialize()` method which exposes the inner type, and the constructor which takes the value returned from the database and can rebuild the custom type.

When it came to deciding how best to deserialize the value back into the tiny type, I had three ideas:

- Use reflection to find a constructor which takes one argument of the inner type, and invoke it with the value from the database
- Use reflection to find a constructor which is annotated with `DeserializeAttribute`, and invoke it with the value from the database (hoping the type signature matches)
- Use reflection to find the inner property of type `T` and try to set its value (haven't confirmed this would even work)

I went with the first option, but let me know if you think one of the other ideas would be better suited.

I also haven't wired up `SerializableTest` into any of the platform-specific test projects just yet. I figured that #18 would land first and then I can wire it up to all test projects at once.

Let me know what you think!