web version vs UnitOfWork

Oct 28, 2008 at 2:00 AM
I recently bought your book and I'm reading it as crazy!
My main activity is web app, so I immediatly try to figure out how to reuse your DDD code into a web app.
Did I miss somenthing or UnitOfWork.Commit() should be wrapped into a lock(...){} to avoid to Clear dictionary during concurrently web requests / different threads?
or for example  into CompanyService -> SaveCompany
{
lock(..)
{
            CompanyService.repository[company.Key] = company;
            CompanyService.unitOfWork.Commit();
}
}
Coordinator
Oct 28, 2008 at 5:32 PM
Crixo,

First of all thank you for buying the book!

In order to give you a better idea of how to use DDD in a web applicatiion, I highly recommend checking out my other CodePlex project which is a web-based aplication.  As you will see in that application, the Unit of Work is never static.  Whenever there is a series of changes that need to be made to the Domain Model, a new instance of an IUnitOfWork derivative is created and then committed when all of the changes are complete.

I hope this helps you.

--Tim
Oct 28, 2008 at 6:05 PM
Edited Oct 28, 2008 at 6:07 PM
Hi 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