Extends the MySQL implementation of the RevStack Repository Pattern to allow for fast bulk operations
Install-Package RevStackCore.MySQL.BulkInsert
public interface IBulkRepository<TEntity, TKey> : IRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>
{
int BulkInsert(IEnumerable<TEntity> entities);
int BulkUpdate(IEnumerable<TEntity> entities);
int BulkDelete();
IDbConnection Db { get; }
}
MySQLBulkRepository<TEntity,Tkey> implements IBulkRepository<TEntity,TKey> for basic Crud operations, Find and Bulk Operations
For bulk operations, the AllowLoadLocalInfile=true must be included in the connection string.
server=localhost;uid=root;pwd=**************;database=MyDb;Allow User Variables=True;AllowLoadLocalInfile=trueInject an instance of MySQLDbContext with the my sql connection string passed in the constructor.
var dbContext = new MySQLDbContext(connectionString);using RevStackCore.Pattern.SQL; //IBulkRepository interface
using RevStackCore.MySQL.BulkInsert;
class Program
{
static void main(string[] args)
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();
var dataService = serviceProvider.GetService<IDataService>();
dataService.MyMethod();
}
private static void ConfigureServices(IServiceCollection services)
{
string connectionString = "";
services
.AddSingleton(x => new MySQLDbContext(connectionString))
.AddSingleton<IBulkRepository<MyTableEntity, int>, MySQLBulkRepository<MyTableEntity, int>>()
.AddSingleton<IDataService, DataService>();
services.AddLogging();
//enforce class name to table name for databse reads/writes
SQLExtensions.SetTableNameMapper();
}
}public interface IBulkService<TEntity, TKey> : IService<TEntity, TKey> where TEntity : class, IEntity<TKey>
{
int BulkInsert(IEnumerable<TEntity> entities);
int BulkUpdate(IEnumerable<TEntity> entities);
int BulkDelete();
Task<int> BulkInsertAsync(IEnumerable<TEntity> entities);
Task<int> BulkUpdateAsync(IEnumerable<TEntity> entities);
Task<int> BulkDeleteAsync();
IDbConnection Db { get; }
}BulkService<TEntity,Tkey> implements IBulkService<TEntity,TKey> for basic Async Crud operations, FindAsync and asynchronous bulk operations