/// <summary>Saves changes made to the data source.</summary>
        /// <returns>Number of records affected by the Save operation; -1 if an
        /// error occurred.</returns>
        /// <remarks>This method will automatically attempt a graceful closure of the handle to the underlying data source in the case that the return value is -1.  If the underlying unit of work has not been initialized, or the database is offline, this method also returns -1.</remarks>
        public int Save()
        {
            if (!EmployeesConnectionTester.IsDatabaseOnline())
            {
                return(-1);
            }

            if (UnitOfWork == null)
            {
                return(-1);                     // unit of work not initialized
            }
            var result = -1;

            try
            {
                result = UnitOfWork.Save();

                // Notify event subscribers that changes have been saved successfully.
                OnChangesSaved(result);
            }
            catch (Exception ex)
            {
                OnExceptionRaised(ex);

                result = -1;
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets up the data context and initializes the database connection.  Checks
        /// whether the underlying database is available.
        /// </summary>
        /// <param name="connectionString">String containing the connection string
        /// to use to access the data source.  Must not be blank.</param>
        /// <returns>True if succeeded; false otherwise.</returns>
        /// <remarks>
        /// This method should be called prior to any actual database operations
        /// for each unit of work.  The reasoning being that this method also ensures
        /// the connection to the underlying database is intact prior to actually
        /// allowing the context to be utilized.
        /// </remarks>
        protected virtual bool OnCreateContext(string connectionString)
        {
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                return(false);
            }

            if (!EmployeesConnectionTester
                .IsDatabaseOnline())
            {
                OnDatabaseNotAvailable();
                return(false);
            }

            // Check if already connected
            if (Context != null)
            {
                return(true);
            }

            var result = true;

            try
            {
                Context = new EmployeesEntities(connectionString);
            }
            catch (Exception ex)
            {
                OnExceptionRaised(ex);

                result = false;
            }
            finally
            {
                if (!result)
                {
                    GracefullyCloseDatabase();
                }
            }

            if (result)
            {
                OnDatabaseConnected();
            }

            return(result);
        }