/// <summary>
        /// Add a new MaterialGroup to the database
        /// </summary>
        public virtual void Add(Model.MaterialGroup newMaterialGroup)
        {
            try
            {
                Trace.WriteVerbose("({0})", Trace.GetMethodName(), CLASSNAME, newMaterialGroup.ToString());
                var helper = Database.GetDbHelper();


                int recordsAffected = helper.ExecuteSPNonQuery(_storedProcedure_Add,
                                                               helper.CreateInputParam("@Id", newMaterialGroup.Id),
                                                               helper.CreateInputParam("@Code", newMaterialGroup.Code),
                                                               helper.CreateInputParam("@Description", newMaterialGroup.Description));

                if (recordsAffected == 0)
                {
                    throw new DalNothingUpdatedException("Unable to add MaterialGroup with Id={0}", newMaterialGroup);
                }

                return;
            }
            catch (Exception ex)
            {
                Trace.WriteError("({0})", Trace.GetMethodName(), CLASSNAME, ex, newMaterialGroup.ToString());
                throw DbHelper.TranslateException(ex);
            }
        }
        /// <summary>
        /// Delete the given MaterialGroup from the database
        /// </summary>
        public virtual void Delete(Model.MaterialGroup delMaterialGroup)
        {
            try
            {
                Trace.WriteInformation("({0})", Trace.GetMethodName(), CLASSNAME, delMaterialGroup);

                //Begin Checks
                if (!Exists(delMaterialGroup))
                {
                    throw new BusinessException(string.Format("There is no MaterialGroup with this id. ({0})", delMaterialGroup));
                }

                DataAccess.MaterialGroups materialGroups = new DataAccess.MaterialGroups();
                materialGroups.Delete(delMaterialGroup);
            }
            catch (DalForeignKeyException ex_fk)
            {
                Trace.WriteError("({0})", Trace.GetMethodName(), CLASSNAME, ex_fk, delMaterialGroup);
                throw new BusinessException(string.Format("The MaterialGroup is still used by {0}", ex_fk.Table), ex_fk);
            }
            catch (Exception ex)
            {
                Trace.WriteError("({0})", Trace.GetMethodName(), CLASSNAME, ex, delMaterialGroup);
                throw;
            }
        }
        /// <summary>
        /// Get a MaterialGroup by id from the database
        /// </summary>
        public virtual Model.MaterialGroup GetById(Int32 id)
        {
            DbDataReader reader = null;

            try
            {
                var helper = Database.GetDbHelper();

                reader = helper.ExecuteSPReader(_storedProcedure_GetById,
                                                helper.CreateInputParam("@Id", id));

                Model.MaterialGroup result = null;

                if (reader.Read())
                {
                    result = CreateMaterialGroup(reader);
                }

                return(result);
            }
            catch (Exception ex)
            {
                Trace.WriteError("{0}", Trace.GetMethodName(), CLASSNAME, ex, id);
                throw DbHelper.TranslateException(ex);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
 /// <summary>
 /// Equals function to compare class
 /// </summary>
 public virtual bool Exists(Model.MaterialGroup materialGroup)
 {
     try
     {
         return(this.GetById(materialGroup.Id) != null);
     }
     catch (Exception ex)
     {
         Trace.WriteError("({0})", Trace.GetMethodName(), CLASSNAME, ex, materialGroup);
         throw;
     }
 }
 /// <summary>
 /// Get a MaterialGroup by id from the database
 /// </summary>
 public virtual Model.MaterialGroup GetById(Int32 id)
 {
     try
     {
         DataAccess.MaterialGroups materialGroups = new DataAccess.MaterialGroups();
         Model.MaterialGroup       result         = materialGroups.GetById(id);
         return(result);
     }
     catch (Exception ex)
     {
         Trace.WriteError("{0}", Trace.GetMethodName(), CLASSNAME, ex, id);
         throw;
     }
 }
        /// <summary>
        /// Delete the given MaterialGroup from the database
        /// </summary>
        public virtual void Delete(Model.MaterialGroup materialGroup)
        {
            try
            {
                Trace.WriteVerbose("({0})", Trace.GetMethodName(), CLASSNAME, materialGroup.ToString());

                var helper = Database.GetDbHelper();
                helper.ExecuteSPNonQuery(_storedProcedure_Delete,
                                         helper.CreateInputParam("@Id", materialGroup.Id));
            }
            catch (Exception ex)
            {
                Trace.WriteError("({0})", Trace.GetMethodName(), CLASSNAME, ex, materialGroup.ToString());
                throw DbHelper.TranslateException(ex);
            }
        }
 /// <summary>
 /// Create a Model.MaterialGroup
 /// </summary>
 protected virtual Model.MaterialGroup CreateMaterialGroup(DbDataReader reader)
 {
     try
     {
         Model.MaterialGroup result = new Model.MaterialGroup(
             (Int32)reader["Id"],
             (String)reader["Code"],
             (String)reader["Description"]
             );
         return(result);
     }
     catch (Exception ex)
     {
         Trace.WriteError("", Trace.GetMethodName(), CLASSNAME, ex);
         throw DbHelper.TranslateException(ex);
     }
 }
        /// <summary>
        /// Add a new MaterialGroup to the database
        /// </summary>
        public virtual void Add(Model.MaterialGroup newMaterialGroup)
        {
            try
            {
                Trace.WriteInformation("({0})", Trace.GetMethodName(), CLASSNAME, newMaterialGroup);

                CheckConstraints(newMaterialGroup);
                DataAccess.MaterialGroups materialGroups = new DataAccess.MaterialGroups();

                materialGroups.Add(newMaterialGroup);
            }
            catch (DalForeignKeyException ex_fk)
            {
                Trace.WriteError("({0})", Trace.GetMethodName(), CLASSNAME, ex_fk, newMaterialGroup);
                throw new BusinessException(string.Format("No related object found in {0}", ex_fk.Table), ex_fk);
            }
            catch (Exception ex)
            {
                Trace.WriteError("({0})", Trace.GetMethodName(), CLASSNAME, ex, newMaterialGroup);
                throw;
            }
        }
        /// <summary>
        /// Check Datafield constraints
        /// </summary>
        protected virtual void CheckConstraints(Model.MaterialGroup materialGroup)
        {
            //Range checks, etc checks go here
            if (materialGroup.Code == null)
            {
                throw new BusinessException(string.Format("Code may not be NULL. ({0})", materialGroup.Code));
            }

            if (materialGroup.Code.Length > 50)
            {
                throw new BusinessException(string.Format("Code may not be longer than 50 characters. ({0})", materialGroup.Code));
            }

            if (materialGroup.Description == null)
            {
                throw new BusinessException(string.Format("Description may not be NULL. ({0})", materialGroup.Description));
            }

            if (materialGroup.Description.Length > 50)
            {
                throw new BusinessException(string.Format("Description may not be longer than 50 characters. ({0})", materialGroup.Description));
            }
        }
        /// <summary>
        /// Modify the given MaterialGroup in the database
        /// </summary>
        public virtual void Modify(Model.MaterialGroup modifiedMaterialGroup)
        {
            try
            {
                Trace.WriteInformation("({0})", Trace.GetMethodName(), CLASSNAME, modifiedMaterialGroup);

                //Begin Checks
                CheckConstraints(modifiedMaterialGroup);

                if (!Exists(modifiedMaterialGroup))
                {
                    throw new BusinessException(string.Format("There is no MaterialGroup with this id. ({0})", modifiedMaterialGroup));
                }

                DataAccess.MaterialGroups materialGroups = new DataAccess.MaterialGroups();
                materialGroups.Modify(modifiedMaterialGroup);
            }
            catch (Exception ex)
            {
                Trace.WriteError("({0})", Trace.GetMethodName(), CLASSNAME, ex, modifiedMaterialGroup);
                throw;
            }
        }
        /// <summary>
        /// Modify the given MaterialGroup in the database
        /// </summary>
        public virtual void Modify(Model.MaterialGroup modifiedMaterialGroup)
        {
            try
            {
                Trace.WriteVerbose("({0})", Trace.GetMethodName(), CLASSNAME, modifiedMaterialGroup.ToString());

                var helper          = Database.GetDbHelper();
                int recordsAffected = helper.ExecuteSPNonQuery(_storedProcedure_Modify,
                                                               helper.CreateInputParam("@Id", modifiedMaterialGroup.Id),
                                                               helper.CreateInputParam("@Code", modifiedMaterialGroup.Code),
                                                               helper.CreateInputParam("@Description", modifiedMaterialGroup.Description));

                if (recordsAffected == 0)
                {
                    throw new DalNothingUpdatedException("No records were updated (Table: MaterialGroups). MaterialGroup=" + modifiedMaterialGroup.ToString());
                }
            }
            catch (Exception ex)
            {
                Trace.WriteError("({0})", Trace.GetMethodName(), CLASSNAME, ex, modifiedMaterialGroup.ToString());
                throw DbHelper.TranslateException(ex);
            }
        }