Esempio n. 1
0
        public virtual IUnitOfWork GetUnitOfWork()
        {
            if (unitOfWork != null)
            {
                throw new Exception("a unit of work is already in use.");
            }

            else
            {
                if (!KeepConnectionAlive)
                {
                    context = new NtierAppDemoDbContext(ConnectionString);

                    if (firstRequest)
                    {
                        context.Database.Initialize(force: false);
                        firstRequest = false;
                    }
                }
                else
                {
                    if (dbConnection == null)
                    {
                        if (firstRequest)
                        {
                            using (var tempContext = new NtierAppDemoDbContext(ConnectionString))
                            {
                                tempContext.Database.Initialize(force: false);
                                dbConnection = tempContext.Database.Connection;
                            }

                            firstRequest = false;
                        }



                        if (dbConnection != null)
                        {
                            dbConnection.ConnectionString = ConnectionString;
                            dbConnection.Open();
                        }
                    }

                    context = new NtierAppDemoDbContext(dbConnection, false);
                }

                unitOfWork = new UnitOfWork(context);
                return(unitOfWork);
            }
        }
Esempio n. 2
0
        public virtual void ReturnUnitOfWork()
        {
            if (unitOfWork != null)
            {
                if (!KeepConnectionAlive)
                {
                    if (dbConnection != null)
                    {
                        throw new Exception("Database connection is not null in disconnected state.");
                    }
                }

                else if (dbConnection == null || dbConnection.State != System.Data.ConnectionState.Open)
                {
                    throw new Exception("Database connection is not open in connected state.");
                }

                unitOfWork.Dispose();
                unitOfWork = null;
                context    = null;
            }
        }