Transaction support

Nov 7, 2007 at 10:38 PM
Are there any plans to add transaction support in the future versions? We have special reasons not to use tran scope.
This is what I had to, and please let me know if it is incorrect.
In Repository<TDomainObject>class I added:

private DbTransaction transaction;

+ 2 additional constructors:

/// <summary>
/// Initializes a new instance of the <see cref="Repository<TDomainObject>"/> class.
/// </summary>
/// <param name="transaction">The transaction.</param>
public Repository(DbTransaction transaction)
: this()
{
this.transaction = transaction;
}

/// <summary>
/// Initializes a new instance of the <see cref="Repository<TDomainObject>"/> class.
/// </summary>
/// <param name="db">The db.</param>
public Repository(string databaseName, DbTransaction transaction)
: this(databaseName)
{
this.transaction = transaction;
}

then modified insert, update, delete functions to:

if (transaction != null)
db.ExecuteNonQuery(command, transaction);
else
db.ExecuteNonQuery(command);

I also modified the template file EntityRepository.t4 with this:

public <#= entity.Name #>Repository(DbTransaction transaction)
: base(transaction)
{
}

public <#= entity.Name #>Repository(string databaseName, DbTransaction transaction)
: base(databaseName, transaction)
{
}

Now I have support for transactions. By the way the binary release does not match source code. Source code is missing RepositoryInvalidParameterException class.
Nov 10, 2007 at 11:37 PM
Yep,

I wonder how to implement a transaction...

Also i wonder if this Repository Factory is ready for a demanding production environment?

Couldn't find any documentation...
Developer
Nov 11, 2007 at 8:14 AM
This seems not to be the way transactionssupport is to be added into the repository.

A repository instance has a lifetime for the whole of the application lifetime. U initiate it once, and use it whenever you need it. It's more of a service.

Benny
Nov 12, 2007 at 4:31 AM
Benny, I think you are correct.
In my case the repositories were wrapped in a service with a lifetime of a single transaction, so it did not make any difference. But the correct way to do it would be to modify Add, Remove, and Save functions to take Transaction as a parameter. I can rebuild it for my project. The question is: is transaction support will be added to the future versions?
Developer
Nov 15, 2007 at 8:57 PM
Is it possible for you to use TransactionScope in System.Transactions?

If so, you should be able to use it across multiple Repository Classes with no changes:

using (TransactionScope scope = new TransactionScope())
{
    // Use as many Repository<T> Classes as you want
    // Insert, Update, Delete etc...
 
   scope.Complete();
}

Regards,

Dave

________________________

David Hayden
Microsoft MVP C#
Developer
Jul 30, 2008 at 4:55 AM
David,

I tried this and found that if one of the repository calls generated an error, then all subsequent calls would fail as the transaction scope had somehow closed my connection, or thats the way it appears at the moment.  I am still investigating the outcome.

Any Thoughts?

Cheers

Adam
Aug 1, 2008 at 5:44 PM
The original design intent was that you'd use TransactionScope to manage transactions.