/// <summary> Delete All records from the table with a transaction. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="sqlTransaction"> The SQL transaction. </param>
        ///
        /// <returns> Number of rows affected (integer) </returns>

        public int DeleteAll <TEntity>(SqlTransaction sqlTransaction) where TEntity : class
        {
            var entityInfo  = RepositorySetting.GetEntityInfo(typeof(TEntity));
            var commandText = string.Format("{0}_DeleteAll", entityInfo.DbObjectName);

            return(SimpleAccess.ExecuteNonQuery(sqlTransaction, commandText, CommandType.StoredProcedure));
        }
        /// <summary> Deletes all the <typeparamref name="TEntity"/> records by given IDs. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="ids"> The identifiers of records. </param>
        ///
        /// <returns> Number of rows affected (integer) </returns>
        public int DeleteAll <TEntity>(IEnumerable <long> ids)
            where TEntity : class
        {
            SqlTransaction sqlTransaction = null;
            int            result         = 0;

            try
            {
                using (sqlTransaction = SimpleAccess.BeginTrasaction())
                {
                    var entityInfo  = RepositorySetting.GetEntityInfo(typeof(TEntity));
                    var commandText = string.Format("{0}_Delete", entityInfo.DbObjectName);

                    foreach (var id in ids)
                    {
                        result += SimpleAccess.ExecuteNonQuery(sqlTransaction, commandText, CommandType.StoredProcedure, new[] { id.ToDataParam("Id") });
                    }
                }
            }
            catch (Exception)
            {
                SimpleAccess.EndTransaction(sqlTransaction, false);
            }
            finally
            {
                SimpleAccess.EndTransaction(sqlTransaction);
            }

            return(result);
        }
        /// <summary> Updates all the given entities. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="entities"> The <![CDATA[IEnumerable<TEntity>]]> to update </param>
        ///
        /// <returns> Number of rows affected (integer) </returns>
        public int UpdateAll <TEntity>(IEnumerable <TEntity> entities)
            where TEntity : class
        {
            SqlTransaction sqlTransaction = null;
            int            result         = 0;

            try
            {
                using (sqlTransaction = SimpleAccess.BeginTrasaction())
                {
                    var    entityInfo  = RepositorySetting.GetEntityInfo(typeof(TEntity));
                    string commandText = string.Format("{0}_Update", entityInfo.DbObjectName);

                    foreach (var entity in entities)
                    {
                        var entityParameters = entityInfo.GetUpdateParameters(entity);

                        result += SimpleAccess.ExecuteNonQuery(sqlTransaction, commandText, CommandType.StoredProcedure
                                                               , entityParameters.DataParametersDictionary.Values.ToArray());

                        entityParameters.LoadOutParametersProperties(entity);
                    }
                }
            }
            catch (Exception)
            {
                SimpleAccess.EndTransaction(sqlTransaction, false);
            }
            finally
            {
                SimpleAccess.EndTransaction(sqlTransaction);
            }

            return(result);
        }
        /// <summary> Soft delete. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="id"> The identifier. </param>
        ///
        /// <returns> . </returns>
        public int SoftDelete <TEntity>(long id)
            where TEntity : class
        {
            //var name = typeof(TEntity).Name;
            var entityInfo  = RepositorySetting.GetEntityInfo(typeof(TEntity));
            var commandText = string.Format("{0}_MarkDelete", entityInfo.DbObjectName);

            return(SimpleAccess.ExecuteNonQuery(commandText, CommandType.StoredProcedure, new [] { id.ToDataParam("id") }));
        }
        /// <summary> Deletes the given ID. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="sqlTransaction"> The SQL transaction. </param>
        /// <param name="sqlParameters">  Options for controlling the SQL. </param>
        ///
        /// <returns> . </returns>
        public virtual int Delete <TEntity>(SqlTransaction sqlTransaction, params SqlParameter[] sqlParameters)
            where TEntity : class
        {
            //var name = typeof(TEntity).Name;
            var entityInfo  = RepositorySetting.GetEntityInfo(typeof(TEntity));
            var commandText = string.Format("{0}_Delete", entityInfo.DbObjectName);

            return(SimpleAccess.ExecuteNonQuery(sqlTransaction, commandText, CommandType.StoredProcedure, sqlParameters));
        }
        /// <summary> Deletes the given dynamic object as SqlParameter names and values. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="paramObject"> The dynamic object as parameters. </param>
        ///
        /// <returns> . </returns>
        public virtual int Delete <TEntity>(object paramObject)
            where TEntity : class
        {
            //var name = typeof(TEntity).Name;
            var entityInfo  = RepositorySetting.GetEntityInfo(typeof(TEntity));
            var commandText = string.Format("{0}_Delete", entityInfo.DbObjectName);

            return(SimpleAccess.ExecuteNonQuery(commandText, CommandType.StoredProcedure, SimpleAccess.BuildSqlParameters(paramObject)));
        }
        /// <summary> Enumerates get all in this collection. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="fieldToSkip"> (optional) the field to skip. </param>
        ///
        /// <returns> An enumerator that allows for each to be used to process get all {TEntity} in this
        /// collection. </returns>

        public virtual IEnumerable <TEntity> GetAll <TEntity>(string fieldToSkip = null)
            where TEntity : new()
        {
            //var name = typeof(TEntity).Name;
            var    entityInfo  = RepositorySetting.GetEntityInfo(typeof(TEntity));
            string commandText = string.Format("{0}_GetAll", entityInfo.DbObjectName);

            return(SimpleAccess.ExecuteEntities <TEntity>(commandText, CommandType.StoredProcedure, fieldToSkip));
        }
        /// <summary> Inserts the given dynamic object as SqlParameter names and values. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="paramObject"> The dynamic object as parameters. </param>
        ///
        /// <returns> . </returns>
        public int Insert <TEntity>(object paramObject)
        {
            //var name = typeof(TEntity).Name;
            var entityInfo = RepositorySetting.GetEntityInfo(typeof(TEntity));

            string commandText = string.Format("{0}_Insert", entityInfo.DbObjectName);

            return(SimpleAccess.ExecuteNonQuery(commandText, CommandType.StoredProcedure, SimpleAccess.BuildSqlParameters(paramObject)));
        }
        /// <summary> Gets. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="transaction"> The transaction. </param>
        /// <param name="sqlParameter"> The SQL parameter. </param>
        /// <param name="fieldToSkip">  (optional) the field to skip. </param>
        ///
        /// <returns> . </returns>
        public TEntity Get <TEntity>(SqlTransaction transaction, SqlParameter sqlParameter, string fieldToSkip = null)
            where TEntity : class, new()
        {
            var entityInfo = RepositorySetting.GetEntityInfo(typeof(TEntity));

            var commandText = string.Format("{0}_GetById", entityInfo.DbObjectName);

            return(SimpleAccess.ExecuteEntity <TEntity>(transaction, commandText, CommandType.StoredProcedure, fieldToSkip, null, new[] { sqlParameter }));
        }
        /// <summary> Find a single <typeparamref name="TEntity"/>. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="transaction"> The transaction. </param>
        /// <param name="expression">The expression.</param>
        /// <param name="fieldToSkip"> (optional) the field to skip. </param>
        ///
        /// <returns> . </returns>
        public IEnumerable <TEntity> Find <TEntity>(SqlTransaction transaction, Expression <Func <TEntity, bool> > expression, string fieldToSkip = null)
            where TEntity : class, new()
        {
            var entityInfo = RepositorySetting.GetEntityInfo(typeof(TEntity));

            var commandText = string.Format("{0}_Find", entityInfo.DbObjectName);

            return(SimpleAccess.ExecuteEntities <TEntity>(transaction, commandText, CommandType.StoredProcedure
                                                          , fieldToSkip, parameters: new SqlParameter("@whereClause", DynamicQuery.GetStoredProcedureWhere(expression, entityInfo))));
        }
        /// <summary> Gets. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="paramObject"> The dynamic object as parameters. </param>
        /// <param name="fieldToSkip">  (optional) the field to skip. </param>
        ///
        /// <returns> . </returns>
        public TEntity Get <TEntity>(object paramObject, string fieldToSkip = null)
            where TEntity : class, new()
        {
            //var name = typeof(TEntity).Name;
            var entityInfo = RepositorySetting.GetEntityInfo(typeof(TEntity));

            var commandText = string.Format("{0}_GetById", entityInfo.DbObjectName);

            return(SimpleAccess.ExecuteEntity <TEntity>(commandText, CommandType.StoredProcedure, paramObject, fieldToSkip));
        }
        /// <summary> Deletes the given ID. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="sqlTransaction">			 The SQL transaction. </param>
        /// <param name="id"> The identifier. </param>
        ///
        /// <returns> . </returns>
        public int Delete <TEntity>(SqlTransaction sqlTransaction, long id)
            where TEntity : class
        {
            //var name = typeof(TEntity).Name;
            var entityInfo = RepositorySetting.GetEntityInfo(typeof(TEntity));

            var commandText = string.Format("{0}_Delete", entityInfo.DbObjectName);
            var result      = SimpleAccess.ExecuteNonQuery(sqlTransaction, commandText, CommandType.StoredProcedure, new [] { id.ToDataParam("Id") });

            return(result);
        }
        //public TEntity Get<TEntity>(long id, string fieldToSkip = null, Dictionary<string, PropertyInfo> piList = null)
        //    where TEntity : new()
        //{
        //    return Get<TEntity>(new SqlParameter("@id", id), fieldToSkip, piList);
        //}

        /// <summary> Gets. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="id">		   The identifier. </param>
        /// <param name="fieldToSkip"> (optional) the field to skip. </param>
        ///
        /// <returns> . </returns>
        public TEntity Get <TEntity>(long id, string fieldToSkip = null)
            where TEntity : class, new()
        {
            var entityInfo = RepositorySetting.GetEntityInfo(typeof(TEntity));

            var commandText = string.Format("{0}_GetById", entityInfo.DbObjectName);

            return(SimpleAccess.ExecuteEntity <TEntity>(commandText, CommandType.StoredProcedure, fieldToSkip, null,
                                                        new SqlParameter("@id", id)));

            // return Get<TEntity>(new SqlParameter("@id", id), transaction, fieldToSkip);
        }
        /// <summary> Deletes all the <typeparamref name="TEntity"/> records by given IDs. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="ids"> The identifiers of records. </param>
        /// <param name="sqlTransaction"> The SQL transaction. </param>
        ///
        /// <returns> Number of rows affected (integer) </returns>
        public int DeleteAll <TEntity>(SqlTransaction sqlTransaction, IEnumerable <long> ids)
            where TEntity : class
        {
            int result = 0;

            var entityInfo  = RepositorySetting.GetEntityInfo(typeof(TEntity));
            var commandText = string.Format("{0}_Delete", entityInfo.DbObjectName);

            foreach (var id in ids)
            {
                result += SimpleAccess.ExecuteNonQuery(sqlTransaction, commandText, CommandType.StoredProcedure, new[] { id.ToDataParam("Id") });
            }

            return(result);
        }
        /// <summary> Updates the given TEntity. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="sqlTransaction">			 The SQL transaction. </param>
        /// <param name="entity"> Entity to insert </param>
        ///
        /// <returns> . </returns>
        public int Update <TEntity>(SqlTransaction sqlTransaction, TEntity entity)
            where TEntity : class
        {
            var entityInfo       = RepositorySetting.GetEntityInfo(typeof(TEntity));
            var entityParameters = entityInfo.GetUpdateParameters(entity);

            string commandText = string.Format("{0}_Update", entityInfo.DbObjectName);

            var result = SimpleAccess.ExecuteNonQuery(sqlTransaction, commandText, CommandType.StoredProcedure
                                                      , entityParameters.DataParametersDictionary.Values.ToArray());

            entityParameters.LoadOutParametersProperties(entity);

            return(result);
        }
        /// <summary> Inserts the given SQL parameters. </summary>
        ///
        /// <typeparam name="TEntity"> Type of the entity. </typeparam>
        /// <param name="sqlTransaction">			 The SQL transaction. </param>
        /// <param name="entities"> The <![CDATA[IEnumerable<TEntity>]]> to insert </param>
        ///
        /// <returns> The number of affected records</returns>
        public int InsertAll <TEntity>(SqlTransaction sqlTransaction, IEnumerable <TEntity> entities)
            where TEntity : class
        {
            int    result      = 0;
            var    entityInfo  = RepositorySetting.GetEntityInfo(typeof(TEntity));
            string commandText = string.Format("[dbo].{0}_Insert", entityInfo.DbObjectName);

            foreach (var entity in entities)
            {
                var entityParameters = entityInfo.GetInsertParameters(entity);

                result += SimpleAccess.ExecuteNonQuery(sqlTransaction, commandText, CommandType.StoredProcedure
                                                       , entityParameters.DataParametersDictionary.Values.ToArray());

                entityParameters.LoadOutParametersProperties(entity);
            }
            return(result);
        }