Beispiel #1
0
        private IUnitOfWork GetRequiredNewUnitOfWork(TransactionOptions transactionOptions)
        {
            var unitOfWork = new EfUnitOfWork(AmbientContextManager, new DbContextContainer());

            AmbientContextManager.AddUnitOfWork(unitOfWork);

            return(unitOfWork);
        }
Beispiel #2
0
        private IUnitOfWork GetRequiredUnitOfWork(TransactionOptions transactionOptions)
        {
            if (!AmbientContextManager.AmbientExists || !AmbientContextManager.AmbientUnitOfWorkIsValid)
            {
                return(GetRequiredNewUnitOfWork(transactionOptions));
            }

            /* First check if the ambient is a CompositeUnitOfWork and add to its collection.
             * This must come prior to checking whether it is not an instance of IEfUnitOfWork so we can
             * add it as a leaf to a composite.
             * */
            var ambient = AmbientContextManager.Ambient;

            if (ambient.IsTypeOf <CompositeUnitOfWork>())
            {
                UnitOfWorkBase unitOfWork;
                var            currentCompositeUnitOfWork = (CompositeUnitOfWork)AmbientContextManager.Ambient.UnitOfWork;
                if (currentCompositeUnitOfWork.ContainsType <IEfUnitOfWork>())
                {
                    unitOfWork = currentCompositeUnitOfWork.Single(uow => uow.GetType().Implements <IEfUnitOfWork>());
                }
                else
                {
                    unitOfWork = new EfUnitOfWork(AmbientContextManager, new DbContextContainer(), currentCompositeUnitOfWork);
                    currentCompositeUnitOfWork.Add(unitOfWork);
                }

                AmbientContextManager.AddUnitOfWork(unitOfWork);

                return(unitOfWork);
            }

            // Current ambient is not Entity Framework.
            if (!AmbientContextManager.Ambient.IsTypeOf <IEfUnitOfWork>())
            {
                return(GetRequiredNewUnitOfWork(transactionOptions));
            }

            // Current ambient implements IEfUnitOfWork so let's simply retain it by incrementing the ambient session count.
            AmbientContextManager.RetainAmbient();

            return(AmbientContextManager.Ambient.UnitOfWork);
        }