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
}
My question is alse answered in this link under example heading P11: http://www.codeproject.com/Articles/18743/Interfaces-in-C-For-Beginners
The reason for asking this question was just to confirm the Object Oriented Rule on interface that was stated during discussion on Domain Driven Design book in whihc Repositoary Factory class returns a generic type of IRepository which also means that the RepositoryFactory will be able to return IRepository OR any interface OR Class extending IRepository in addition to implementing it (That is how i think .NET generics are interpreted ( full code and discussion is below).
---Discussion STARTS here---
--Code
--Code
The signature of this method is interesting because it uses two Generic type parameters, TRepository and TEntity , with the restrictions that TRepository is a class and implements the IRepository < TEntity > interface, and that TEntity derives from the EntityBase class. Because the Repository Framework is supporting interfaces other than just IRepository < T > , the method cannot just return a type of IRepository < T > for the Repository instance. It must also support returning any interface that implements IRepository < T > , since the repository interface being used can also have additional methods defined in it; that is why TRepository has been declared as a Generic type, so that the factory can support the Repository Framework requirements of being able to pass in a valid Repository interface type and get an instance of the interface (as long as it has been properly defined in the application ’ s configuration file).
---Discussion ENDS here---