0

I'm trying to make a UnitOfWork/Repository pattern using fastcrud.

I have created a generic repository

public interface IRepository<T> where T : BaseEntity
{
    IDbTransaction Transaction { get; set; }

    T Get(T entityKeys, Action<ISelectSqlSqlStatementOptionsBuilder<T>> statementOptions = null);

    IEnumerable<T> Find(Action<IRangedBatchSelectSqlSqlStatementOptionsOptionsBuilder<T>> statementOptions = null);

    int Count(Action<IConditionalSqlStatementOptionsBuilder<T>> statementOptions = null);
    bool Delete(T entityToDelete, Action<IStandardSqlStatementOptionsBuilder<T>> statementOptions = null);
}

From the service I call

        var repo = UnitOfWork.GetRepository<MyTable>();

        var myList = repo.Find(statement => statement
            .AttachToTransaction(repo.Transaction)
            .OrderBy($"{nameof(MyTable.Name):C}")
        );

This works. But I don't want the service to handle the AttachToTransaction call, instead i would like to add it to my repository

    public IEnumerable<T> Find(Action<IRangedBatchSelectSqlSqlStatementOptionsOptionsBuilder<T>> statementOptions = null)
    {
        return Connection.Find<T>(statementOptions);
    }

But here the statementOption is a delegated Action, and I can't do

statementOption.AttachToTransaction(this.Transaction)

My UnitOfWork always creates an transaction, so if I skip attaching to transaction it I will get an exception

An unhandled exception occurred while processing the request.
InvalidOperationException: ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.
Trixz
  • 190
  • 13

2 Answers2

0

You can do it like this:

public IEnumerable<T> Find(Action<IRangedBatchSelectSqlSqlStatementOptionsOptionsBuilder<T>> statementOptions = null)
{
     statementOptions += s => s.AttachToTransaction(this.Transaction);
     return Connection.Find<T>(statementOptions);
}
0

I had the same issue too. I have used this extension method resolved it:

internal static IRangedBatchSelectSqlSqlStatementOptionsOptionsBuilder<TEntity> AttachToTransaction<TEntity>(
                        this IRangedBatchSelectSqlSqlStatementOptionsOptionsBuilder<TEntity> statement,
                        Action<IRangedBatchSelectSqlSqlStatementOptionsOptionsBuilder<TEntity>> originalStatementOptionsBuilder,
                        IDbTransaction transaction)
{
  if (originalStatementOptionsBuilder == null)
  {
    statement.AttachToTransaction(transaction);
  }
  else
  {
    originalStatementOptionsBuilder(statement);
    statement.AttachToTransaction(transaction);
  }

  return statement;
}

Now your service must change like this:

public IEnumerable<T> Find(Action<IRangedBatchSelectSqlSqlStatementOptionsOptionsBuilder<T>> statementOptions = null)
{
  return Connection.Find<T>(s => s.AttachToTransaction(statementOptions, this.Transaction));
}
Fred
  • 3,365
  • 4
  • 36
  • 57