Skip to content

Instantly share code, notes, and snippets.

@rqx110
Forked from hikalkan/HomeController.cs
Last active March 9, 2018 03:46
Show Gist options
  • Save rqx110/38df69360cc8f5f37dee75fa1f94e742 to your computer and use it in GitHub Desktop.
Save rqx110/38df69360cc8f5f37dee75fa1f94e742 to your computer and use it in GitHub Desktop.
扩展IRepository,支持批量插入
//Demo usage
public class HomeController : MyControllerBase
{
private readonly IRepository<MyEntity> _myEntityRepository;
public HomeController(IRepository<MyEntity> myEntityRepository)
{
_myEntityRepository = myEntityRepository;
}
public ActionResult Index()
{
var entities = new List<MyEntity>
{
new MyEntity(),
new MyEntity(),
new MyEntity()
};
_myEntityRepository.BulkInsert(entities); //using the new extension method
return View();
}
}
using System;
using System.Collections.Generic;
using System.Reflection;
using Abp.Domain.Entities;
using Abp.Domain.Repositories;
namespace MyProject
{
//Add this class to .Core project
public static class MyRepositoryExtensions
{
public static void BulkInsert<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository,
IEnumerable<TEntity> entities)
where TEntity : class, IEntity<TPrimaryKey>, new()
{
var type = Type.GetType("MyProject.MyRepositoryHelper, MyProject.EntityFramework");
var bulkInsertMethod = type.GetMethod("BulkInsert", BindingFlags.Static | BindingFlags.Public);
var genericMethod = bulkInsertMethod.MakeGenericMethod(typeof(TEntity), typeof(TPrimaryKey));
genericMethod.Invoke(null, new object[] { repository, entities });
}
public static void BulkDelete<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository,
Expression<Func<TEntity, bool>> predicate)
where TEntity : class, IEntity<TPrimaryKey>, new()
{
var type = Type.GetType("SimpleTaskSystem.MyRepositoryHelper, SimpleTaskSystem.EntityFramework");
var bulkDeleteMethod = type.GetMethod("BulkDelete", BindingFlags.Static | BindingFlags.Public);
var genericMethod = bulkDeleteMethod.MakeGenericMethod(typeof(TEntity), typeof(TPrimaryKey));
genericMethod.Invoke(null, new object[] { repository, predicate });
}
public static void BulkUpdate<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository,
Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TEntity>> updateExpression)
where TEntity : class, IEntity<TPrimaryKey>, new()
{
var type = Type.GetType("SimpleTaskSystem.MyRepositoryHelper, SimpleTaskSystem.EntityFramework");
var bulkUpdateMethod = type.GetMethod("BulkUpdate", BindingFlags.Static | BindingFlags.Public);
var genericMethod = bulkUpdateMethod.MakeGenericMethod(typeof(TEntity), typeof(TPrimaryKey));
genericMethod.Invoke(null, new object[] {repository, predicate, updateExpression});
}
}
}
using System.Collections.Generic;
using Abp.Domain.Entities;
using Abp.Domain.Repositories;
using Abp.EntityFramework.Repositories;
using EntityFramework.Extensions;
namespace MyProject
{
//Add this class to .EntityFramework project
public static class MyRepositoryHelper
{
public static void BulkInsert<TEntity, TPrimaryKey>(IRepository<TEntity, TPrimaryKey> repository, IEnumerable<TEntity> entities)
where TEntity : class, IEntity<TPrimaryKey>, new()
{
var dbContext = repository.GetDbContext();
dbContext.Set<TEntity>().AddRange(entities);
}
public static void BulkDelete<TEntity, TPrimaryKey>(IRepository<TEntity, TPrimaryKey> repository, Expression<Func<TEntity, bool>> predicate)
where TEntity : class, IEntity<TPrimaryKey>, new()
{
var dbContext = repository.GetDbContext();
dbContext.Set<TEntity>().Where(predicate).Delete();
}
public static void BulkUpdate<TEntity, TPrimaryKey>(IRepository<TEntity, TPrimaryKey> repository, Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TEntity>> updateExpression)
where TEntity : class, IEntity<TPrimaryKey>, new()
{
var dbContext = repository.GetDbContext();
dbContext.Set<TEntity>().Where(predicate).Update(updateExpression);
}
}
}
@rqx110
Copy link
Author

rqx110 commented Nov 13, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment