Skip to content

Instantly share code, notes, and snippets.

@cchamberlain
Created August 26, 2015 04:46
Show Gist options
  • Select an option

  • Save cchamberlain/0963cfb865e389b335c3 to your computer and use it in GitHub Desktop.

Select an option

Save cchamberlain/0963cfb865e389b335c3 to your computer and use it in GitHub Desktop.
Abstract service base class that combines DapperService.cs (https://gist.github.com/cchamberlain/e5bb0595d0630685cc6a) and IApiService.cs (https://gist.github.com/cchamberlain/c92f1c88c4fdcdfc715f)
public abstract class DapperApiService<TModel, TKey> : DapperService, IApiService<TModel, TKey> {
protected DapperApiService(string connectionString, ICacheProvider cacheProvider = null) : base(connectionString, cacheProvider) {}
protected SqlApiService<TModel, TKey> Sql { get; } = new SqlApiService<TModel, TKey>();
public IEnumerable<TModel> Get(string whereClause, object param = null, int top = 500)
=> Query<TModel>(Sql.Get(whereClause, top));
public IEnumerable<TModel> Get(object param = null, int top = 500)
=> Query<TModel>(Sql.Get(param, top));
public TModel GetOne(TKey id)
=> One<TModel>(Sql.GetOne(id));
public TModel GetFirst<TColumn>(TColumn value)
=> First<TModel>(Sql.GetFirst(value));
public IEnumerable<TModel> GetTop<TColumn>(TColumn value, int top = 500)
=> Query<TModel>(Sql.GetTop(value, top));
public TModel Put(TModel entity) {
Execute(Sql.Put(entity));
return entity;
}
public TModel Post(TModel entity) {
Execute(Sql.Post(entity));
return entity;
}
public bool Delete(TKey id) {
throw new NotImplementedException();
}
public async Task<IList<TModel>> GetAsync(object param = null, int top = 500)
=> (await QueryAsync<TModel>(Sql.Get(param, top))).ToList();
public async Task<TModel> GetOneAsync(TKey id)
=> await OneAsync<TModel>(Sql.GetOne(id));
public async Task<TModel> GetFirstAsync<TColumn>(TColumn value)
=> await FirstAsync<TModel>(Sql.GetFirst(value));
public async Task<IList<TModel>> GetTopAsync<TColumn>(TColumn value, int top = 500)
=> (await QueryAsync<TModel>(Sql.GetTop(value, top))).ToList();
public async Task<TModel> PutAsync(TModel entity) {
await ExecuteAsync(Sql.Put(entity));
return entity;
}
public async Task<TModel> PostAsync(TModel entity) {
await ExecuteAsync(Sql.Post(entity));
return entity;
}
public Task<bool> DeleteAsync(TKey id) {
throw new NotImplementedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment