Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EntityFrameworkUnitOfWork"/> class.
        /// </summary>
        public EntityFrameworkUnitOfWork(IUnitOfWorkProvider provider, Func <AppDbContext> dbContextFactory, DbContextOptions options)
        {
            if (options.HasFlag(DbContextOptions.DisableTransactionMode))
            {
                TransactionalModeEnabled = false;
            }

            if (options.HasFlag(DbContextOptions.ReuseParentContext))
            {
                var parentUow = provider.GetCurrent() as EntityFrameworkUnitOfWork;
                if (parentUow != null)
                {
                    Context = parentUow.Context;
                    if (!parentUow.TransactionalModeEnabled)
                    {
                        canCommit = true;
                    }

                    return;
                }
            }

            Context    = dbContextFactory();
            ownContext = true;
            canCommit  = true;
        }
Esempio n. 2
0
 public static IDbTransaction TryGetDbTransaction(IUnitOfWorkProvider provider)
 {
     if (!(provider.GetCurrent() is NativeUnitOfWork uow))
     {
         throw new InvalidOperationException("Must be used inside of unit of work. Did you forget call UowProvider.Create() inside 'using' block?");
     }
     return(uow.Transaction);
 }
Esempio n. 3
0
 public static DbContext TryGetDbContext(IUnitOfWorkProvider provider)
 {
     if (!(provider.GetCurrent() is EntityFrameworkUnitOfWork uow))
     {
         throw new InvalidOperationException("Must be used inside of unit of work. Did you forget call UowProvider.Create() inside 'using' block?");
     }
     return(uow.Context);
 }
 /// <summary>
 /// Tries to get the <see cref="DbContext"/> in the current scope.
 /// </summary>
 public static DbContext TryGetDbContext(IUnitOfWorkProvider provider)
 {
     var uow = provider.GetCurrent() as EntityFrameworkUnitOfWork;
     if (uow == null)
     {
         throw new InvalidOperationException("The EntityFrameworkRepository must be used in a unit of work of type EntityFrameworkUnitOfWork!");
     }
     return uow.Context;
 }
        /// <summary>
        /// Tries to get the <see cref="DbContext" /> in the current scope.
        /// </summary>
        public static TDbContext TryGetDbContext <TDbContext>(IUnitOfWorkProvider unitOfWorkProvider)
            where TDbContext : DbContext
        {
            var index = 0;
            var uow   = unitOfWorkProvider.GetCurrent(index);

            while (uow != null)
            {
                if (uow is EntityFrameworkUnitOfWork <TDbContext> efuow)
                {
                    return(efuow.Context);
                }

                index++;
                uow = unitOfWorkProvider.GetCurrent(index);
            }

            return(null);
        }
Esempio n. 6
0
        /// <summary>
        /// Tries to get the <see cref="Microsoft.EntityFrameworkCore.DbContext"/> in the current scope.
        /// </summary>
        public static DbContext TryGetDbContext(IUnitOfWorkProvider provider)
        {
            var uow = provider.GetCurrent() as EntityFrameworkUnitOfWork;

            if (uow == null)
            {
                throw new InvalidOperationException("The EntityFrameworkRepository must be used in a unit of work of type EntityFrameworkUnitOfWork!");
            }
            return(uow.Context);
        }
Esempio n. 7
0
        /// <summary>
        /// Tries to get the <see cref="ITableStorageContext"/> in the current scope.
        /// </summary>
        public static ITableStorageContext TryGetTableStorageContext(IUnitOfWorkProvider provider)
        {
            var uow = provider.GetCurrent() as TableStorageUnitOfWork;

            if (uow == null)
            {
                throw new InvalidOperationException("The TableStorageRepository must be used in a unit of work of type TableStorageUnitOfWork!");
            }
            return(uow.Context);
        }
Esempio n. 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EntityFrameworkUnitOfWork"/> class.
        /// </summary>
        public EntityFrameworkUnitOfWork(IUnitOfWorkProvider provider, Func <DbContext> dbContextFactory, DbConnectionOption options)
        {
            if (options == DbConnectionOption.ReuseParentConnection)
            {
                if (provider.GetCurrent() is EntityFrameworkUnitOfWork parentUow)
                {
                    Context = parentUow.Context;
                    return;
                }
            }

            Context        = dbContextFactory();
            _hasOwnContext = true;
        }
Esempio n. 9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EntityFrameworkUnitOfWork"/> class.
        /// </summary>
        public EntityFrameworkUnitOfWork(IUnitOfWorkProvider provider, Func <DbContext> dbContextFactory, DbContextOptions options)
        {
            if (options == DbContextOptions.ReuseParentContext)
            {
                var parentUow = provider.GetCurrent() as EntityFrameworkUnitOfWork;
                if (parentUow != null)
                {
                    this.Context = parentUow.Context;
                    return;
                }
            }

            this.Context  = dbContextFactory();
            hasOwnContext = true;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="EntityFrameworkUnitOfWork"/> class.
        /// </summary>
        public EntityFrameworkUnitOfWork(IUnitOfWorkProvider provider, Func<DbContext> dbContextFactory, DbContextOptions options)
        {
            if (options == DbContextOptions.ReuseParentContext)
            {
                var parentUow = provider.GetCurrent() as EntityFrameworkUnitOfWork;
                if (parentUow != null)
                {
                    this.Context = parentUow.Context;
                    return;
                }
            }

            this.Context = dbContextFactory();
            hasOwnContext = true;
        }
Esempio n. 11
0
        public TableStorageUnitOfWork(IUnitOfWorkProvider provider, Func <ITableStorageContext> contextFactory, StorageContextOptions options)
        {
            if (options == StorageContextOptions.ReuseParentContext)
            {
                var parentUow = provider.GetCurrent() as TableStorageUnitOfWork;
                if (parentUow != null)
                {
                    Context = parentUow.Context;
                    return;
                }
            }

            Context       = contextFactory();
            hasOwnContext = true;
        }
Esempio n. 12
0
        public NativeUnitOfWork(IUnitOfWorkProvider provider, Func <IDbConnection> connectionFactory, DbConnectionOption options)
        {
            if (options == DbConnectionOption.ReuseParentConnection)
            {
                if (provider.GetCurrent() is NativeUnitOfWork parentUow)
                {
                    Transaction = parentUow.Transaction;
                    return;
                }
            }

            Connection = connectionFactory();
            Connection.Open();
            Transaction       = Connection.BeginTransaction();
            _hasOwnConnection = true;
        }
Esempio n. 13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ExpenseManagerUnitOfWork"/> class.
        /// </summary>
        public ExpenseManagerUnitOfWork(IUnitOfWorkProvider provider, bool reuseParentContext)
        {
            if (reuseParentContext)
            {
                var parentunitOfWork = provider.GetCurrent() as ExpenseManagerUnitOfWork;
                if (parentunitOfWork != null)
                {
                    this.Context = parentunitOfWork.Context;
                    return;
                }
            }

            var unitOfWorkProvider = (ExpenseManagerUnitOfWorkProvider)provider;

            Context = unitOfWorkProvider.ConnectionOptions == null
                ? unitOfWorkProvider.DbContextFactory?.Invoke() as ExpenseDbContext
                      // internal DbContext shall not be injected in some scenarios in order to increase persistence separation
                : new ExpenseDbContext(unitOfWorkProvider.ConnectionOptions.ConnectionString);

            _hasOwnContext = true;
        }
 public ApplicationUserStore(IUnitOfWorkProvider unitOfWorkProvider)
     : base((unitOfWorkProvider.GetCurrent() as AppUnitOfWork).Context)
 {
 }