Пример #1
0
        /// <summary>
        /// Parameterized constructor.
        /// </summary>
        /// <param name="saveAllChangesAtEndOfScope">
        /// A boolean value that indicates whether to automatically save
        /// all object changes at end of the scope.
        /// </param>
        public UnitOfWorkScope(bool saveAllChangesAtEndOfScope, string connectionString)
        {
            if (_currentScope != null && !_currentScope._isDisposed)
            {
                throw new InvalidOperationException("ObjectContextScope instances cannot be nested.");
            }

            _giornale.Debug("+Apro Unit-Of-Work");

            _saveAllChangesAtEndOfScope = saveAllChangesAtEndOfScope;


            /* Create a new ObjectContext instance: uso il proxy */
            if (String.IsNullOrEmpty(connectionString))
            {
                _dbContext = new LumenEntities();
            }
            else
            {
                _dbContext = new LumenEntities(connectionString);
            }

//			_dbContext.Configuration.ProxyCreationEnabled = false;


            _isDisposed = false;
            Thread.BeginThreadAffinity();
            /* Set the current scope to this UnitOfWorkScope object: */
            _currentScope = this;
        }
Пример #2
0
        /// <summary>
        /// Called on the end of the scope. Disposes the ObjectContext.
        /// </summary>
        public void Dispose()
        {
            if (!_isDisposed)
            {
                try {
                    /* End of scope, so clear the thread static
                     * _currentScope member: */
                    _currentScope = null;
                    Thread.EndThreadAffinity();

                    if (_saveAllChangesAtEndOfScope)
                    {
                        // Qui ci potrebbero essere delle eccezioni
                        _dbContext.SaveChanges();
                    }
                } catch (Exception ee) {
                    _giornale.Error("Salvataggio sul db fallito: " + ErroriUtil.estraiMessage(ee), ee);
                    throw ee;
                } finally {
                    // In ogni caso devo chiudere tutto
                    _dbContext.Dispose();
                    _dbContext  = null;
                    _isDisposed = true;

                    _giornale.Debug("-Chiudo Unit-Of-Work");
                }
            }
            else
            {
                _giornale.Warn("Come mai casco qui ?? Impossibile. Debuggare!");
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debugger.Break();
                }
            }
        }