Last active
January 28, 2022 05:38
-
-
Save mikependon/d61928bd1230a41c249b72d24a24ab21 to your computer and use it in GitHub Desktop.
RepoDb_SqlConnectionEntityRepository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Data.SqlClient; | |
using RepoDb; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace GenericRepository.Repositories | |
{ | |
public abstract class SqlConnectionEntityRepository<TEntity> : EntityRepository<TEntity, SqlConnection> | |
where TEntity : class | |
{ | |
public SqlConnectionEntityRepository(string connectionString) : | |
base(connectionString) | |
{ } | |
/* Properties */ | |
private int BulkNumber => 1000; | |
/* Bulk */ | |
public override async Task<int> DeleteAllAsync(IEnumerable<TEntity> entities) | |
{ | |
if (entities?.Count() >= BulkNumber) | |
{ | |
using (var connection = (SqlConnection)GetConnection()) | |
{ | |
return await connection.BulkDeleteAsync<TEntity>(entities); | |
} | |
} | |
return await base.DeleteAllAsync(entities); | |
} | |
public override async Task<int> MergeAllAsync(IEnumerable<TEntity> entities) | |
{ | |
if (entities?.Count() >= BulkNumber) | |
{ | |
using (var connection = (SqlConnection)GetConnection()) | |
{ | |
return await connection.BulkMergeAsync<TEntity>(entities); | |
} | |
} | |
return await base.MergeAllAsync(entities); | |
} | |
public override async Task<int> SaveAllAsync(IEnumerable<TEntity> entities) | |
{ | |
if (entities?.Count() >= BulkNumber) | |
{ | |
using (var connection = (SqlConnection)GetConnection()) | |
{ | |
return await connection.BulkInsertAsync<TEntity>(entities); | |
} | |
} | |
return await base.SaveAllAsync(entities); | |
} | |
public override async Task<int> UpdateAllAsync(IEnumerable<TEntity> entities) | |
{ | |
if (entities?.Count() >= BulkNumber) | |
{ | |
using (var connection = (SqlConnection)GetConnection()) | |
{ | |
return await connection.BulkUpdateAsync<TEntity>(entities); | |
} | |
} | |
return await base.UpdateAllAsync(entities); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment