Esempio n. 1
0
        public async Task <int> UpdateAsync <TEntity>(TEntity entity, object id) where TEntity : class, new()
        {
            bool _returnedEffectedRow = false;
            var  _dbconnection        = _connection;

            //if (_dbconnection.State == ConnectionState.Closed)
            //{
            //    _dbconnection.Open();
            //}

            try
            {
                _returnedEffectedRow = await _connection.UpdateAsync <TEntity>(entity, transaction : _transaction);
            }
            catch (Exception ex)
            {
                NlogLoggerClient _client = new NlogLoggerClient(new NLogFileLogger(_classFullName));
                await _client.ActionLoggerAsync(LogginLevels.LoggingLevels.Error, MethodBase.GetCurrentMethod().Name, ex.Message, ex);
            }
            //finally
            //{
            //    if (_dbconnection.State == ConnectionState.Open)
            //    {
            //        _dbconnection.Close();
            //    }
            //}



            return(_returnedEffectedRow ? 1 : 0);
        }
Esempio n. 2
0
        public int Add <TEntity>(TEntity entity, bool isRowEffected = false) where TEntity : class, new()
        {
            int _returnedEffectedRow = 0;
            var _dbconnection        = _connection;

            //if (_dbconnection.State == ConnectionState.Closed)
            //{
            //    _dbconnection.Open();
            //}
            try
            {
                _returnedEffectedRow = (int)_connection.Insert <TEntity>(entity, transaction: _transaction);
            }
            catch (Exception ex)
            {
                NlogLoggerClient _client = new NlogLoggerClient(new NLogFileLogger(_classFullName));
                _client.ActionLoggerAsync(LogginLevels.LoggingLevels.Error, MethodBase.GetCurrentMethod().Name, ex.Message, ex).Wait();
            }
            finally
            {
                if (_dbconnection.State == ConnectionState.Open)
                {
                    _dbconnection.Close();
                }
            }


            return(_returnedEffectedRow);
        }
Esempio n. 3
0
        public IEnumerable <TEntity> FindByKey <TEntity>(Expression <Func <TEntity, bool> > keyFilter = null) where TEntity : class, new()
        {
            IEnumerable <TEntity> _entity = null;
            var _dbconnection             = _connection;

            //if (_dbconnection.State == ConnectionState.Closed)
            //{
            //    _dbconnection.Open();
            //}

            try
            {
                _entity = _connection.Query <TEntity>(new ConvertTEntityToRawSql <TEntity>().BuildRawSQLScript(SQLQuereyOptions._SQLQuereyOptions.GetByKeyFilter, keyFilter), null, transaction: _transaction);
            }
            catch (Exception ex)
            {
                NlogLoggerClient _client = new NlogLoggerClient(new NLogFileLogger(_classFullName));
                _client.ActionLoggerAsync(LogginLevels.LoggingLevels.Error, MethodBase.GetCurrentMethod().Name, ex.Message, ex).Wait();
            }
            //finally
            //{
            //    if (_dbconnection.State == ConnectionState.Open)
            //    {
            //        _dbconnection.Close();
            //    }
            //}


            return(_entity);
        }
Esempio n. 4
0
        public async Task <int> RemoveByKeyFilterAsync <TEntity>(Expression <Func <TEntity, bool> > keyFilter = null) where TEntity : class, new()
        {
            int _entity       = 0;
            var _dbconnection = _connection;

            //if (_dbconnection.State == ConnectionState.Closed)
            //{
            //    _dbconnection.Open();
            //}

            try
            {
                _entity = await _connection.ExecuteScalarAsync <int>(new ConvertTEntityToRawSql <TEntity>().BuildRawSQLScript(SQLQuereyOptions._SQLQuereyOptions.Delete, keyFilter), null, transaction : _transaction);
            }
            catch (Exception ex)
            {
                NlogLoggerClient _client = new NlogLoggerClient(new NLogFileLogger(_classFullName));
                await _client.ActionLoggerAsync(LogginLevels.LoggingLevels.Error, MethodBase.GetCurrentMethod().Name, ex.Message, ex);
            }
            //finally
            //{
            //    if (_dbconnection.State == ConnectionState.Open)
            //    {
            //        _dbconnection.Close();
            //    }
            //}



            return(_entity);
        }
Esempio n. 5
0
        public async Task <TEntity> FindByIDAsync <TEntity>(object id) where TEntity : class, new()
        {
            TEntity _entity       = null;
            var     _dbconnection = _connection;

            //if (_dbconnection.State == ConnectionState.Closed)
            //{
            //    _dbconnection.Open();
            //}

            try
            {
                _entity = await _connection.GetAsync <TEntity>(id, transaction : _transaction);
            }
            catch (Exception ex)
            {
                NlogLoggerClient _client = new NlogLoggerClient(new NLogFileLogger(_classFullName));
                await _client.ActionLoggerAsync(LogginLevels.LoggingLevels.Error, MethodBase.GetCurrentMethod().Name, ex.Message, ex);
            }
            //finally
            //{
            //    if (_dbconnection.State == ConnectionState.Open)
            //    {
            //        _dbconnection.Close();
            //    }
            //}



            return(_entity);
        }
Esempio n. 6
0
        public IEnumerable <TEntity> FindAll <TEntity>() where TEntity : class, new()
        {
            IEnumerable <TEntity> _entity = null;
            var _dbconnection             = _connection;

            //if (_dbconnection.State == ConnectionState.Closed)
            //{
            //    _dbconnection.Open();
            //}

            try
            {
                _entity = _connection.GetAll <TEntity>(transaction: _transaction);
            }
            catch (Exception ex)
            {
                NlogLoggerClient _client = new NlogLoggerClient(new NLogFileLogger(_classFullName));
                _client.ActionLoggerAsync(LogginLevels.LoggingLevels.Error, MethodBase.GetCurrentMethod().Name, ex.Message, ex).Wait();
            }
            //finally
            //{
            //    if (_dbconnection.State == ConnectionState.Open)
            //    {
            //        _dbconnection.Close();
            //    }
            //}



            return(_entity);
        }
Esempio n. 7
0
        public DbConnection Create(DbProvidersOptions dbProvidersOptions)
        {
            try
            {
                switch (dbProvidersOptions)
                {
                case DbProvidersOptions.MsSqlServer:
                    _dbConnection = new SqlConnection(GetConnectionString());
                    break;

                case DbProvidersOptions.Oracle:
                    //_dbConnection = new System.Data.OracleClient.OracleConnection(GetConnectionString());
                    break;

                case DbProvidersOptions.Sybase:
                    _dbConnection = new AseConnection(GetConnectionString());
                    break;

                case DbProvidersOptions.SQLLite:
                    _dbConnection = new SQLiteConnection(GetConnectionString());
                    break;

                case DbProvidersOptions.MySql:
                    _dbConnection = new MySqlConnection(GetConnectionString());
                    break;
                }
            }
            catch (Exception ex)
            {
                NlogLoggerClient _client = new NlogLoggerClient(new NLogFileLogger(_classFullName));
                _client.ActionLoggerAsync(LogginLevels.LoggingLevels.Error, MethodBase.GetCurrentMethod().Name, ex.Message, ex).Wait();
            }
            return(_dbConnection);
        }
Esempio n. 8
0
        public async Task <List <DbParameter> > DbParametersListGeneratorAsync(object model, DbParameter dbParameter)
        {
            List <DbParameter> _dbParameters = new List <DbParameter>();

            try
            {
                _dbParameters = await Task.Run(() => {
                    List <DbParameter> _list          = new List <DbParameter>();
                    PropertyInfo[] _paramPropertyInfo = model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
                    foreach (PropertyInfo item in _paramPropertyInfo)
                    {
                        SqlDataTypeAttribute _sqlDataTypeAttribute = (SqlDataTypeAttribute)item.GetCustomAttribute(typeof(SqlDataTypeAttribute));
                        ParameterAttribute _parameterAttribute     = (ParameterAttribute)item.GetCustomAttribute(typeof(ParameterAttribute));
                        var _value = item.GetValue(model);
                        DbParameter _dbParameter   = (DbParameter) new TParameter();
                        _dbParameter.DbType        = _sqlDataTypeAttribute.SqlDbType;
                        _dbParameter.ParameterName = _parameterAttribute.value;
                        _dbParameter.Value         = _value == null? DBNull.Value : _value;
                        _list.Add(_dbParameter);
                    }
                    return(_list);
                });
            }
            catch (Exception ex)
            {
                NlogLoggerClient _client = new NlogLoggerClient(new NLogFileLogger(_classNameSpace));
                await _client.ActionLoggerAsync(LogginLevels.LoggingLevels.Error, MethodBase.GetCurrentMethod().Name, ex.Message, ex);

                throw new Exception(ex.Message, ex);
            }
            return(_dbParameters);
        }
        public static string ExpressionTreeToString(this Expression expression)
        {
            string _returnedWhereCluase = string.Empty;

            try
            {
                _returnedWhereCluase = ExpressionValue(expression);
            }
            catch (Exception ex)
            {
                NlogLoggerClient _client = new NlogLoggerClient(new NLogFileLogger("Aregak_Framework_DataRepository.DapperDataRepository.DataRepository<TEntity>"));
                _client.ActionLoggerAsync(LogginLevels.LoggingLevels.Error, MethodBase.GetCurrentMethod().Name, ex.Message, ex).Wait();
            }
            return(_returnedWhereCluase);
        }
Esempio n. 10
0
        public async Task <List <DbParameter> > DbParametersListGeneratorAsync(object model, DbParameter dbParameter)
        {
            List <DbParameter> _dbParameters = new List <DbParameter>();

            try
            {
                _dbParameters = await _dapperSqlParameter.DbParametersListGeneratorAsync(model, dbParameter);
            }
            catch (Exception ex)
            {
                NlogLoggerClient _client = new NlogLoggerClient(new NLogFileLogger(_classNameSpace));
                await _client.ActionLoggerAsync(LogginLevels.LoggingLevels.Error, MethodBase.GetCurrentMethod().Name, ex.Message, ex);
            }
            return(_dbParameters);
        }
Esempio n. 11
0
        public async Task <IEnumerable <TEntity> > ExecuteQueryAsync <TEntity>(string Query, params object[] _params) where TEntity : class, new()
        {
            IEnumerable <TEntity> _entity = null;
            var _dbconnection             = _connection;

            //if (_dbconnection.State == ConnectionState.Closed)
            //{
            //    _dbconnection.Open();
            //}

            try
            {
                if (_params.Length == 0)
                {
                    _entity = await _connection.QueryAsync <TEntity>(Query, null, transaction : _transaction);
                }
                else
                {
                    var           _dynamicpara  = new DynamicParameters();
                    DbParameter[] sqlParameters = (DbParameter[])_params;
                    foreach (var item in sqlParameters)
                    {
                        _dynamicpara.Add(item.ParameterName, item.Value, item.DbType);
                    }

                    _entity = await _connection.QueryAsync <TEntity>(Query, _dynamicpara, transaction : _transaction);
                }
            }
            catch (Exception ex)
            {
                NlogLoggerClient _client = new NlogLoggerClient(new NLogFileLogger(_classFullName));
                await _client.ActionLoggerAsync(LogginLevels.LoggingLevels.Error, MethodBase.GetCurrentMethod().Name, ex.Message, ex);
            }
            //finally
            //{
            //    if (_dbconnection.State == ConnectionState.Open)
            //    {
            //        _dbconnection.Close();
            //    }
            //}



            return(_entity);
        }
        public string BuildRawSQLScript(SQLQuereyOptions._SQLQuereyOptions quereyOptions, Expression <Func <TEntity, bool> > keyFilter = null)
        {
            string _executebleQuerey = string.Empty;

            try
            {
                switch (quereyOptions)
                {
                case SQLQuereyOptions._SQLQuereyOptions.Insert:
                    break;

                case SQLQuereyOptions._SQLQuereyOptions.Update:
                    break;

                case SQLQuereyOptions._SQLQuereyOptions.Delete:
                    _executebleQuerey = DeleteWithKeyFilter(keyFilter);
                    break;

                case SQLQuereyOptions._SQLQuereyOptions.GetAll:
                    break;

                case SQLQuereyOptions._SQLQuereyOptions.GetByID:
                    break;

                case SQLQuereyOptions._SQLQuereyOptions.GetByKeyFilter:
                    _executebleQuerey = SelectWithKeyFilter(keyFilter);
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                NlogLoggerClient _client = new NlogLoggerClient(new NLogFileLogger("Application_DataRepository.DapperDataRepository.DataRepository<TEntity>"));
                _client.ActionLoggerAsync(LogginLevels.LoggingLevels.Error, MethodBase.GetCurrentMethod().Name, ex.Message, ex).Wait();
            }
            return(_executebleQuerey);
        }