Skip to content

Instantly share code, notes, and snippets.

@topnotch48
Created December 21, 2017 10:44
Show Gist options
  • Save topnotch48/0c8f2e4d72b3fde42c114ce97a13489a to your computer and use it in GitHub Desktop.
Save topnotch48/0c8f2e4d72b3fde42c114ce97a13489a to your computer and use it in GitHub Desktop.
General Repo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using AutoMapper;
using CardDisputes.Db.Olap.Contracts;
using CardDisputes.Db.Olap.DAL.DataMapping;
using CardDisputes.Db.Olap.DAL.DataMapping.Predicates;
namespace CardDisputes.Db.Olap.DAL
{
public abstract class Repository<TEntity, TEntityMap> : DbConnectionProvider, IRepository<TEntity>, IEntityMapperAware
where TEntity : class
where TEntityMap : class
{
public IMapper EntityMapper { get; set; }
#region DictionaryRepository Methods
public async Task<int> Count()
{
var connection = await GetPreparedConnection();
return await connection.Count<TEntityMap>(DbTransactionContext.Transaction);
}
public virtual async Task<int> Insert(TEntity item)
{
var connection = await GetPreparedConnection();
return await connection.Create(EntityMapper.Map<TEntityMap>(item), transaction: DbTransactionContext.Transaction);
}
public virtual async Task Insert(IEnumerable<TEntity> items)
{
var connection = await GetPreparedConnection();
await connection.Create(EntityMapper.Map<IEnumerable<TEntityMap>>(items), DbTransactionContext.Transaction);
}
public virtual async Task Remove(TEntity entity)
{
var connection = await GetPreparedConnection();
await connection.Delete(EntityMapper.Map<TEntityMap>(entity), DbTransactionContext.Transaction);
}
public virtual async Task<TEntity> GetOrInsert(TEntity entity, Expression<Func<TEntity, bool>> selector, Action<TEntity,int> identitySetter)
{
var dbEntity = await GetSingleOrDefault(selector);
if (dbEntity != null)
{
return dbEntity;
}
var identity = await Insert(entity);
identitySetter(entity, identity);
return entity;
}
public virtual async Task<int> RemoveAll()
{
var conection = await GetPreparedConnection();
return await conection.DeleteAll<TEntityMap>(DbTransactionContext.Transaction);
}
public virtual async Task<TEntity> Get(int id)
{
var connection = await GetPreparedConnection();
var entity = await connection.Retrieve<TEntityMap>(id, transaction: DbTransactionContext.Transaction);
return EntityMapper.Map<TEntity>(entity);
}
public virtual async Task<IEnumerable<TEntity>> Get(Expression<Func<TEntity, bool>> predicate)
{
var connection = await GetPreparedConnection();
var sql = PredicateTranslator.Translate(predicate);
var entities = await connection.Retrieve<TEntityMap>(sql, transaction: DbTransactionContext.Transaction);
return EntityMapper.Map<IEnumerable<TEntity>>(entities);
}
public virtual async Task<TEntity> GetSingle(Expression<Func<TEntity, bool>> predicate)
{
var connection = await GetPreparedConnection();
var sql = PredicateTranslator.Translate(predicate);
var entities = await connection.RetrieveSingle<TEntityMap>(sql, transaction: DbTransactionContext.Transaction);
return EntityMapper.Map<TEntity>(entities);
}
public virtual async Task<TEntity> GetSingleOrDefault(Expression<Func<TEntity, bool>> predicate)
{
var connection = await GetPreparedConnection();
var sql = PredicateTranslator.Translate(predicate);
var entities = await connection.RetrieveSingleOrDefault<TEntityMap>(sql, transaction: DbTransactionContext.Transaction);
return EntityMapper.Map<TEntity>(entities);
}
public virtual async Task<TEntity> GetFirst(Expression<Func<TEntity, bool>> predicate)
{
var connection = await GetPreparedConnection();
var sql = PredicateTranslator.Translate(predicate);
var entities = await connection.RetrieveFirst<TEntityMap>(sql, transaction: DbTransactionContext.Transaction);
return EntityMapper.Map<TEntity>(entities);
}
public virtual async Task<IEnumerable<TEntity>> Get(string queryText, object param = null)
{
var connection = await GetPreparedConnection();
var entities = await connection.Retrieve<TEntityMap>(queryText, param: param, transaction: DbTransactionContext.Transaction);
return EntityMapper.Map<IEnumerable<TEntity>>(entities);
}
public virtual async Task<TEntity> GetSingleOrDefault(string queryText, object param = null)
{
var connection = await GetPreparedConnection();
var entity = await connection.RetrieveSingleOrDefault<TEntityMap>(queryText, param: param, transaction: DbTransactionContext.Transaction);
return EntityMapper.Map<TEntity>(entity);
}
public virtual async Task<IEnumerable<TEntity>> Get()
{
var connection = await GetPreparedConnection();
var entities = await connection.Retrieve<TEntityMap>(transaction: DbTransactionContext.Transaction);
return EntityMapper.Map<IEnumerable<TEntity>>(entities);
}
public async Task<IEnumerable<TEntity>> Get(int[] ids)
{
var connection = await GetPreparedConnection();
var tasks = ids.Select(id => connection.Retrieve<TEntityMap>(id, DbTransactionContext.Transaction)).ToArray();
await Task.WhenAll(tasks);
return EntityMapper.Map<IEnumerable<TEntity>>(tasks.Select(t => t.Result));
}
public virtual async Task Update(TEntity item)
{
var connection = await GetPreparedConnection();
await connection.Update(EntityMapper.Map<TEntityMap>(item), transaction: DbTransactionContext.Transaction);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment