protected async Task <T> InTx <T>(Func <ISession, Task <T> > function)
        {
            using (var session = SessionFactory.OpenSession())
                using (var txn = session.BeginTransaction())
                {
                    T result;
                    try
                    {
                        result = await function.Invoke(session).ConfigureAwait(false);

                        await txn.CommitAsync().ConfigureAwait(false);

                        return(result);
                    }
                    catch (Exception ex)
                    {
                        // TODO: exception handling including retry logic
                        string message = $"Exception {ex}. Inner exception ${ex.InnerException}";
                        TestContextWriter.WriteLine(message);
                        Trace.WriteLine(message);
                        await txn.RollbackAsync().ConfigureAwait(false);

                        throw;
                    }
                }
        }
 public async Task Save(Param model)
 {
     try
     {
         await InTx(async session =>
         {
             ParamEntity entity = await session.GetAsync <ParamEntity>(model.Id).ConfigureAwait(false) ?? new ParamEntity();
             entity.Key         = model.Key;
             entity.Value       = model.Value;
             entity.Description = model.Description;
             model.Id           = (Guid)await session.SaveAsync(entity).ConfigureAwait(false);
         }).ConfigureAwait(false);
     }
     catch (GenericADOException ex)
     {
         if (ex.IsDuplicateKey())
         {
             throw new ValidationException("The Key field must be unique");
         }
         TestContextWriter.WriteLine($"GenericADOException: HRESULT = {ex.HResult}");
         throw;
     }
 }