Skip to main content

Transaction Usage and Unit of Work

All storage transactions are handled using the Eleflex.IStorageContextUnitOfWork object. This object holds a list of all transactions that occur during processing of the current thread/request and allow Commit() or RollBack() of individual or all transactions created during the lifetime. Storage Repositories automatically register themselves with the Unit of Work object on creation. It is up to the programmer to Commit() or Rollback() the unit of work when all processing has been completed.

The Eleflex.IStorageContextUnitOfWork is configured with StructureMap by default to only create one instance per thread. This allows the same object instance to be shared by all objects on the same thread when using the object locator.

An example of managing transaction usage on a thread would be the following:


IStorageContextUnitOfWork uow = ObjectLocator.Current.GetInstance<IStorageContextUnitOfWork>();
try
{
//DO WORK HERE WITH MULTIPLE REPOSITORIES
uow.Commit();
}
catch (Exception ex)
{
uow.Rollback();                
Logger.Current.Error(“My transaction”, ex);
}