Beispiel #1
0
        public async Task <bool> DeleteAsync <T>(T entity, Enum product) where T : class
        {
            try
            {
                if (entity == null)
                {
                    return(false);
                }

                var assemblies = new List <Assembly> {
                    Assembly.GetAssembly(typeof(T))
                };
                DapperAsyncExtensions.SetMappingAssemblies(assemblies);


                return(await WithConnection(async c =>
                {
                    await c.DeleteAsync(entity);
                    return true;
                }, product));
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message + "------" + ex.StackTrace);
                throw;
            }
        }
Beispiel #2
0
        protected override void CommonSetup(DbConnection connection, SqlDialectBase sqlDialect)
        {
            var config       = DapperAsyncExtensions.Configure(typeof(AutoClassMapper <>), new List <Assembly>(), sqlDialect);
            var sqlGenerator = new SqlGeneratorImpl(config);

            Db = new AsyncDatabase(connection, sqlGenerator);
        }
Beispiel #3
0
        public async Task <IEnumerable <T> > GetListAsync <T>(object parameters, Enum product) where T : class
        {
            var assemblies = new List <Assembly> {
                Assembly.GetAssembly(typeof(T))
            };

            DapperAsyncExtensions.SetMappingAssemblies(assemblies);

            string query = "Select * from [{tablename}] ";

            query = query.Replace("{tablename}", typeof(T).Name);

            List <string> whereclause = new List <string>();

            foreach (var prop in parameters.GetType().GetProperties())
            {
                var propValue = prop.GetValue(parameters, null);

                if (propValue == null)
                {
                    whereclause.Add(prop.Name + " is null");
                }
                else
                {
                    whereclause.Add(prop.Name + "=" + "@" + prop.Name);
                }
            }
            if (whereclause.Any())
            {
                query = query + " where " + string.Join(" and ", whereclause);
            }

            return(await WithConnection(async c => await c.QueryAsync <T>(query, parameters, commandType: CommandType.Text), product));
        }
Beispiel #4
0
        public async Task <bool> UpdateAsync <T>(T entity, Enum product) where T : class
        {
            try
            {
                if (entity == null)
                {
                    return(false);
                }

                var assemblies = new List <Assembly> {
                    Assembly.GetAssembly(typeof(T))
                };
                DapperAsyncExtensions.SetMappingAssemblies(assemblies);

                var t   = typeof(T);
                var pro = t.GetProperty("ModifiedOn");
                if (pro != null)
                {
                    pro.SetValue(entity, DateTime.Now, null);
                }

                return(await WithConnection(async c =>
                {
                    await c.UpdateAsync(entity);
                    return true;
                }, product));
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message + "------" + ex.StackTrace);
                throw;
            }
        }
Beispiel #5
0
        public async Task <IEnumerable <T> > GetListAsync <T>(Enum product) where T : class
        {
            var assemblies = new List <Assembly> {
                Assembly.GetAssembly(typeof(T))
            };

            DapperAsyncExtensions.SetMappingAssemblies(assemblies);

            return(await WithConnection(async c => await c.GetListAsync <T>(), product));
        }
Beispiel #6
0
            public void GetMultiple_DoesNotDuplicate()
            {
                List <Guid> list = new List <Guid>();

                for (int i = 0; i < 1000; i++)
                {
                    Guid id = DapperAsyncExtensions.GetNextGuid().Result;
                    Assert.IsFalse(list.Contains(id));
                    list.Add(id);
                }
            }
Beispiel #7
0
            public void MappingClass_ReturnsFromDifferentAssembly()
            {
                DapperAsyncExtensions.SetMappingAssemblies(new[] { typeof(ExternallyMappedMap).Assembly });
                var mapper = DapperAsyncExtensions.GetMap <ExternallyMapped>().Result;

                Assert.AreEqual(typeof(ExternallyMappedMap.ExternallyMappedMapper), mapper.GetType());

                DapperAsyncExtensions.SetMappingAssemblies(null);
                mapper = DapperAsyncExtensions.GetMap <ExternallyMapped>().Result;
                Assert.AreEqual(typeof(AutoClassMapper <ExternallyMapped>), mapper.GetType());
            }
        public virtual async Task <IEnumerable <TEntity> > ObterTodosAsync()
        {
            IEnumerable <TEntity> retorno = default(IEnumerable <TEntity>);

            try
            {
                using (var conn = _dbContext.GetConnection())
                {
                    retorno = await DapperAsyncExtensions.GetListAsync <TEntity>(conn);
                }
            }
            catch (Exception ex)
            {
                _logger.Write(ex);
                throw;
            }
            return(retorno);
        }
        public virtual async Task <TEntity> ObterPorIdAsync(int id)
        {
            TEntity retorno = default(TEntity);

            try
            {
                using (var conn = _dbContext.GetConnection())
                {
                    retorno = await DapperAsyncExtensions.GetAsync <TEntity>(conn, id);
                }
            }
            catch (Exception ex)
            {
                _logger.Write(ex);
                throw;
            }

            return(retorno);
        }
Beispiel #10
0
        private static void ConfigureDapper(HostBuilderContext hostBuilderContext, IServiceCollection services)
        {
            services.AddSingleton <DbConnectionStringProvider>(new DbConnectionStringProvider(DbConnectionString(hostBuilderContext)));

            if (hostBuilderContext.HostingEnvironment.IsDevelopment())
            {
                DapperExtensions.DapperExtensions.SqlDialect = new SqliteDialect();
                DapperAsyncExtensions.SqlDialect             = new SqliteDialect();
                services.AddScoped <IUnitOfWork, Paramore.Brighter.Sqlite.Dapper.UnitOfWork>();
            }
            else
            {
                DapperExtensions.DapperExtensions.SqlDialect = new MySqlDialect();
                DapperAsyncExtensions.SqlDialect             = new MySqlDialect();
                services.AddScoped <IUnitOfWork, Paramore.Brighter.MySql.Dapper.UnitOfWork>();
            }

            DapperExtensions.DapperExtensions.SetMappingAssemblies(new[] { typeof(SalutationMapper).Assembly });
            DapperAsyncExtensions.SetMappingAssemblies(new[] { typeof(SalutationMapper).Assembly });
        }
Beispiel #11
0
        private void ConfigureDapper(IServiceCollection services)
        {
            services.AddSingleton <DbConnectionStringProvider>(new DbConnectionStringProvider(DbConnectionString()));

            if (_env.IsDevelopment())
            {
                DapperExtensions.DapperExtensions.SqlDialect = new SqliteDialect();
                DapperAsyncExtensions.SqlDialect             = new SqliteDialect();
                services.AddScoped <IUnitOfWork, Paramore.Brighter.Sqlite.Dapper.UnitOfWork>();
            }
            else
            {
                DapperExtensions.DapperExtensions.SqlDialect = new MySqlDialect();
                DapperAsyncExtensions.SqlDialect             = new MySqlDialect();
                services.AddScoped <IUnitOfWork, Paramore.Brighter.MySql.Dapper.UnitOfWork>();
            }

            DapperExtensions.DapperExtensions.SetMappingAssemblies(new[] { typeof(PersonMapper).Assembly });
            DapperAsyncExtensions.SetMappingAssemblies(new[] { typeof(PersonMapper).Assembly });
        }
        public virtual async Task <bool> RemoverAsync(TEntity entidade)
        {
            bool retorno = false;

            try
            {
                using (var conn = _dbContext.GetConnection())
                {
                    await DapperAsyncExtensions.DeleteAsync <TEntity>(conn, entidade);
                }

                retorno = true;
            }
            catch (Exception ex)
            {
                _logger.Write(ex);
                throw;
            }

            return(retorno);
        }
Beispiel #13
0
        public async Task <IEnumerable <decimal> > InsertMultipleAsync <T>(List <T> entities, Enum product)
            where T : class
        {
            try
            {
                if (!entities.Any())
                {
                    return(null);
                }
                var assemblies = new List <Assembly> {
                    Assembly.GetAssembly(typeof(T))
                };
                DapperAsyncExtensions.SetMappingAssemblies(assemblies);

                var t      = typeof(T);
                var mapper = DapperExtensions.DapperExtensions.GetMap <T>();

                var tableName = mapper.TableName;

                var query = "insert into " + tableName + " (##columns)  values (##values);SELECT SCOPE_IDENTITY();";

                var columns = new List <string>();
                var values  = new List <string>();

                for (var i = 0; i < entities.Count; i++)
                {
                    foreach (var prop in mapper.Properties)
                    {
                        if (prop.KeyType == KeyType.Identity)
                        {
                            continue;
                        }
                        columns.Add(prop.ColumnName);
                        values.Add("@" + prop.Name + i);
                    }

                    query = query.Replace("##columns", string.Join(",", columns))
                            .Replace("##values", string.Join(",", values));
                }

                var p = new DynamicParameters();
                for (var i = 0; i < entities.Count; i++)
                {
                    foreach (var prop in mapper.Properties)
                    {
                        if (prop.KeyType == KeyType.Identity)
                        {
                            continue;
                        }
                        columns.Add(prop.ColumnName);
                        values.Add(prop.Name + i);

                        var pro = t.GetProperty(prop.Name);
                        if (pro != null)
                        {
                            p.Add(prop.Name + i, pro.GetValue(entities[i], null));
                        }
                    }
                }


                return(await WithConnection(async c =>
                {
                    var aa = await c.QueryMultipleAsync(query, p, commandType: CommandType.Text);
                    var identityColumns = await aa.ReadAsync <decimal>();
                    return identityColumns;
                }, product));
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message + "------" + ex.StackTrace);
                throw;
            }
        }
Beispiel #14
0
            public void ClassMapperInterface_Returns_DefinedMapper()
            {
                var mapper = DapperAsyncExtensions.GetMap <EntityWithInterfaceMapper>().Result;

                Assert.AreEqual(typeof(EntityWithInterfaceMapperMapper), mapper.GetType());
            }
Beispiel #15
0
            public void ClassMapperDescendant_Returns_DefinedClass()
            {
                var mapper = DapperAsyncExtensions.GetMap <EntityWithMapper>().Result;

                Assert.AreEqual(typeof(EntityWithMapperMapper), mapper.GetType());
            }
Beispiel #16
0
            public void NoMappingClass_ReturnsDefaultMapper()
            {
                var mapper = DapperAsyncExtensions.GetMap <EntityWithoutMapper>().Result;

                Assert.AreEqual(typeof(AutoClassMapper <EntityWithoutMapper>), mapper.GetType());
            }