Skip to content

A few dapper extension that can help in everyday use

License

Notifications You must be signed in to change notification settings

RobbSadler/dapper-extensions

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Forked from original at https://github.com/bolicd/dapper-extensions

Generic Repository

Generic repository based on dapper exposes most common repository methods as well as providing some additional functionality.

Here is the complete interface that is exposed:

public interface IGenericRepository<T> where T: class
{
    Task<IEnumerable<T>> GetAllAsync(CancellationToken cancellationToken = default);
    Task<T> GetAsync(object id,CancellationToken cancellationToken = default);
    Task InsertAsync(T t,CancellationToken cancellationToken = default);
    void InsertBulk(IEnumerable<T> items); 
    Task UpdateAsync(T t, CancellationToken cancellationToken = default);
    Task DeleteAsync(object id, CancellationToken cancellationToken = default);
    Task InsertRangeAsync(IEnumerable<T> t, CancellationToken cancellationToken = default);   
}

Each async method also supports cancellation token except for InsertBulk which is not async and is using SqlBulkCopy for efficient insert large number of records without a transaction.

Repository generates queries based on provided generic type T and usage is pretty straightforward. Examples can be found in Tests project.

Please note: For inserting large number of records use InsertBulk for inserting large number of records with the transaction use InsertRangeAsync.

Dapper-extensions

A few dapper extension that can help in everyday use

How-to-use

Dapper can be used the same way. If you want to pass token to the method with the same name as dapperWithToken(). Example:

public async Task<IEnumerable<T>> GetAllAsync(CancellationToken cancellationToken)
{
  using (var connection = CreateConnection())
  {
      return await connection.QueryAsyncWithToken<T>($"SELECT * FROM {_tableName}", cancellationToken: cancellationToken);
  }
}

About

A few dapper extension that can help in everyday use

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 98.0%
  • TSQL 2.0%