This project is read-only.

UnitOfWork static gateway

Oct 3, 2008 at 4:26 PM

I am checking the code in the UnitOfWork class, which is is a static gateway to the current UnifOfWork instance. However, it is something that i can' understant the intention.

The implementation of UnitOfWork.Complete is

    public static void Complete()

and the Current property is

    private static IUnitOfWork Current
      get { return GetCurrent<IUnitOfWork>(); }

where the GetCurrent

    public static T GetCurrent<T>()
      where T : IUnitOfWork
      return ContextItemsBroker.GetItem<T>(CurrentContextKey);

The CuurentContextKey is based on the private field _depth, which is a static field. First of all, there is no sycnhronization in this field, which is static. Interlock.Increment/Decrement would be more safe? When you have this code

 using (UnitOfWork.Begin())
        contribution = UnmoderatedContributions
          .Where(c => c.Id == id)

        if (contribution != null)
          contribution.ApprovedBy = CurrentMember;

The UnitOfWork.Complete can be call a Complete in a UnitOfWork which is not the one returned in the Begin method. Is it supposed to be like this?

Thanks in advance