Does the TransactionScope object also synchronizes the access to code among Multiple threads of Users ? OR, it only declares the code (Business Operations) as atomic (single Transaction)?
Details: 1. I am implementing UnitOfWork class for Repositories in Infrastructure Layer which itself is defined as class library project (dll).
Repository contains reference to object of UnitOfWork to call its methods which maintain diciontary/collection of Entities that has been added, changed Or updated.
Unit of Work class has a member function Commits() which has the code wrapped inside TransactionScope object.
Consider that Multiple users access the Domain/Business objects then i presume that each user will have its own set of business objects running in its thread.
I am not sure what TransactionScope object will do in this case ? Is it just decalring the multiple operations inside a user thread as single business transaction ? OR it is synchronizing the acess to code also among different threads of user/s? The code of UnitOfWork class is as below:
public class UnitOfWork
{
private Dictionary<EntityBase, IUnitOfWorkRepository> addedEntities;
private Dictionary<EntityBase, IUnitOfWorkRepository> changedEntities;
private Dictionary<EntityBase, IUnitOfWorkRepository> deletedEntities;
public UnitOfWork()
{
this.addedEntities = new Dictionary<EntityBase, IUnitOfWorkRepository>();
this.changedEntities = new Dictionary<EntityBase, IUnitOfWorkRepository>();
this.deletedEntities = new Dictionary<EntityBase, IUnitOfWorkRepository>();
}
#region IUnitOfWork Members
public void RegisterAdded(EntityBase entity, IUnitOfWorkRepository repository)
{
this.addedEntities.Add(entity, repository);
}
public void RegisterChanged(EntityBase entity, IUnitOfWorkRepository repository)
{
this.changedEntities.Add(entity, repository);
}
public void RegisterRemoved(EntityBase entity, IUnitOfWorkRepository repository)
{
this.deletedEntities.Add(entity, repository);
}
public void Commit()
{
using (TransactionScope scope = new TransactionScope())
{
foreach (EntityBase entity in this.deletedEntities.Keys)
{
this.deletedEntities[entity].PersistDeletedItem(entity);
}
foreach (EntityBase entity in this.addedEntities.Keys)
{
this.addedEntities[entity].PersistDeletedItem(entity);
}
foreach (EntityBase entity in this.changedEntities.Keys)
{
this.changedEntities[entity].PersistDeletedItem(entity);
}
scope.Complete();
}
this.deletedEntities.Clear();
this.addedEntities.Clear();
this.changedEntities.Clear();
}
#endregion
}