I have a model with a bunch of properties in Cassandra and C#, so to prevent myself having to assign manually every value from a normal Prepared Statement (or a script that does it for me), I started using the Mapper Component. However, CQL date
s are automatically mapped to LocalDate
s which are serialized into a JSON object with Date, Month and Year properties and I need a normal string.
I gave a look into MappingConfiguration.Global
which has the Define
and ConvertTypesUsing
methods, but the first seems to not have what I'm looking for and I wasn't even able to implement the TypeConverter
class it expects as a parameter on the latter, since it has a syntax I've never seen (and I don't even know if it does the job).
So I was wondering, are there any built in options in the CassandraDrive to achieve what I want?
If anyone wants to give a look to my custom TypeConverter, this what I thought would work:
using System;
using Cassandra;
using Cassandra.Mapping.TypeConversion;
namespace Api.Helpers
{
public class LocalDateConverter : TypeConverter
{
protected override Func<TDatabase, TPoco> GetUserDefinedFromDbConverter<TDatabase, TPoco>()
{
Func<LocalDate, string> converter = date =>
String.Format("{0}-{1}-{2}", date.Year, date.Month, date.Year);
return converter;
}
protected override Func<TPoco, TDatabase> GetUserDefinedToDbConverter<TPoco, TDatabase>()
{
throw new NotImplementedException();
}
}
With that method declaration syntax I don't know where I'm supposed to indicate what types I want the converter to apply. This code fails with Can't convert 'System.Func<Cassandra.LocalDate, string>' into 'System.Func<TDatabase, TPoco>'
.
Another solution non related to the Cassandra driver was a custom Json Converter, but I'm also using OData, which uses a custom serializer.