How to map C# ticks to PostgreSQL timestamp when using a Protocol Buffer model with Dapper?
<p>I'm using protobuf-net in my application to send logged data from a remote site to the server. There are multiple data types - a sample message of one of these is as follows:</p>

<pre><code>message Sample {
    required int64 recording_time = 1; // UTC Timestamp in Ticks
    required double x_position = 2;
    required double y_position = 3;
    required double x_velocity = 4;
    required double y_velocity = 5;
}
</code></pre>

<p>On the server, the object is stored in a PostgreSQL database. All the <code>double</code> fields in the Protocol Buffer (<code>proto2</code>) message are mapped to <code>double</code> fields in PostgreSQL. The timestamp field, <code>uint64 recording_time</code> must be mapped to a <code>timestamp with time zone</code> field in the database. </p>

<p>I want to use the same C# class (with <code>ProtoContract</code> attribute) to serialize <code>Sample</code> on the client, and also with Dapper for database operations (perhaps with an extension, e.g. FastCRUD). </p>

<p>This requires a mapping/conversion between <code>ticks</code> (<em>C# type: long</em>) and <code>timestamp with time zone</code> (<em>C# type: DateTime</em>). What is the best way to implement this, without creating a second class?</p>

<p>This is how I currently write objects to database:</p>

<pre><code>string sql = "COPY samples (recording_time, x_position, y_position, x_velocity, y_velocity) FROM STDIN (FORMAT BINARY)";
using (var writer = conn.BeginBinaryImport(sql))
{
    foreach (Sample sample in sampleList)
    {
        writer.StartRow();

        writer.Write(new DateTime(sample.RecordingTime, DateTimeKind.UTC), NpgsqlTypes.NpgsqlDbType.TimestampTZ);

        writer.Write(sample.X_Position, NpgsqlTypes.NpgsqlDbType.Double);
        writer.Write(sample.Y_Position, NpgsqlTypes.NpgsqlDbType.Double);
        writer.Write(sample.X_Velocity, NpgsqlTypes.NpgsqlDbType.Double);
        writer.Write(sample.Y_Velocity, NpgsqlTypes.NpgsqlDbType.Double);

    }
}
</code></pre>

<p>This is how I want to write to database:</p>

<pre><code>foreach (Sample sample in sampleList)
{
    conn.Insert&lt;Sample&gt;(sample);
}
</code></pre>

<p>And use the corresponding <code>Query</code> method for retrieval.</p>