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; } }
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); }
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)); }
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; } }
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)); }
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); } }
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); }
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 }); }
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); }
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; } }
public void ClassMapperInterface_Returns_DefinedMapper() { var mapper = DapperAsyncExtensions.GetMap <EntityWithInterfaceMapper>().Result; Assert.AreEqual(typeof(EntityWithInterfaceMapperMapper), mapper.GetType()); }
public void ClassMapperDescendant_Returns_DefinedClass() { var mapper = DapperAsyncExtensions.GetMap <EntityWithMapper>().Result; Assert.AreEqual(typeof(EntityWithMapperMapper), mapper.GetType()); }
public void NoMappingClass_ReturnsDefaultMapper() { var mapper = DapperAsyncExtensions.GetMap <EntityWithoutMapper>().Result; Assert.AreEqual(typeof(AutoClassMapper <EntityWithoutMapper>), mapper.GetType()); }