/// <summary> /// Constructor containing a <see cref="DALCTransaction"/> for data access logic component(s) that /// need to participate in a transaction. /// </summary> /// <param name="transaction">The <see cref="DALCTransaction"/> to be used for this data operation.</param> protected CommonDALC(DALCTransaction transaction) { // Assign the DALCTransaction object _transaction = transaction; // Set in transaction flag. The IDbTransaction may be null if the BusinessFacade was // instantiated to not do transactioning. A DALCTransaction is still created, just // without the inner IDbTransaction. if (transaction == null || transaction.GetTransaction() == null) { _isInTransaction = false; } else { _isInTransaction = true; } }
/// <summary> /// This method executes a query against the database and returns the results as a /// <see cref="DataTransferObject"/>. The primary purpose of this method is to retrieve /// a single entity (record) from the database (master) that may have associated child /// records (details). /// </summary> /// <param name="helper">The <see cref="DALCHelper"/> class that contains the database /// command information.</param> /// <param name="criteria">The <see cref="DataTransferObject"/> containing the criteria /// to be used to determine mathing records in the database.</param> /// <returns>The <see cref="DataTransferObject"/> now populated with the record found /// in the database matching the specified criteria.</returns> protected DataTransferObject ExecuteQueryDto(DALCHelper helper, DataTransferObject criteria) { // Initialize objects IDataReader reader = null; Database db = null; DbCommandWrapper cw = null; // Initialize output parameters DataTransferObject dto = null; // If the passed in helper is null throw exception if (null == helper) { throw new MNNonFatalException("CommonDALC.ExecuteQueryDto was passed a null DALCHelper."); } // Make sure any necessary criteria has been set if (helper.CriteriaIsValid(criteria) == false) { throw new MNNonFatalException("CommonDALC.ExecuteQueryDto was passed invalid search criteria."); } try { // Initialize the database and command wrapper if (helper.DbInstanceName() == null) { db = DatabaseFactory.CreateDatabase(); } else { db = DatabaseFactory.CreateDatabase(helper.DbInstanceName()); } cw = helper.InitializeCommand(db, criteria); DbCommand command = cw.Command; // Execute a data reader if (_isInTransaction) { reader = db.ExecuteReader(command, _transaction.GetTransaction()); } else { reader = db.ExecuteReader(command); } // set command for garbage collection command = null; // Convert the results into a data transfer object dto = helper.ConvertResultsDto(cw, reader); } catch (MNException e) { e.AddMessageTraceData("MNException caught in CommonDALC.ExecuteQueryDto"); throw; } catch (Exception e) { MNException ex = new MNException(e.Message, e, criteria); ex.AddMessageTraceData("Exception caught in CommonDALC.ExecuteQueryDto"); throw ex; } finally { // Close the data reader if it has not already been closed if ((null != reader) && (reader.IsClosed == false)) { reader.Close(); } // Set the database object for garbage collection if (db != null) { db = null; } // Set the command wrapper object for garbage collection if (cw != null) { cw = null; } } // Return the data transfer object return(dto); }