Twee Cloud : Sync service

Our first service in our twee cloud. Will be an synchronization service so if you have twee cloud account.
You will be able to use Twee cross devices and also later cross platforms.

The approach we have taken for this is, that we will be running asp.net core connecting to an postgres database with dapper.
For those that care we use the following helper to make our database calls transactional without worrying about opening an transaction etc.

So here is the code.

    public class DbConnectionManager : IDbConnectionManager
    {
        private readonly Settings _settings;

        public DbConnectionManager(IOptions<Settings> settings)
        {
            _settings = settings.Value;
        }

        public async Task<T> TransactionalAsync<T>(Func<DbConnection, DbTransaction, Task<T>> execute)
        {
            using (var connection = CreateConnection())
            {
                await connection.OpenAsync();

                using (var transaction = connection.BeginTransaction())
                {
                    try
                    {
                        var result = await execute(connection, transaction);

                        transaction.Commit();

                        return result;
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();

                        throw;
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
        }

        private DbConnection CreateConnection()
        {
            var settings = _settings.Data.Postgres;
            return new NpgsqlConnection(settings.ConnectionString);
        }
    } 

and an example usage:

 var result = await _connectionManager.TransactionalAsync(async
                (connection, transaction) => await
                    connection.QueryAsync<SerieSync>("INSERT INTO TABLE", new {yourdata},transaction));

So that was just an sampel how we do dataaccess at the moment. So our phase 1 approach for the sync service will just be a single table with id,userid,serieid,episodenumber,season,watched,device,syncdate. The interesting thing here is the syncdate.

The app will send all watched episodes as a one line each, That will later be saved in the database. If a serie hasn't got any "watched" episode , we will just send an row with just the serieid. So the new client will know that it should add the serie even if there isn't any watched episodes.

Then when a new device connections , it will make a query to the sync service , where it will have either a lastsyncdate = null or the latest synced date. The client will get back all rows that are newer then that date and update it's database according to that data.

This will be the approach we will start with, just to keep it simpel. Think there will be another post about this when we know how we will handle merging and conflicts. We will just take this for a drive first to see how will it works.

comments powered by Disqus