Created
January 26, 2011 20:30
-
-
Save codelinq/797390 to your computer and use it in GitHub Desktop.
Make EFCachingProvider Support ambient transactions via TransactionScope.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class EFCachingCommand : DbCommandWrapper | |
{ | |
// Modified this method to update the EFCachingEnlistment if it exists. | |
private void UpdateAffectedEntitySets() | |
{ | |
if (this.transaction != null) | |
{ | |
if (this.Definition.IsModification) | |
{ | |
this.transaction.HasModifications = true; | |
} | |
foreach (EntitySetBase entitySet in this.Definition.AffectedEntitySets) | |
{ | |
this.transaction.AddAffectedEntitySet(entitySet); | |
} | |
} | |
else if (this.Connection.Enlistment != null) | |
{ | |
if (this.Definition.IsModification) | |
{ | |
this.Connection.Enlistment.HasModifications = true; | |
} | |
foreach (EntitySetBase entitySet in this.Definition.AffectedEntitySets) | |
{ | |
this.Connection.Enlistment.AddAffectedEntitySet(entitySet); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EFCachingConnection : DbConnectionWrapper | |
{ | |
// Added the following property and method | |
internal EFCachingEnlistment Enlistment { get; set; } | |
public override void Open() | |
{ | |
base.Open(); | |
if (System.Transactions.Transaction.Current != null) | |
{ | |
this.Enlistment = new EFCachingEnlistment {Cache = this.Cache, HasModifications = false}; | |
System.Transactions.Transaction.Current.EnlistVolatile(this.Enlistment, EnlistmentOptions.None); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class EFCachingEnlistment : IEnlistmentNotification | |
{ | |
private HashSet<EntitySetBase> affectedEntitySets = new HashSet<EntitySetBase>(); | |
public EFCachingEnlistment() | |
{ | |
HasModifications = false; | |
} | |
public void Commit(Enlistment enlistment) | |
{ | |
if (Cache != null && this.HasModifications) | |
{ | |
Cache.InvalidateSets(this.affectedEntitySets.Select(c => c.Name)); | |
} | |
enlistment.Done(); | |
} | |
public void InDoubt(Enlistment enlistment) | |
{ | |
enlistment.Done(); | |
} | |
public void Prepare(PreparingEnlistment preparingEnlistment) | |
{ | |
preparingEnlistment.Prepared(); | |
} | |
public void Rollback(Enlistment enlistment) | |
{ | |
enlistment.Done(); | |
} | |
internal void AddAffectedEntitySet(EntitySetBase entitySet) | |
{ | |
this.affectedEntitySets.Add(entitySet); | |
} | |
public bool HasModifications { get; set; } | |
public ICache Cache { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment