Race Conditions with the Unit Of Work

Oct 28, 2008 at 7:35 PM
Rajulpatel & Tim,

I checked out the code in your Navigant project and it has the same RepositoryFactory as you use in your book, if the Repository has already been created it is saved in a Dictionary and if anyone else needs it it is pulled from there using this piece of code:

// The provider was in the cache, so retrieve it
repository = (TRepository)RepositoryFactory.repositories[repositoryCacheKey];

if (unitOfWork != null && repository.GetType().IsSubclassOf(typeof(RepositoryBase<TEntity>)))
{
    repository.SetUnitOfWork(unitOfWork);
}

I notice in your Navigant project you are not using Static Service classes but are creating new classes and everytime you need to use a repository you are getting it from the factory and injecting your UnitOfWork class, like so:

// The PreservationNoticeService

protected override IRepository<PreservationNotice> GetRepository()
{
     return RepositoryFactory.GetRepository<IPreservationNoticeRepository, PreservationNotice>(this.currentCase, this.unitOfWork);
}

public void RemovePreservationNotice(PreservationNotice notice)
{
    this.Repository.Remove(notice);
}

Could you ever run into race conditions where the UnitOfWork gets replaced before you have run your method?

Cheers
Scott