mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
FreeSql.DbContext 内部仓储融合
This commit is contained in:
parent
ff758f338c
commit
496750da94
@ -30,19 +30,51 @@ namespace dbcontext_01.Controllers
|
|||||||
long id = 0;
|
long id = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
var repos2Song = _orm.GetRepository<Song, int>();
|
||||||
|
repos2Song.Where(a => a.Id > 10).ToList();
|
||||||
|
//查询结果,进入 states
|
||||||
|
|
||||||
|
var song = new Song { };
|
||||||
|
repos2Song.Insert(song);
|
||||||
|
id = song.Id;
|
||||||
|
|
||||||
|
var adds = Enumerable.Range(0, 100)
|
||||||
|
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
|
||||||
|
.ToList();
|
||||||
|
//创建一堆无主键值
|
||||||
|
|
||||||
|
repos2Song.Insert(adds);
|
||||||
|
|
||||||
|
for (var a = 0; a < 10; a++)
|
||||||
|
adds[a].Title = "dkdkdkdk" + a;
|
||||||
|
|
||||||
|
repos2Song.Update(adds);
|
||||||
|
//批量修改
|
||||||
|
|
||||||
|
repos2Song.Delete(adds.Skip(10).Take(20).ToList());
|
||||||
|
//批量删除,10-20 元素的主键值会被清除
|
||||||
|
|
||||||
|
adds.Last().Url = "skldfjlksdjglkjjcccc";
|
||||||
|
repos2Song.Update(adds.Last());
|
||||||
|
|
||||||
|
adds.First().Url = "skldfjlksdjglkjjcccc";
|
||||||
|
repos2Song.Update(adds.First());
|
||||||
|
|
||||||
|
|
||||||
using (var ctx = new SongContext()) {
|
using (var ctx = new SongContext()) {
|
||||||
|
|
||||||
ctx.Songs.Select.Where(a => a.Id > 10).ToList();
|
ctx.Songs.Select.Where(a => a.Id > 10).ToList();
|
||||||
//查询结果,进入 states
|
//查询结果,进入 states
|
||||||
|
|
||||||
var song = new Song { };
|
song = new Song { };
|
||||||
//可插入的 song
|
//可插入的 song
|
||||||
|
|
||||||
ctx.Songs.Add(song);
|
ctx.Songs.Add(song);
|
||||||
id = song.Id;
|
id = song.Id;
|
||||||
//因有自增类型,立即开启事务执行SQL,返回自增值
|
//因有自增类型,立即开启事务执行SQL,返回自增值
|
||||||
|
|
||||||
var adds = Enumerable.Range(0, 100)
|
adds = Enumerable.Range(0, 100)
|
||||||
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
|
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
|
||||||
.ToList();
|
.ToList();
|
||||||
//创建一堆无主键值
|
//创建一堆无主键值
|
||||||
@ -64,6 +96,9 @@ namespace dbcontext_01.Controllers
|
|||||||
adds.Last().Url = "skldfjlksdjglkjjcccc";
|
adds.Last().Url = "skldfjlksdjglkjjcccc";
|
||||||
ctx.Songs.Update(adds.Last());
|
ctx.Songs.Update(adds.Last());
|
||||||
|
|
||||||
|
adds.First().Url = "skldfjlksdjglkjjcccc";
|
||||||
|
ctx.Songs.Update(adds.First());
|
||||||
|
|
||||||
//单条修改 urls 的值,进入队列
|
//单条修改 urls 的值,进入队列
|
||||||
|
|
||||||
//throw new Exception("回滚");
|
//throw new Exception("回滚");
|
||||||
@ -75,6 +110,43 @@ namespace dbcontext_01.Controllers
|
|||||||
//打包【执行队列】,提交事务
|
//打包【执行队列】,提交事务
|
||||||
}
|
}
|
||||||
|
|
||||||
|
using (var uow = _orm.CreateUnitOfWork()) {
|
||||||
|
|
||||||
|
var reposSong = uow.GetRepository<Song, int>();
|
||||||
|
reposSong.Where(a => a.Id > 10).ToList();
|
||||||
|
//查询结果,进入 states
|
||||||
|
|
||||||
|
song = new Song { };
|
||||||
|
reposSong.Insert(song);
|
||||||
|
id = song.Id;
|
||||||
|
|
||||||
|
adds = Enumerable.Range(0, 100)
|
||||||
|
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
|
||||||
|
.ToList();
|
||||||
|
//创建一堆无主键值
|
||||||
|
|
||||||
|
reposSong.Insert(adds);
|
||||||
|
|
||||||
|
for (var a = 0; a < 10; a++)
|
||||||
|
adds[a].Title = "dkdkdkdk" + a;
|
||||||
|
|
||||||
|
reposSong.Update(adds);
|
||||||
|
//批量修改
|
||||||
|
|
||||||
|
reposSong.Delete(adds.Skip(10).Take(20).ToList());
|
||||||
|
//批量删除,10-20 元素的主键值会被清除
|
||||||
|
|
||||||
|
adds.Last().Url = "skldfjlksdjglkjjcccc";
|
||||||
|
reposSong.Update(adds.Last());
|
||||||
|
|
||||||
|
adds.First().Url = "skldfjlksdjglkjjcccc";
|
||||||
|
reposSong.Update(adds.First());
|
||||||
|
|
||||||
|
uow.Commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//using (var ctx = new SongContext()) {
|
//using (var ctx = new SongContext()) {
|
||||||
|
|
||||||
// var song = new Song { };
|
// var song = new Song { };
|
||||||
|
@ -13,8 +13,8 @@ namespace FreeSql {
|
|||||||
internal IFreeSql _orm;
|
internal IFreeSql _orm;
|
||||||
internal IFreeSql _fsql => _orm ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
|
internal IFreeSql _fsql => _orm ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
|
||||||
|
|
||||||
Object<DbConnection> _conn;
|
UnitOfWork _uowPriv;
|
||||||
DbTransaction _tran;
|
internal UnitOfWork _uow => _uowPriv ?? (_uowPriv = new UnitOfWork(_fsql));
|
||||||
|
|
||||||
static ConcurrentDictionary<Type, PropertyInfo[]> _dicGetDbSetProps = new ConcurrentDictionary<Type, PropertyInfo[]>();
|
static ConcurrentDictionary<Type, PropertyInfo[]> _dicGetDbSetProps = new ConcurrentDictionary<Type, PropertyInfo[]>();
|
||||||
protected DbContext() {
|
protected DbContext() {
|
||||||
@ -34,8 +34,6 @@ namespace FreeSql {
|
|||||||
prop.SetValue(this, set);
|
prop.SetValue(this, set);
|
||||||
AllSets.Add(prop.Name, set);
|
AllSets.Add(prop.Name, set);
|
||||||
}
|
}
|
||||||
|
|
||||||
//_fsql.Aop.ToList += AopToList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void OnConfiguring(DbContextOptionsBuilder builder) {
|
protected virtual void OnConfiguring(DbContextOptionsBuilder builder) {
|
||||||
@ -68,49 +66,18 @@ namespace FreeSql {
|
|||||||
_actions.Enqueue(new ExecCommandInfo { actionType = actionType, dbSet = dbSet, stateType = stateType, state = state });
|
_actions.Enqueue(new ExecCommandInfo { actionType = actionType, dbSet = dbSet, stateType = stateType, state = state });
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReturnObject() {
|
~DbContext() {
|
||||||
_fsql.Ado.MasterPool.Return(_conn);
|
this.Dispose();
|
||||||
_tran = null;
|
|
||||||
_conn = null;
|
|
||||||
}
|
|
||||||
internal DbTransaction GetOrBeginTransaction(bool isCreate = true) {
|
|
||||||
|
|
||||||
if (_tran != null) return _tran;
|
|
||||||
if (isCreate == false) return null;
|
|
||||||
if (_conn != null) _fsql.Ado.MasterPool.Return(_conn);
|
|
||||||
|
|
||||||
_conn = _fsql.Ado.MasterPool.Get();
|
|
||||||
try {
|
|
||||||
_tran = _conn.Value.BeginTransaction();
|
|
||||||
} catch {
|
|
||||||
ReturnObject();
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
return _tran;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Commit() {
|
|
||||||
if (_tran != null) {
|
|
||||||
try {
|
|
||||||
_tran.Commit();
|
|
||||||
} finally {
|
|
||||||
ReturnObject();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void Rollback() {
|
|
||||||
_actions.Clear();
|
|
||||||
if (_tran != null) {
|
|
||||||
try {
|
|
||||||
_tran.Rollback();
|
|
||||||
} finally {
|
|
||||||
ReturnObject();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
bool _isdisposed = false;
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
//_fsql.Aop.ToList -= AopToList;
|
if (_isdisposed) return;
|
||||||
this.Rollback();
|
try {
|
||||||
|
_uow.Rollback();
|
||||||
|
} finally {
|
||||||
|
_isdisposed = true;
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1 +1,115 @@
|
|||||||
|
using SafeObjectPool;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
partial class DbContext {
|
||||||
|
|
||||||
|
async public Task<long> SaveChangesAsync() {
|
||||||
|
await ExecCommandAsync();
|
||||||
|
_uow.Commit();
|
||||||
|
return _affrows;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Dictionary<Type, Dictionary<string, Func<object, object[], Task<int>>>> _dicExecCommandDbContextBetchAsync = new Dictionary<Type, Dictionary<string, Func<object, object[], Task<int>>>>();
|
||||||
|
async internal Task ExecCommandAsync() {
|
||||||
|
ExecCommandInfo oldinfo = null;
|
||||||
|
var states = new List<object>();
|
||||||
|
|
||||||
|
Func<string, Task<int>> dbContextBetch = methodName => {
|
||||||
|
if (_dicExecCommandDbContextBetchAsync.TryGetValue(oldinfo.stateType, out var trydic) == false)
|
||||||
|
trydic = new Dictionary<string, Func<object, object[], Task<int>>>();
|
||||||
|
if (trydic.TryGetValue(methodName, out var tryfunc) == false) {
|
||||||
|
var arrType = oldinfo.stateType.MakeArrayType();
|
||||||
|
var dbsetType = oldinfo.dbSet.GetType().BaseType;
|
||||||
|
var dbsetTypeMethod = dbsetType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
|
||||||
|
|
||||||
|
var returnTarget = Expression.Label(typeof(Task<int>));
|
||||||
|
var parm1DbSet = Expression.Parameter(typeof(object));
|
||||||
|
var parm2Vals = Expression.Parameter(typeof(object[]));
|
||||||
|
var var1Vals = Expression.Variable(arrType);
|
||||||
|
tryfunc = Expression.Lambda<Func<object, object[], Task<int>>>(Expression.Block(
|
||||||
|
new[] { var1Vals },
|
||||||
|
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
|
||||||
|
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeMethod, var1Vals)),
|
||||||
|
Expression.Label(returnTarget, Expression.Default(typeof(int)))
|
||||||
|
), new[] { parm1DbSet, parm2Vals }).Compile();
|
||||||
|
trydic.Add(methodName, tryfunc);
|
||||||
|
}
|
||||||
|
return tryfunc(oldinfo.dbSet, states.ToArray());
|
||||||
|
};
|
||||||
|
Func<Task> funcDelete = async () => {
|
||||||
|
_affrows += await dbContextBetch("DbContextBetchRemoveAsync");
|
||||||
|
states.Clear();
|
||||||
|
};
|
||||||
|
Func<Task> funcInsert = async () => {
|
||||||
|
_affrows += await dbContextBetch("DbContextBetchAddAsync");
|
||||||
|
states.Clear();
|
||||||
|
};
|
||||||
|
Func<bool, Task> funcUpdate = async (isLiveUpdate) => {
|
||||||
|
var affrows = 0;
|
||||||
|
if (isLiveUpdate) affrows = await dbContextBetch("DbContextBetchUpdateNowAsync");
|
||||||
|
else affrows = await dbContextBetch("DbContextBetchUpdateAsync");
|
||||||
|
if (affrows == -999) { //最后一个元素已被删除
|
||||||
|
states.RemoveAt(states.Count - 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (affrows == -998 || affrows == -997) { //没有执行更新
|
||||||
|
var laststate = states[states.Count - 1];
|
||||||
|
states.Clear();
|
||||||
|
if (affrows == -997) states.Add(laststate); //保留最后一个
|
||||||
|
}
|
||||||
|
if (affrows > 0) {
|
||||||
|
_affrows += affrows;
|
||||||
|
var islastNotUpdated = states.Count != affrows;
|
||||||
|
var laststate = states[states.Count - 1];
|
||||||
|
states.Clear();
|
||||||
|
if (islastNotUpdated) states.Add(laststate); //保留最后一个
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
while (_actions.Any() || states.Any()) {
|
||||||
|
var info = _actions.Any() ? _actions.Dequeue() : null;
|
||||||
|
if (oldinfo == null) oldinfo = info;
|
||||||
|
var isLiveUpdate = false;
|
||||||
|
|
||||||
|
if (_actions.Any() == false && states.Any() ||
|
||||||
|
info != null && oldinfo.actionType != info.actionType ||
|
||||||
|
info != null && oldinfo.stateType != info.stateType) {
|
||||||
|
|
||||||
|
if (info != null && oldinfo.actionType == info.actionType && oldinfo.stateType == info.stateType) {
|
||||||
|
//最后一个,合起来发送
|
||||||
|
states.Add(info.state);
|
||||||
|
info = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (oldinfo.actionType) {
|
||||||
|
case ExecCommandInfoType.Insert:
|
||||||
|
await funcInsert();
|
||||||
|
break;
|
||||||
|
case ExecCommandInfoType.Delete:
|
||||||
|
await funcDelete();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
isLiveUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isLiveUpdate || oldinfo.actionType == ExecCommandInfoType.Update) {
|
||||||
|
if (states.Any())
|
||||||
|
await funcUpdate(isLiveUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (info != null) {
|
||||||
|
states.Add(info.state);
|
||||||
|
oldinfo = info;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -12,7 +12,7 @@ namespace FreeSql {
|
|||||||
|
|
||||||
public long SaveChanges() {
|
public long SaveChanges() {
|
||||||
ExecCommand();
|
ExecCommand();
|
||||||
Commit();
|
_uow.Commit();
|
||||||
return _affrows;
|
return _affrows;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,37 +10,39 @@ namespace FreeSql {
|
|||||||
internal class BaseDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
|
internal class BaseDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
|
||||||
|
|
||||||
public BaseDbSet(DbContext ctx) {
|
public BaseDbSet(DbContext ctx) {
|
||||||
|
if (ctx != null) {
|
||||||
_ctx = ctx;
|
_ctx = ctx;
|
||||||
|
_uow = ctx._uow;
|
||||||
_fsql = ctx._fsql;
|
_fsql = ctx._fsql;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public abstract partial class DbSet<TEntity> where TEntity : class {
|
public abstract partial class DbSet<TEntity> where TEntity : class {
|
||||||
|
|
||||||
internal DbContext _ctx;
|
internal DbContext _ctx;
|
||||||
|
internal UnitOfWork _uow;
|
||||||
internal IFreeSql _fsql;
|
internal IFreeSql _fsql;
|
||||||
|
bool IsNoneDbContext => _ctx == null;
|
||||||
|
|
||||||
internal ISelect<TEntity> OrmSelect(object dywhere) {
|
protected virtual ISelect<TEntity> OrmSelect(object dywhere) {
|
||||||
ExecuteCommand(); //查询前先提交,否则会出脏读
|
DbContextExecCommand(); //查询前先提交,否则会出脏读
|
||||||
return _fsql.Select<TEntity>(dywhere).WithTransaction(_ctx?.GetOrBeginTransaction(false)).TrackToList(TrackToList);
|
return _fsql.Select<TEntity>(dywhere).WithTransaction(_uow?.GetOrBeginTransaction(false)).TrackToList(TrackToList);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal virtual IInsert<TEntity> OrmInsert() => _fsql.Insert<TEntity>().WithTransaction(_ctx?.GetOrBeginTransaction());
|
protected virtual IInsert<TEntity> OrmInsert() => _fsql.Insert<TEntity>().WithTransaction(_uow?.GetOrBeginTransaction());
|
||||||
internal virtual IInsert<TEntity> OrmInsert(TEntity data) => _fsql.Insert<TEntity>(data).WithTransaction(_ctx?.GetOrBeginTransaction());
|
protected virtual IInsert<TEntity> OrmInsert(TEntity data) => _fsql.Insert<TEntity>(data).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||||
internal virtual IInsert<TEntity> OrmInsert(TEntity[] data) => _fsql.Insert<TEntity>(data).WithTransaction(_ctx?.GetOrBeginTransaction());
|
protected virtual IInsert<TEntity> OrmInsert(IEnumerable<TEntity> data) => _fsql.Insert<TEntity>(data).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||||
internal virtual IInsert<TEntity> OrmInsert(IEnumerable<TEntity> data) => _fsql.Insert<TEntity>(data).WithTransaction(_ctx?.GetOrBeginTransaction());
|
|
||||||
|
|
||||||
internal virtual IUpdate<TEntity> OrmUpdate(object dywhere) => _fsql.Update<TEntity>(dywhere).WithTransaction(_ctx?.GetOrBeginTransaction());
|
protected virtual IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys) => _fsql.Update<TEntity>().SetSource(entitys).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||||
internal virtual IDelete<TEntity> OrmDelete(object dywhere) => _fsql.Delete<TEntity>(dywhere).WithTransaction(_ctx?.GetOrBeginTransaction());
|
protected virtual IDelete<TEntity> OrmDelete(object dywhere) => _fsql.Delete<TEntity>(dywhere).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||||
|
|
||||||
internal void EnqueueAction(DbContext.ExecCommandInfoType actionType, object dbSet, Type stateType, object state) {
|
internal void EnqueueToDbContext(DbContext.ExecCommandInfoType actionType, EntityState state) {
|
||||||
_ctx?.EnqueueAction(actionType, dbSet, stateType, state);
|
if (IsNoneDbContext == false)
|
||||||
}
|
_ctx.EnqueueAction(actionType, this, typeof(EntityState), state);
|
||||||
internal void ExecuteCommand() {
|
|
||||||
_ctx?.ExecCommand();
|
|
||||||
}
|
}
|
||||||
internal void IncrAffrows(long affrows) {
|
internal void IncrAffrows(long affrows) {
|
||||||
if (_ctx != null)
|
if (IsNoneDbContext == false)
|
||||||
_ctx._affrows += affrows;
|
_ctx._affrows += affrows;
|
||||||
}
|
}
|
||||||
internal void TrackToList(object list) {
|
internal void TrackToList(object list) {
|
||||||
@ -63,12 +65,14 @@ namespace FreeSql {
|
|||||||
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).Where(exp);
|
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).Where(exp);
|
||||||
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).WhereIf(condition, exp);
|
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).WhereIf(condition, exp);
|
||||||
|
|
||||||
Dictionary<string, EntityState> _states = new Dictionary<string, EntityState>();
|
protected Dictionary<string, EntityState> _states = new Dictionary<string, EntityState>();
|
||||||
|
internal Dictionary<string, EntityState> _statesInternal => _states;
|
||||||
TableInfo _tablePriv;
|
TableInfo _tablePriv;
|
||||||
TableInfo _table => _tablePriv ?? (_tablePriv = _fsql.CodeFirst.GetTableByEntity(_entityType));
|
protected TableInfo _table => _tablePriv ?? (_tablePriv = _fsql.CodeFirst.GetTableByEntity(_entityType));
|
||||||
ColumnInfo[] _tableIdentitysPriv;
|
ColumnInfo[] _tableIdentitysPriv;
|
||||||
ColumnInfo[] _tableIdentitys => _tableIdentitysPriv ?? (_tableIdentitysPriv = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray());
|
protected ColumnInfo[] _tableIdentitys => _tableIdentitysPriv ?? (_tableIdentitysPriv = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray());
|
||||||
Type _entityType = typeof(TEntity);
|
protected Type _entityType = typeof(TEntity);
|
||||||
|
internal Type _entityTypeInternal => _entityType;
|
||||||
|
|
||||||
public class EntityState {
|
public class EntityState {
|
||||||
public EntityState(TEntity value, string key) {
|
public EntityState(TEntity value, string key) {
|
||||||
@ -96,14 +100,7 @@ namespace FreeSql {
|
|||||||
if (string.IsNullOrEmpty(key)) return false;
|
if (string.IsNullOrEmpty(key)) return false;
|
||||||
return _states.ContainsKey(key);
|
return _states.ContainsKey(key);
|
||||||
}
|
}
|
||||||
bool CanAdd(TEntity[] data, bool isThrow) {
|
|
||||||
if (data == null) {
|
|
||||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
foreach (var s in data) if (CanAdd(s, isThrow) == false) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
bool CanAdd(IEnumerable<TEntity> data, bool isThrow) {
|
bool CanAdd(IEnumerable<TEntity> data, bool isThrow) {
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
if (isThrow) throw new ArgumentNullException(nameof(data));
|
||||||
@ -146,14 +143,6 @@ namespace FreeSql {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CanUpdate(TEntity[] data, bool isThrow) {
|
|
||||||
if (data == null) {
|
|
||||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
foreach (var s in data) if (CanUpdate(s, isThrow) == false) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
bool CanUpdate(IEnumerable<TEntity> data, bool isThrow) {
|
bool CanUpdate(IEnumerable<TEntity> data, bool isThrow) {
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
if (isThrow) throw new ArgumentNullException(nameof(data));
|
||||||
@ -183,14 +172,6 @@ namespace FreeSql {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CanRemove(TEntity[] data, bool isThrow) {
|
|
||||||
if (data == null) {
|
|
||||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
foreach (var s in data) if (CanRemove(s, isThrow) == false) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
bool CanRemove(IEnumerable<TEntity> data, bool isThrow) {
|
bool CanRemove(IEnumerable<TEntity> data, bool isThrow) {
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
if (isThrow) throw new ArgumentNullException(nameof(data));
|
||||||
|
@ -1 +1,239 @@
|
|||||||
|
using FreeSql.Extensions.EntityUtil;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
partial class DbSet<TEntity> {
|
||||||
|
|
||||||
|
Task DbContextExecCommandAsync() {
|
||||||
|
if (IsNoneDbContext == false) {
|
||||||
|
_dicUpdateTimes.Clear();
|
||||||
|
return _ctx.ExecCommandAsync();
|
||||||
|
}
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<int> DbContextBetchAddAsync(EntityState[] adds) {
|
||||||
|
if (adds.Any() == false) return 0;
|
||||||
|
var affrows = await this.OrmInsert(adds.Select(a => a.Value)).ExecuteAffrowsAsync();
|
||||||
|
return affrows;
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Add
|
||||||
|
async Task AddPrivAsync(TEntity data, bool isCheck) {
|
||||||
|
if (isCheck && CanAdd(data, true) == false) return;
|
||||||
|
if (_tableIdentitys.Length > 0) {
|
||||||
|
//有自增,马上执行
|
||||||
|
switch (_fsql.Ado.DataType) {
|
||||||
|
case DataType.SqlServer:
|
||||||
|
case DataType.PostgreSQL:
|
||||||
|
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||||
|
await DbContextExecCommandAsync();
|
||||||
|
var idtval = await this.OrmInsert(data).ExecuteIdentityAsync();
|
||||||
|
IncrAffrows(1);
|
||||||
|
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
||||||
|
var state = CreateEntityState(data);
|
||||||
|
_states.Add(state.Key, state);
|
||||||
|
} else {
|
||||||
|
await DbContextExecCommandAsync();
|
||||||
|
var newval = (await this.OrmInsert(data).ExecuteInsertedAsync()).First();
|
||||||
|
IncrAffrows(1);
|
||||||
|
_fsql.MapEntityValue(newval, data);
|
||||||
|
var state = CreateEntityState(newval);
|
||||||
|
_states.Add(state.Key, state);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
case DataType.MySql:
|
||||||
|
case DataType.Oracle:
|
||||||
|
case DataType.Sqlite:
|
||||||
|
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||||
|
await DbContextExecCommandAsync();
|
||||||
|
var idtval = await this.OrmInsert(data).ExecuteIdentityAsync();
|
||||||
|
IncrAffrows(1);
|
||||||
|
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
||||||
|
var state = CreateEntityState(data);
|
||||||
|
_states.Add(state.Key, state);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (IsNoneDbContext)
|
||||||
|
IncrAffrows(await OrmInsert(data).ExecuteAffrowsAsync());
|
||||||
|
else
|
||||||
|
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public Task AddAsync(TEntity data) => AddPrivAsync(data, true);
|
||||||
|
async public Task AddRangeAsync(IEnumerable<TEntity> data) {
|
||||||
|
if (CanAdd(data, true) == false) return;
|
||||||
|
if (data.ElementAtOrDefault(1) == default(TEntity)) {
|
||||||
|
await AddAsync(data.First());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (_tableIdentitys.Length > 0) {
|
||||||
|
//有自增,马上执行
|
||||||
|
switch (_fsql.Ado.DataType) {
|
||||||
|
case DataType.SqlServer:
|
||||||
|
case DataType.PostgreSQL:
|
||||||
|
await DbContextExecCommandAsync();
|
||||||
|
var rets = await this.OrmInsert(data).ExecuteInsertedAsync();
|
||||||
|
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_fsql.Ado.DataType} 的返回数据,与添加的数目不匹配");
|
||||||
|
var idx = 0;
|
||||||
|
foreach (var s in data)
|
||||||
|
_fsql.MapEntityValue(rets[idx++], s);
|
||||||
|
IncrAffrows(rets.Count);
|
||||||
|
TrackToList(rets);
|
||||||
|
return;
|
||||||
|
case DataType.MySql:
|
||||||
|
case DataType.Oracle:
|
||||||
|
case DataType.Sqlite:
|
||||||
|
foreach (var s in data)
|
||||||
|
await AddPrivAsync(s, false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (IsNoneDbContext)
|
||||||
|
IncrAffrows(await OrmInsert(data).ExecuteAffrowsAsync());
|
||||||
|
else
|
||||||
|
//进入队列,等待 SaveChanges 时执行
|
||||||
|
foreach (var s in data)
|
||||||
|
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(s));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region UpdateAsync
|
||||||
|
Task<int> DbContextBetchUpdateAsync(EntityState[] ups) => DbContextBetchUpdatePrivAsync(ups, false);
|
||||||
|
Task<int> DbContextBetchUpdateNowAsync(EntityState[] ups) => DbContextBetchUpdatePrivAsync(ups, true);
|
||||||
|
async Task<int> DbContextBetchUpdatePrivAsync(EntityState[] ups, bool isLiveUpdate) {
|
||||||
|
if (ups.Any() == false) return 0;
|
||||||
|
var uplst1 = ups[ups.Length - 1];
|
||||||
|
var uplst2 = ups.Length > 1 ? ups[ups.Length - 2] : null;
|
||||||
|
|
||||||
|
if (_states.TryGetValue(uplst1.Key, out var lstval1) == false) return -999;
|
||||||
|
var lstval2 = default(EntityState);
|
||||||
|
if (uplst2 != null && _states.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception($"特别错误:更新失败,数据未被跟踪:{_fsql.GetEntityString(uplst2.Value)}");
|
||||||
|
|
||||||
|
var cuig1 = _fsql.CompareEntityValueReturnColumns(uplst1.Value, lstval1.Value, true);
|
||||||
|
var cuig2 = uplst2 != null ? _fsql.CompareEntityValueReturnColumns(uplst2.Value, lstval2.Value, true) : null;
|
||||||
|
|
||||||
|
List<EntityState> data = null;
|
||||||
|
string[] cuig = null;
|
||||||
|
if (uplst2 != null && string.Compare(string.Join(",", cuig1), string.Join(",", cuig2)) != 0) {
|
||||||
|
//最后一个不保存
|
||||||
|
data = ups.ToList();
|
||||||
|
data.RemoveAt(ups.Length - 1);
|
||||||
|
cuig = cuig2;
|
||||||
|
} else if (isLiveUpdate) {
|
||||||
|
//立即保存
|
||||||
|
data = ups.ToList();
|
||||||
|
cuig = cuig1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data?.Count > 0) {
|
||||||
|
|
||||||
|
if (cuig.Length == _table.Columns.Count)
|
||||||
|
return ups.Length == data.Count ? -998 : -997;
|
||||||
|
|
||||||
|
var updateSource = data.Select(a => a.Value).ToArray();
|
||||||
|
var update = this.OrmUpdate(null).SetSource(updateSource).IgnoreColumns(cuig);
|
||||||
|
|
||||||
|
var affrows = await update.ExecuteAffrowsAsync();
|
||||||
|
|
||||||
|
foreach (var newval in data) {
|
||||||
|
if (_states.TryGetValue(newval.Key, out var tryold))
|
||||||
|
_fsql.MapEntityValue(newval.Value, tryold.Value);
|
||||||
|
if (newval.OldValue != null)
|
||||||
|
_fsql.MapEntityValue(newval.Value, newval.OldValue);
|
||||||
|
}
|
||||||
|
return affrows;
|
||||||
|
}
|
||||||
|
|
||||||
|
//等待下次对比再保存
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal Task<int> UpdateAffrowsAsync(TEntity data) => UpdateRangeAffrowsAsync(new[] { data });
|
||||||
|
async internal Task<int> UpdateRangeAffrowsAsync(IEnumerable<TEntity> data) {
|
||||||
|
if (CanUpdate(data, true) == false) return 0;
|
||||||
|
if (IsNoneDbContext) {
|
||||||
|
var dataarray = data.ToArray();
|
||||||
|
var ups = new List<EntityState>();
|
||||||
|
var totalAffrows = 0;
|
||||||
|
for (var a = 0; a < dataarray.Length + 1; a++) {
|
||||||
|
var item = a < dataarray.Length ? dataarray[a] : null;
|
||||||
|
if (item != null) {
|
||||||
|
var state = CreateEntityState(item);
|
||||||
|
state.Value = item;
|
||||||
|
ups.Add(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
var affrows = await DbContextBetchUpdatePrivAsync(ups.ToArray(), item == null);
|
||||||
|
if (affrows == -999) { //最后一个元素已被删除
|
||||||
|
ups.RemoveAt(ups.Count - 1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (affrows == -998 || affrows == -997) { //没有执行更新
|
||||||
|
var laststate = ups[ups.Count - 1];
|
||||||
|
ups.Clear();
|
||||||
|
if (affrows == -997) ups.Add(laststate); //保留最后一个
|
||||||
|
}
|
||||||
|
if (affrows > 0) {
|
||||||
|
totalAffrows += affrows;
|
||||||
|
var islastNotUpdated = ups.Count != affrows;
|
||||||
|
var laststate = ups[ups.Count - 1];
|
||||||
|
ups.Clear();
|
||||||
|
if (islastNotUpdated) ups.Add(laststate); //保留最后一个
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IncrAffrows(totalAffrows);
|
||||||
|
return totalAffrows;
|
||||||
|
}
|
||||||
|
foreach (var item in data) {
|
||||||
|
if (_dicUpdateTimes.ContainsKey(item))
|
||||||
|
await DbContextExecCommandAsync();
|
||||||
|
_dicUpdateTimes.Add(item, 1);
|
||||||
|
|
||||||
|
var state = CreateEntityState(item);
|
||||||
|
state.OldValue = item;
|
||||||
|
EnqueueToDbContext(DbContext.ExecCommandInfoType.Update, state);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
internal Task UpdateAsync(TEntity data) => UpdateAffrowsAsync(data);
|
||||||
|
internal Task UpdateRangeAsync(IEnumerable<TEntity> data) => UpdateRangeAffrowsAsync(data);
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region RemoveAsync
|
||||||
|
async Task<int> DbContextBetchRemoveAsync(EntityState[] dels) {
|
||||||
|
if (dels.Any() == false) return 0;
|
||||||
|
var affrows = await this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrowsAsync();
|
||||||
|
return Math.Max(dels.Length, affrows);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal Task<int> RemoveAffrowsAsync(TEntity data) => RemoveRangeAffrowsAsync(new[] { data });
|
||||||
|
async internal Task<int> RemoveRangeAffrowsAsync(IEnumerable<TEntity> data) {
|
||||||
|
if (CanRemove(data, true) == false) return 0;
|
||||||
|
var dels = new List<EntityState>();
|
||||||
|
foreach (var item in data) {
|
||||||
|
var state = CreateEntityState(item);
|
||||||
|
if (_states.ContainsKey(state.Key)) _states.Remove(state.Key);
|
||||||
|
_fsql.ClearEntityPrimaryValueWithIdentityAndGuid(item);
|
||||||
|
|
||||||
|
if (IsNoneDbContext) dels.Add(state);
|
||||||
|
EnqueueToDbContext(DbContext.ExecCommandInfoType.Delete, state);
|
||||||
|
}
|
||||||
|
if (IsNoneDbContext) {
|
||||||
|
var affrows = await DbContextBetchRemoveAsync(dels.ToArray());
|
||||||
|
IncrAffrows(affrows);
|
||||||
|
return affrows;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
internal Task RemoveAsync(TEntity data) => RemoveAffrowsAsync(data);
|
||||||
|
internal Task RemoveRangeAsync(IEnumerable<TEntity> data) => RemoveRangeAffrowsAsync(data);
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -6,6 +6,13 @@ using System.Linq;
|
|||||||
namespace FreeSql {
|
namespace FreeSql {
|
||||||
partial class DbSet<TEntity> {
|
partial class DbSet<TEntity> {
|
||||||
|
|
||||||
|
void DbContextExecCommand() {
|
||||||
|
if (IsNoneDbContext == false) {
|
||||||
|
_dicUpdateTimes.Clear();
|
||||||
|
_ctx.ExecCommand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int DbContextBetchAdd(EntityState[] adds) {
|
int DbContextBetchAdd(EntityState[] adds) {
|
||||||
if (adds.Any() == false) return 0;
|
if (adds.Any() == false) return 0;
|
||||||
var affrows = this.OrmInsert(adds.Select(a => a.Value)).ExecuteAffrows();
|
var affrows = this.OrmInsert(adds.Select(a => a.Value)).ExecuteAffrows();
|
||||||
@ -21,14 +28,14 @@ namespace FreeSql {
|
|||||||
case DataType.SqlServer:
|
case DataType.SqlServer:
|
||||||
case DataType.PostgreSQL:
|
case DataType.PostgreSQL:
|
||||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||||
ExecuteCommand();
|
DbContextExecCommand();
|
||||||
var idtval = this.OrmInsert(data).ExecuteIdentity();
|
var idtval = this.OrmInsert(data).ExecuteIdentity();
|
||||||
IncrAffrows(1);
|
IncrAffrows(1);
|
||||||
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
||||||
var state = CreateEntityState(data);
|
var state = CreateEntityState(data);
|
||||||
_states.Add(state.Key, state);
|
_states.Add(state.Key, state);
|
||||||
} else {
|
} else {
|
||||||
ExecuteCommand();
|
DbContextExecCommand();
|
||||||
var newval = this.OrmInsert(data).ExecuteInserted().First();
|
var newval = this.OrmInsert(data).ExecuteInserted().First();
|
||||||
IncrAffrows(1);
|
IncrAffrows(1);
|
||||||
_fsql.MapEntityValue(newval, data);
|
_fsql.MapEntityValue(newval, data);
|
||||||
@ -40,7 +47,7 @@ namespace FreeSql {
|
|||||||
case DataType.Oracle:
|
case DataType.Oracle:
|
||||||
case DataType.Sqlite:
|
case DataType.Sqlite:
|
||||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||||
ExecuteCommand();
|
DbContextExecCommand();
|
||||||
var idtval = this.OrmInsert(data).ExecuteIdentity();
|
var idtval = this.OrmInsert(data).ExecuteIdentity();
|
||||||
IncrAffrows(1);
|
IncrAffrows(1);
|
||||||
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
||||||
@ -50,47 +57,13 @@ namespace FreeSql {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//进入队列,等待 SaveChanges 时执行
|
if (IsNoneDbContext)
|
||||||
EnqueueAction(DbContext.ExecCommandInfoType.Insert, this, typeof(EntityState), CreateEntityState(data));
|
IncrAffrows(OrmInsert(data).ExecuteAffrows());
|
||||||
|
else
|
||||||
|
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void Add(TEntity data) => AddPriv(data, true);
|
public void Add(TEntity data) => AddPriv(data, true);
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region AddRange
|
|
||||||
public void AddRange(TEntity[] data) {
|
|
||||||
if (CanAdd(data, true) == false) return;
|
|
||||||
if (data.Length == 1) {
|
|
||||||
Add(data.First());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (_tableIdentitys.Length > 0) {
|
|
||||||
//有自增,马上执行
|
|
||||||
switch (_fsql.Ado.DataType) {
|
|
||||||
case DataType.SqlServer:
|
|
||||||
case DataType.PostgreSQL:
|
|
||||||
ExecuteCommand();
|
|
||||||
var rets = this.OrmInsert(data).ExecuteInserted();
|
|
||||||
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_fsql.Ado.DataType} 的返回数据,与添加的数目不匹配");
|
|
||||||
var idx = 0;
|
|
||||||
foreach (var s in data)
|
|
||||||
_fsql.MapEntityValue(rets[idx++], s);
|
|
||||||
IncrAffrows(rets.Count);
|
|
||||||
TrackToList(rets);
|
|
||||||
return;
|
|
||||||
case DataType.MySql:
|
|
||||||
case DataType.Oracle:
|
|
||||||
case DataType.Sqlite:
|
|
||||||
foreach (var s in data)
|
|
||||||
AddPriv(s, false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//进入队列,等待 SaveChanges 时执行
|
|
||||||
foreach (var s in data)
|
|
||||||
EnqueueAction(DbContext.ExecCommandInfoType.Insert, this, typeof(EntityState), CreateEntityState(s));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void AddRange(IEnumerable<TEntity> data) {
|
public void AddRange(IEnumerable<TEntity> data) {
|
||||||
if (CanAdd(data, true) == false) return;
|
if (CanAdd(data, true) == false) return;
|
||||||
if (data.ElementAtOrDefault(1) == default(TEntity)) {
|
if (data.ElementAtOrDefault(1) == default(TEntity)) {
|
||||||
@ -102,7 +75,7 @@ namespace FreeSql {
|
|||||||
switch (_fsql.Ado.DataType) {
|
switch (_fsql.Ado.DataType) {
|
||||||
case DataType.SqlServer:
|
case DataType.SqlServer:
|
||||||
case DataType.PostgreSQL:
|
case DataType.PostgreSQL:
|
||||||
ExecuteCommand();
|
DbContextExecCommand();
|
||||||
var rets = this.OrmInsert(data).ExecuteInserted();
|
var rets = this.OrmInsert(data).ExecuteInserted();
|
||||||
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_fsql.Ado.DataType} 的返回数据,与添加的数目不匹配");
|
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_fsql.Ado.DataType} 的返回数据,与添加的数目不匹配");
|
||||||
var idx = 0;
|
var idx = 0;
|
||||||
@ -119,13 +92,17 @@ namespace FreeSql {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (IsNoneDbContext)
|
||||||
|
IncrAffrows(OrmInsert(data).ExecuteAffrows());
|
||||||
|
else
|
||||||
//进入队列,等待 SaveChanges 时执行
|
//进入队列,等待 SaveChanges 时执行
|
||||||
foreach (var s in data)
|
foreach (var s in data)
|
||||||
EnqueueAction(DbContext.ExecCommandInfoType.Insert, this, typeof(EntityState), CreateEntityState(s));
|
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(s));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Update
|
||||||
int DbContextBetchUpdate(EntityState[] ups) => DbContextBetchUpdatePriv(ups, false);
|
int DbContextBetchUpdate(EntityState[] ups) => DbContextBetchUpdatePriv(ups, false);
|
||||||
int DbContextBetchUpdateNow(EntityState[] ups) => DbContextBetchUpdatePriv(ups, true);
|
int DbContextBetchUpdateNow(EntityState[] ups) => DbContextBetchUpdatePriv(ups, true);
|
||||||
int DbContextBetchUpdatePriv(EntityState[] ups, bool isLiveUpdate) {
|
int DbContextBetchUpdatePriv(EntityState[] ups, bool isLiveUpdate) {
|
||||||
@ -176,47 +153,86 @@ namespace FreeSql {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdatePriv(TEntity data, bool isCheck) {
|
Dictionary<TEntity, byte> _dicUpdateTimes = new Dictionary<TEntity, byte>();
|
||||||
if (isCheck && CanUpdate(data, true) == false) return;
|
internal int UpdateAffrows(TEntity data) => UpdateRangeAffrows(new[] { data });
|
||||||
var state = CreateEntityState(data);
|
internal int UpdateRangeAffrows(IEnumerable<TEntity> data) {
|
||||||
state.OldValue = data;
|
if (CanUpdate(data, true) == false) return 0;
|
||||||
EnqueueAction(DbContext.ExecCommandInfoType.Update, this, typeof(EntityState), state);
|
if (IsNoneDbContext) {
|
||||||
}
|
var dataarray = data.ToArray();
|
||||||
public void Update(TEntity data) => UpdatePriv(data, true);
|
var ups = new List<EntityState>();
|
||||||
public void UpdateRange(TEntity[] data) {
|
var totalAffrows = 0;
|
||||||
if (CanUpdate(data, true) == false) return;
|
for (var a = 0; a < dataarray.Length + 1; a++) {
|
||||||
foreach (var item in data)
|
var item = a < dataarray.Length ? dataarray[a] : null;
|
||||||
UpdatePriv(item, false);
|
if (item != null) {
|
||||||
}
|
var state = CreateEntityState(item);
|
||||||
public void UpdateRange(IEnumerable<TEntity> data) {
|
state.Value = item;
|
||||||
if (CanUpdate(data, true) == false) return;
|
ups.Add(state);
|
||||||
foreach (var item in data)
|
|
||||||
UpdatePriv(item, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var affrows = DbContextBetchUpdatePriv(ups.ToArray(), item == null);
|
||||||
|
if (affrows == -999) { //最后一个元素已被删除
|
||||||
|
ups.RemoveAt(ups.Count - 1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (affrows == -998 || affrows == -997) { //没有执行更新
|
||||||
|
var laststate = ups[ups.Count - 1];
|
||||||
|
ups.Clear();
|
||||||
|
if (affrows == -997) ups.Add(laststate); //保留最后一个
|
||||||
|
}
|
||||||
|
if (affrows > 0) {
|
||||||
|
totalAffrows += affrows;
|
||||||
|
var islastNotUpdated = ups.Count != affrows;
|
||||||
|
var laststate = ups[ups.Count - 1];
|
||||||
|
ups.Clear();
|
||||||
|
if (islastNotUpdated) ups.Add(laststate); //保留最后一个
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IncrAffrows(totalAffrows);
|
||||||
|
return totalAffrows;
|
||||||
|
}
|
||||||
|
foreach (var item in data) {
|
||||||
|
if (_dicUpdateTimes.ContainsKey(item))
|
||||||
|
DbContextExecCommand();
|
||||||
|
_dicUpdateTimes.Add(item, 1);
|
||||||
|
|
||||||
|
var state = CreateEntityState(item);
|
||||||
|
state.OldValue = item;
|
||||||
|
EnqueueToDbContext(DbContext.ExecCommandInfoType.Update, state);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
public void Update(TEntity data) => UpdateAffrows(data);
|
||||||
|
public void UpdateRange(IEnumerable<TEntity> data) => UpdateRangeAffrows(data);
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Remove
|
||||||
int DbContextBetchRemove(EntityState[] dels) {
|
int DbContextBetchRemove(EntityState[] dels) {
|
||||||
if (dels.Any() == false) return 0;
|
if (dels.Any() == false) return 0;
|
||||||
var affrows = this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrows();
|
var affrows = this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrows();
|
||||||
return Math.Max(dels.Length, affrows);
|
return Math.Max(dels.Length, affrows);
|
||||||
}
|
}
|
||||||
void RemovePriv(TEntity data, bool isCheck) {
|
|
||||||
if (isCheck && CanRemove(data, true) == false) return;
|
|
||||||
var state = CreateEntityState(data);
|
|
||||||
EnqueueAction(DbContext.ExecCommandInfoType.Delete, this, typeof(EntityState), state);
|
|
||||||
|
|
||||||
|
internal int RemoveAffrows(TEntity data) => RemoveRangeAffrows(new[] { data });
|
||||||
|
internal int RemoveRangeAffrows(IEnumerable<TEntity> data) {
|
||||||
|
if (CanRemove(data, true) == false) return 0;
|
||||||
|
var dels = new List<EntityState>();
|
||||||
|
foreach (var item in data) {
|
||||||
|
var state = CreateEntityState(item);
|
||||||
if (_states.ContainsKey(state.Key)) _states.Remove(state.Key);
|
if (_states.ContainsKey(state.Key)) _states.Remove(state.Key);
|
||||||
_fsql.ClearEntityPrimaryValueWithIdentityAndGuid(data);
|
_fsql.ClearEntityPrimaryValueWithIdentityAndGuid(item);
|
||||||
|
|
||||||
|
if (IsNoneDbContext) dels.Add(state);
|
||||||
|
EnqueueToDbContext(DbContext.ExecCommandInfoType.Delete, state);
|
||||||
}
|
}
|
||||||
public void Remove(TEntity data) => RemovePriv(data, true);
|
if (IsNoneDbContext) {
|
||||||
public void RemoveRange(TEntity[] data) {
|
var affrows = DbContextBetchRemove(dels.ToArray());
|
||||||
if (CanRemove(data, true) == false) return;
|
IncrAffrows(affrows);
|
||||||
foreach (var item in data)
|
return affrows;
|
||||||
RemovePriv(item, false);
|
|
||||||
}
|
|
||||||
public void RemoveRange(IEnumerable<TEntity> data) {
|
|
||||||
if (CanRemove(data, true) == false) return;
|
|
||||||
foreach (var item in data)
|
|
||||||
RemovePriv(item, false);
|
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
public void Remove(TEntity data) => RemoveAffrows(data);
|
||||||
|
public void RemoveRange(IEnumerable<TEntity> data) => RemoveRangeAffrows(data);
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
153
FreeSql.DbContext/Repository/DataFilter/DataFilter.cs
Normal file
153
FreeSql.DbContext/Repository/DataFilter/DataFilter.cs
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
public interface IDataFilter<TEntity> : IDisposable where TEntity : class {
|
||||||
|
|
||||||
|
IDataFilter<TEntity> Apply(string filterName, Expression<Func<TEntity, bool>> filterAndValidateExp);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开启过滤器,若使用 using 则使用完后,恢复为原有状态
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filterName">过滤器名称</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
IDisposable Enable(params string[] filterName);
|
||||||
|
/// <summary>
|
||||||
|
/// 开启所有过滤器,若使用 using 则使用完后,恢复为原有状态
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
IDisposable EnableAll();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 禁用过滤器,若使用 using 则使用完后,恢复为原有状态
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filterName"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
IDisposable Disable(params string[] filterName);
|
||||||
|
/// <summary>
|
||||||
|
/// 禁用所有过滤器,若使用 using 则使用完后,恢复为原有状态
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
IDisposable DisableAll();
|
||||||
|
|
||||||
|
bool IsEnabled(string filterName);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class DataFilter<TEntity> : IDataFilter<TEntity> where TEntity : class {
|
||||||
|
|
||||||
|
internal class FilterItem {
|
||||||
|
public Expression<Func<TEntity, bool>> Expression { get; set; }
|
||||||
|
Func<TEntity, bool> _expressionDelegate;
|
||||||
|
public Func<TEntity, bool> ExpressionDelegate => _expressionDelegate ?? (_expressionDelegate = Expression?.Compile());
|
||||||
|
public bool IsEnabled { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
internal ConcurrentDictionary<string, FilterItem> _filters = new ConcurrentDictionary<string, FilterItem>(StringComparer.CurrentCultureIgnoreCase);
|
||||||
|
public IDataFilter<TEntity> Apply(string filterName, Expression<Func<TEntity, bool>> filterAndValidateExp) {
|
||||||
|
|
||||||
|
if (filterName == null)
|
||||||
|
throw new ArgumentNullException(nameof(filterName));
|
||||||
|
if (filterAndValidateExp == null) return this;
|
||||||
|
|
||||||
|
var filterItem = new FilterItem { Expression = filterAndValidateExp, IsEnabled = true };
|
||||||
|
_filters.AddOrUpdate(filterName, filterItem, (k, v) => filterItem);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IDisposable Disable(params string[] filterName) {
|
||||||
|
if (filterName == null || filterName.Any() == false) return new UsingAny(() => { });
|
||||||
|
|
||||||
|
List<string> restore = new List<string>();
|
||||||
|
foreach (var name in filterName) {
|
||||||
|
if (_filters.TryGetValue(name, out var tryfi)) {
|
||||||
|
if (tryfi.IsEnabled) {
|
||||||
|
restore.Add(name);
|
||||||
|
tryfi.IsEnabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new UsingAny(() => this.Enable(restore.ToArray()));
|
||||||
|
}
|
||||||
|
public IDisposable DisableAll() {
|
||||||
|
List<string> restore = new List<string>();
|
||||||
|
foreach (var val in _filters) {
|
||||||
|
if (val.Value.IsEnabled) {
|
||||||
|
restore.Add(val.Key);
|
||||||
|
val.Value.IsEnabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new UsingAny(() => this.Enable(restore.ToArray()));
|
||||||
|
}
|
||||||
|
class UsingAny : IDisposable {
|
||||||
|
Action _ondis;
|
||||||
|
public UsingAny(Action ondis) {
|
||||||
|
_ondis = ondis;
|
||||||
|
}
|
||||||
|
public void Dispose() {
|
||||||
|
_ondis?.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IDisposable Enable(params string[] filterName) {
|
||||||
|
if (filterName == null || filterName.Any() == false) return new UsingAny(() => { });
|
||||||
|
|
||||||
|
List<string> restore = new List<string>();
|
||||||
|
foreach (var name in filterName) {
|
||||||
|
if (_filters.TryGetValue(name, out var tryfi)) {
|
||||||
|
if (tryfi.IsEnabled == false) {
|
||||||
|
restore.Add(name);
|
||||||
|
tryfi.IsEnabled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new UsingAny(() => this.Disable(restore.ToArray()));
|
||||||
|
}
|
||||||
|
public IDisposable EnableAll() {
|
||||||
|
List<string> restore = new List<string>();
|
||||||
|
foreach (var val in _filters) {
|
||||||
|
if (val.Value.IsEnabled == false) {
|
||||||
|
restore.Add(val.Key);
|
||||||
|
val.Value.IsEnabled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new UsingAny(() => this.Disable(restore.ToArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsEnabled(string filterName) {
|
||||||
|
if (filterName == null) return false;
|
||||||
|
return _filters.TryGetValue(filterName, out var tryfi) ? tryfi.IsEnabled : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
~DataFilter() {
|
||||||
|
this.Dispose();
|
||||||
|
}
|
||||||
|
public void Dispose() {
|
||||||
|
_filters.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class FluentDataFilter : IDisposable {
|
||||||
|
|
||||||
|
internal List<(Type type, string name, LambdaExpression exp)> _filters = new List<(Type type, string name, LambdaExpression exp)>();
|
||||||
|
|
||||||
|
public FluentDataFilter Apply<TEntity>(string filterName, Expression<Func<TEntity, bool>> filterAndValidateExp) where TEntity : class {
|
||||||
|
if (filterName == null)
|
||||||
|
throw new ArgumentNullException(nameof(filterName));
|
||||||
|
if (filterAndValidateExp == null) return this;
|
||||||
|
|
||||||
|
_filters.Add((typeof(TEntity), filterName, filterAndValidateExp));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
~FluentDataFilter() {
|
||||||
|
this.Dispose();
|
||||||
|
}
|
||||||
|
public void Dispose() {
|
||||||
|
_filters.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
90
FreeSql.DbContext/Repository/DataFilter/DataFilterUtil.cs
Normal file
90
FreeSql.DbContext/Repository/DataFilter/DataFilterUtil.cs
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
|
||||||
|
internal class DataFilterUtil {
|
||||||
|
|
||||||
|
internal static Action<FluentDataFilter> _globalDataFilter;
|
||||||
|
|
||||||
|
static ConcurrentDictionary<Type, Delegate> _dicSetRepositoryDataFilterApplyDataFilterFunc = new ConcurrentDictionary<Type, Delegate>();
|
||||||
|
static ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>> _dicSetRepositoryDataFilterConvertFilterNotExists = new ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>>();
|
||||||
|
internal static void SetRepositoryDataFilter(object repos, Action<FluentDataFilter> scopedDataFilter) {
|
||||||
|
if (scopedDataFilter != null) {
|
||||||
|
SetRepositoryDataFilter(repos, null);
|
||||||
|
}
|
||||||
|
if (scopedDataFilter == null) {
|
||||||
|
scopedDataFilter = _globalDataFilter;
|
||||||
|
}
|
||||||
|
if (scopedDataFilter == null) return;
|
||||||
|
using (var globalFilter = new FluentDataFilter()) {
|
||||||
|
scopedDataFilter(globalFilter);
|
||||||
|
|
||||||
|
var type = repos.GetType();
|
||||||
|
Type entityType = (repos as IRepository).EntityType;
|
||||||
|
if (entityType == null) throw new Exception("FreeSql.Repository 设置过滤器失败,原因是对象不属于 IRepository");
|
||||||
|
|
||||||
|
var notExists = _dicSetRepositoryDataFilterConvertFilterNotExists.GetOrAdd(type, t => new ConcurrentDictionary<string, bool>());
|
||||||
|
var newFilter = new Dictionary<string, LambdaExpression>();
|
||||||
|
foreach (var gf in globalFilter._filters) {
|
||||||
|
if (notExists.ContainsKey(gf.name)) continue;
|
||||||
|
|
||||||
|
LambdaExpression newExp = null;
|
||||||
|
var filterParameter1 = Expression.Parameter(entityType, gf.exp.Parameters[0].Name);
|
||||||
|
try {
|
||||||
|
newExp = Expression.Lambda(
|
||||||
|
typeof(Func<,>).MakeGenericType(entityType, typeof(bool)),
|
||||||
|
new ReplaceVisitor().Modify(gf.exp.Body, filterParameter1),
|
||||||
|
filterParameter1
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
notExists.TryAdd(gf.name, true); //防止第二次错误
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
newFilter.Add(gf.name, newExp);
|
||||||
|
}
|
||||||
|
if (newFilter.Any() == false) return;
|
||||||
|
|
||||||
|
var del = _dicSetRepositoryDataFilterApplyDataFilterFunc.GetOrAdd(type, t => {
|
||||||
|
var reposParameter = Expression.Parameter(type);
|
||||||
|
var nameParameter = Expression.Parameter(typeof(string));
|
||||||
|
var expressionParameter = Expression.Parameter(
|
||||||
|
typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(entityType, typeof(bool)))
|
||||||
|
);
|
||||||
|
return Expression.Lambda(
|
||||||
|
Expression.Block(
|
||||||
|
Expression.Call(reposParameter, type.GetMethod("ApplyDataFilter", BindingFlags.Instance | BindingFlags.NonPublic), nameParameter, expressionParameter)
|
||||||
|
),
|
||||||
|
new[] {
|
||||||
|
reposParameter, nameParameter, expressionParameter
|
||||||
|
}
|
||||||
|
).Compile();
|
||||||
|
});
|
||||||
|
foreach (var nf in newFilter) {
|
||||||
|
del.DynamicInvoke(repos, nf.Key, nf.Value);
|
||||||
|
}
|
||||||
|
newFilter.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ReplaceVisitor : ExpressionVisitor {
|
||||||
|
private ParameterExpression parameter;
|
||||||
|
|
||||||
|
public Expression Modify(Expression expression, ParameterExpression parameter) {
|
||||||
|
this.parameter = parameter;
|
||||||
|
return Visit(expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override Expression VisitMember(MemberExpression node) {
|
||||||
|
if (node.Expression?.NodeType == ExpressionType.Parameter)
|
||||||
|
return Expression.Property(parameter, node.Member.Name);
|
||||||
|
return base.VisitMember(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
using FreeSql;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
public static class FreeSqlRepositoryDependencyInjection {
|
||||||
|
|
||||||
|
public static IServiceCollection AddFreeRepository(this IServiceCollection services, Action<FluentDataFilter> globalDataFilter = null, params Assembly[] assemblies) {
|
||||||
|
|
||||||
|
DataFilterUtil._globalDataFilter = globalDataFilter;
|
||||||
|
|
||||||
|
services.AddScoped(typeof(IReadOnlyRepository<>), typeof(GuidRepository<>));
|
||||||
|
services.AddScoped(typeof(IBasicRepository<>), typeof(GuidRepository<>));
|
||||||
|
services.AddScoped(typeof(BaseRepository<>), typeof(GuidRepository<>));
|
||||||
|
services.AddScoped(typeof(GuidRepository<>));
|
||||||
|
|
||||||
|
services.AddScoped(typeof(IReadOnlyRepository<,>), typeof(DefaultRepository<,>));
|
||||||
|
services.AddScoped(typeof(IBasicRepository<,>), typeof(DefaultRepository<,>));
|
||||||
|
services.AddScoped(typeof(BaseRepository<,>), typeof(DefaultRepository<,>));
|
||||||
|
services.AddScoped(typeof(DefaultRepository<,>));
|
||||||
|
|
||||||
|
if (assemblies?.Any() == true) {
|
||||||
|
foreach(var asse in assemblies) {
|
||||||
|
foreach (var repos in asse.GetTypes().Where(a => a.IsAbstract == false && typeof(IRepository).IsAssignableFrom(a))) {
|
||||||
|
|
||||||
|
services.AddScoped(repos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
using FreeSql;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Text;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Data;
|
||||||
|
|
||||||
|
public static class FreeSqlRepositoryExtenssions {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 返回默认仓库类
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity"></typeparam>
|
||||||
|
/// <typeparam name="TKey"></typeparam>
|
||||||
|
/// <param name="that"></param>
|
||||||
|
/// <param name="filter">数据过滤 + 验证</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
|
||||||
|
return new DefaultRepository<TEntity, TKey>(that, filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 返回仓库类,适用 Insert 方法无须返回插入的数据
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity"></typeparam>
|
||||||
|
/// <param name="that"></param>
|
||||||
|
/// <param name="filter">数据过滤 + 验证</param>
|
||||||
|
/// <param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static GuidRepository<TEntity> GetGuidRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class {
|
||||||
|
return new GuidRepository<TEntity>(that, filter, asTable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 合并两个仓储的设置(过滤+分表),以便查询
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity"></typeparam>
|
||||||
|
/// <typeparam name="T2"></typeparam>
|
||||||
|
/// <param name="that"></param>
|
||||||
|
/// <param name="repos"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static ISelect<TEntity> FromRepository<TEntity, T2>(this ISelect<TEntity> that, BaseRepository<T2> repos) where TEntity : class where T2 : class {
|
||||||
|
var filters = (repos.DataFilter as DataFilter<T2>)._filters.Where(a => a.Value.IsEnabled == true);
|
||||||
|
foreach (var filter in filters) that.Where<T2>(filter.Value.Expression);
|
||||||
|
return that.AsTable(repos.AsTableSelectInternal);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建基于仓储功能的工作单元,务必使用 using 包含使用
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="that"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IRepositoryUnitOfWork CreateUnitOfWork(this IFreeSql that) {
|
||||||
|
return new RepositoryUnitOfWork(that);
|
||||||
|
}
|
||||||
|
}
|
99
FreeSql.DbContext/Repository/Repository/BaseRepository.cs
Normal file
99
FreeSql.DbContext/Repository/Repository/BaseRepository.cs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
using FreeSql.Extensions.EntityUtil;
|
||||||
|
using FreeSql.Internal.Model;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
public abstract class BaseRepository<TEntity> : IRepository<TEntity>
|
||||||
|
where TEntity : class {
|
||||||
|
|
||||||
|
internal IFreeSql _fsql;
|
||||||
|
internal UnitOfWork _uow;
|
||||||
|
RepositoryDbSet<TEntity> _setPriv;
|
||||||
|
internal RepositoryDbSet<TEntity> _set => _setPriv ?? (_setPriv = new RepositoryDbSet<TEntity>(this));
|
||||||
|
public IDataFilter<TEntity> DataFilter { get; } = new DataFilter<TEntity>();
|
||||||
|
Func<string, string> _asTableVal;
|
||||||
|
protected Func<string, string> AsTable {
|
||||||
|
get => _asTableVal;
|
||||||
|
set {
|
||||||
|
_asTableVal = value;
|
||||||
|
AsTableSelect = value == null ? null : new Func<Type, string, string>((a, b) => a == EntityType ? value(b) : null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
internal Func<string, string> AsTableInternal => AsTable;
|
||||||
|
protected Func<Type, string, string> AsTableSelect { get; private set; }
|
||||||
|
internal Func<Type, string, string> AsTableSelectInternal => AsTableSelect;
|
||||||
|
|
||||||
|
protected BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) {
|
||||||
|
_fsql = fsql;
|
||||||
|
DataFilterUtil.SetRepositoryDataFilter(this, null);
|
||||||
|
DataFilter.Apply("", filter);
|
||||||
|
AsTable = asTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Type EntityType => _set._entityTypeInternal;
|
||||||
|
public IUpdate<TEntity> UpdateDiy => _set.OrmUpdateInternal(null);
|
||||||
|
|
||||||
|
|
||||||
|
public ISelect<TEntity> Select => _set.OrmSelectInternal(null);
|
||||||
|
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => _set.OrmSelectInternal(null).Where(exp);
|
||||||
|
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => _set.OrmSelectInternal(null).WhereIf(condition, exp);
|
||||||
|
|
||||||
|
public int Delete(Expression<Func<TEntity, bool>> predicate) => _set.OrmDeleteInternal(null).Where(predicate).ExecuteAffrows();
|
||||||
|
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _set.OrmDeleteInternal(null).Where(predicate).ExecuteAffrowsAsync();
|
||||||
|
|
||||||
|
public int Delete(TEntity entity) => _set.RemoveAffrows(entity);
|
||||||
|
public Task<int> DeleteAsync(TEntity entity) => _set.RemoveAffrowsAsync(entity);
|
||||||
|
public int Delete(IEnumerable<TEntity> entitys) => _set.RemoveRangeAffrows(entitys);
|
||||||
|
public Task<int> DeleteAsync(IEnumerable<TEntity> entitys) => _set.RemoveRangeAffrowsAsync(entitys);
|
||||||
|
|
||||||
|
public virtual TEntity Insert(TEntity entity) {
|
||||||
|
_set.Add(entity);
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
public virtual List<TEntity> Insert(IEnumerable<TEntity> entitys) {
|
||||||
|
_set.AddRange(entitys);
|
||||||
|
return entitys.ToList();
|
||||||
|
}
|
||||||
|
async public virtual Task<TEntity> InsertAsync(TEntity entity) {
|
||||||
|
await _set.AddAsync(entity);
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
async public virtual Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys) {
|
||||||
|
await _set.AddRangeAsync(entitys);
|
||||||
|
return entitys.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Update(TEntity entity) => _set.UpdateAffrows(entity);
|
||||||
|
public Task<int> UpdateAsync(TEntity entity) => _set.UpdateAffrowsAsync(entity);
|
||||||
|
public int Update(IEnumerable<TEntity> entitys) => _set.UpdateRangeAffrows(entitys);
|
||||||
|
public Task<int> UpdateAsync(IEnumerable<TEntity> entitys) => _set.UpdateRangeAffrowsAsync(entitys);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class BaseRepository<TEntity, TKey> : BaseRepository<TEntity>, IRepository<TEntity, TKey>
|
||||||
|
where TEntity : class {
|
||||||
|
|
||||||
|
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base(fsql, filter, asTable) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Delete(TKey id) {
|
||||||
|
var stateKey = string.Concat(id);
|
||||||
|
if (_set._statesInternal.ContainsKey(stateKey)) _set._statesInternal.Remove(stateKey);
|
||||||
|
return _set.OrmDeleteInternal(id).ExecuteAffrows();
|
||||||
|
}
|
||||||
|
public Task<int> DeleteAsync(TKey id) {
|
||||||
|
var stateKey = string.Concat(id);
|
||||||
|
if (_set._statesInternal.ContainsKey(stateKey)) _set._statesInternal.Remove(stateKey);
|
||||||
|
return _set.OrmDeleteInternal(id).ExecuteAffrowsAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TEntity Find(TKey id) => _set.OrmSelectInternal(id).ToOne();
|
||||||
|
public Task<TEntity> FindAsync(TKey id) => _set.OrmSelectInternal(id).ToOneAsync();
|
||||||
|
|
||||||
|
public TEntity Get(TKey id) => Find(id);
|
||||||
|
public Task<TEntity> GetAsync(TKey id) => FindAsync(id);
|
||||||
|
}
|
||||||
|
}
|
19
FreeSql.DbContext/Repository/Repository/DefaultRepository.cs
Normal file
19
FreeSql.DbContext/Repository/Repository/DefaultRepository.cs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
public class DefaultRepository<TEntity, TKey> :
|
||||||
|
BaseRepository<TEntity, TKey>
|
||||||
|
where TEntity : class {
|
||||||
|
|
||||||
|
public DefaultRepository(IFreeSql fsql) : base(fsql, null, null) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public DefaultRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter, null) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
FreeSql.DbContext/Repository/Repository/GuidRepository.cs
Normal file
19
FreeSql.DbContext/Repository/Repository/GuidRepository.cs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
public class GuidRepository<TEntity> :
|
||||||
|
BaseRepository<TEntity, Guid>
|
||||||
|
where TEntity : class {
|
||||||
|
|
||||||
|
public GuidRepository(IFreeSql fsql) : this(fsql, null, null) {
|
||||||
|
|
||||||
|
}
|
||||||
|
public GuidRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable) : base(fsql, filter, asTable) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
FreeSql.DbContext/Repository/Repository/IBasicRepository.cs
Normal file
32
FreeSql.DbContext/Repository/Repository/IBasicRepository.cs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
public interface IBasicRepository<TEntity> : IReadOnlyRepository<TEntity>
|
||||||
|
where TEntity : class {
|
||||||
|
TEntity Insert(TEntity entity);
|
||||||
|
List<TEntity> Insert(IEnumerable<TEntity> entitys);
|
||||||
|
Task<TEntity> InsertAsync(TEntity entity);
|
||||||
|
Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys);
|
||||||
|
|
||||||
|
int Update(TEntity entity);
|
||||||
|
int Update(IEnumerable<TEntity> entitys);
|
||||||
|
Task<int> UpdateAsync(TEntity entity);
|
||||||
|
Task<int> UpdateAsync(IEnumerable<TEntity> entitys);
|
||||||
|
|
||||||
|
IUpdate<TEntity> UpdateDiy { get; }
|
||||||
|
|
||||||
|
int Delete(TEntity entity);
|
||||||
|
int Delete(IEnumerable<TEntity> entitys);
|
||||||
|
Task<int> DeleteAsync(TEntity entity);
|
||||||
|
Task<int> DeleteAsync(IEnumerable<TEntity> entitys);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IBasicRepository<TEntity, TKey> : IBasicRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>
|
||||||
|
where TEntity : class {
|
||||||
|
int Delete(TKey id);
|
||||||
|
|
||||||
|
Task<int> DeleteAsync(TKey id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
public interface IReadOnlyRepository<TEntity> : IRepository
|
||||||
|
where TEntity : class {
|
||||||
|
|
||||||
|
IDataFilter<TEntity> DataFilter { get; }
|
||||||
|
|
||||||
|
ISelect<TEntity> Select { get; }
|
||||||
|
|
||||||
|
ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp);
|
||||||
|
ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IReadOnlyRepository<TEntity, TKey> : IReadOnlyRepository<TEntity>
|
||||||
|
where TEntity : class {
|
||||||
|
TEntity Get(TKey id);
|
||||||
|
|
||||||
|
Task<TEntity> GetAsync(TKey id);
|
||||||
|
|
||||||
|
TEntity Find(TKey id);
|
||||||
|
|
||||||
|
Task<TEntity> FindAsync(TKey id);
|
||||||
|
}
|
||||||
|
}
|
21
FreeSql.DbContext/Repository/Repository/IRepository.cs
Normal file
21
FreeSql.DbContext/Repository/Repository/IRepository.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
|
||||||
|
public interface IRepository {
|
||||||
|
Type EntityType { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IRepository<TEntity> : IReadOnlyRepository<TEntity>, IBasicRepository<TEntity>
|
||||||
|
where TEntity : class {
|
||||||
|
int Delete(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
|
Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IRepository<TEntity, TKey> : IRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>, IBasicRepository<TEntity, TKey>
|
||||||
|
where TEntity : class {
|
||||||
|
}
|
||||||
|
}
|
59
FreeSql.DbContext/Repository/Repository/RepositoryDbSet.cs
Normal file
59
FreeSql.DbContext/Repository/Repository/RepositoryDbSet.cs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
using FreeSql.Extensions.EntityUtil;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
internal class RepositoryDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
|
||||||
|
|
||||||
|
protected BaseRepository<TEntity> _repos;
|
||||||
|
public RepositoryDbSet(BaseRepository<TEntity> repos) {
|
||||||
|
_fsql = repos._fsql;
|
||||||
|
_uow = repos._uow;
|
||||||
|
_repos = repos;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override ISelect<TEntity> OrmSelect(object dywhere) {
|
||||||
|
var select = base.OrmSelect(dywhere);
|
||||||
|
var filters = (_repos.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
|
||||||
|
foreach (var filter in filters) select.Where(filter.Value.Expression);
|
||||||
|
return select.AsTable(_repos.AsTableSelectInternal);
|
||||||
|
}
|
||||||
|
internal ISelect<TEntity> OrmSelectInternal(object dywhere) => OrmSelect(dywhere);
|
||||||
|
protected override IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys) {
|
||||||
|
var update = base.OrmUpdate(entitys);
|
||||||
|
var filters = (_repos.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
|
||||||
|
foreach (var filter in filters) {
|
||||||
|
if (entitys != null)
|
||||||
|
foreach (var entity in entitys)
|
||||||
|
if (filter.Value.ExpressionDelegate?.Invoke(entity) == false)
|
||||||
|
throw new Exception($"FreeSql.Repository Update 失败,因为设置了过滤器 {filter.Key}: {filter.Value.Expression},更新的数据不符合 {_fsql.GetEntityString(entity)}");
|
||||||
|
update.Where(filter.Value.Expression);
|
||||||
|
}
|
||||||
|
return update.AsTable(_repos.AsTableInternal);
|
||||||
|
}
|
||||||
|
internal IUpdate<TEntity> OrmUpdateInternal(IEnumerable<TEntity> entitys) => OrmUpdate(entitys);
|
||||||
|
protected override IDelete<TEntity> OrmDelete(object dywhere) {
|
||||||
|
var delete = base.OrmDelete(dywhere);
|
||||||
|
var filters = (_repos.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
|
||||||
|
foreach (var filter in filters) delete.Where(filter.Value.Expression);
|
||||||
|
return delete.AsTable(_repos.AsTableInternal);
|
||||||
|
}
|
||||||
|
internal IDelete<TEntity> OrmDeleteInternal(object dywhere) => OrmDelete(dywhere);
|
||||||
|
protected override IInsert<TEntity> OrmInsert(TEntity entity) => OrmInsert(new[] { entity });
|
||||||
|
protected override IInsert<TEntity> OrmInsert(IEnumerable<TEntity> entitys) {
|
||||||
|
var insert = base.OrmInsert(entitys);
|
||||||
|
var filters = (_repos.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
|
||||||
|
foreach (var filter in filters) {
|
||||||
|
if (entitys != null)
|
||||||
|
foreach (var entity in entitys)
|
||||||
|
if (filter.Value.ExpressionDelegate?.Invoke(entity) == false)
|
||||||
|
throw new Exception($"FreeSql.Repository Insert 失败,因为设置了过滤器 {filter.Key}: {filter.Value.Expression},插入的数据不符合 {_fsql.GetEntityString(entity)}");
|
||||||
|
}
|
||||||
|
return insert.AsTable(_repos.AsTableInternal);
|
||||||
|
}
|
||||||
|
internal IInsert<TEntity> OrmInsertInternal(TEntity entity) => OrmInsert(entity);
|
||||||
|
internal IInsert<TEntity> OrmInsertInternal(IEnumerable<TEntity> entitys) => OrmInsert(entitys);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
class RepositoryUnitOfWork : UnitOfWork, IRepositoryUnitOfWork {
|
||||||
|
|
||||||
|
public RepositoryUnitOfWork(IFreeSql fsql) : base(fsql) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public GuidRepository<TEntity> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class {
|
||||||
|
var repos = new GuidRepository<TEntity>(_fsql, filter, asTable);
|
||||||
|
repos._uow = this;
|
||||||
|
return repos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
|
||||||
|
var repos = new DefaultRepository<TEntity, TKey>(_fsql, filter);
|
||||||
|
repos._uow = this;
|
||||||
|
return repos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
34
FreeSql.DbContext/UnitOfWork/IUnitOfWork.cs
Normal file
34
FreeSql.DbContext/UnitOfWork/IUnitOfWork.cs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
public interface IUnitOfWork : IDisposable {
|
||||||
|
|
||||||
|
void Commit();
|
||||||
|
|
||||||
|
void Rollback();
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IRepositoryUnitOfWork : IUnitOfWork {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 在工作单元内创建默认仓库类,工作单元下的仓储操作具有事务特点
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity"></typeparam>
|
||||||
|
/// <typeparam name="TKey"></typeparam>
|
||||||
|
/// <param name="filter">数据过滤 + 验证</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 在工作单元内创建仓库类,适用 Insert 方法无须返回插入的数据,工作单元下的仓储操作具有事务特点
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity"></typeparam>
|
||||||
|
/// <param name="filter">数据过滤 + 验证</param>
|
||||||
|
/// <param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
GuidRepository<TEntity> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class;
|
||||||
|
}
|
||||||
|
}
|
73
FreeSql.DbContext/UnitOfWork/UnitOfWork.cs
Normal file
73
FreeSql.DbContext/UnitOfWork/UnitOfWork.cs
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
using SafeObjectPool;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
class UnitOfWork : IUnitOfWork {
|
||||||
|
|
||||||
|
protected IFreeSql _fsql;
|
||||||
|
protected Object<DbConnection> _conn;
|
||||||
|
protected DbTransaction _tran;
|
||||||
|
|
||||||
|
public UnitOfWork(IFreeSql fsql) {
|
||||||
|
_fsql = fsql;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReturnObject() {
|
||||||
|
_fsql.Ado.MasterPool.Return(_conn);
|
||||||
|
_tran = null;
|
||||||
|
_conn = null;
|
||||||
|
}
|
||||||
|
internal DbTransaction GetOrBeginTransaction(bool isCreate = true) {
|
||||||
|
|
||||||
|
if (_tran != null) return _tran;
|
||||||
|
if (isCreate == false) return null;
|
||||||
|
if (_conn != null) _fsql.Ado.MasterPool.Return(_conn);
|
||||||
|
|
||||||
|
_conn = _fsql.Ado.MasterPool.Get();
|
||||||
|
try {
|
||||||
|
_tran = _conn.Value.BeginTransaction();
|
||||||
|
} catch {
|
||||||
|
ReturnObject();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return _tran;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Commit() {
|
||||||
|
if (_tran != null) {
|
||||||
|
try {
|
||||||
|
_tran.Commit();
|
||||||
|
} finally {
|
||||||
|
ReturnObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void Rollback() {
|
||||||
|
if (_tran != null) {
|
||||||
|
try {
|
||||||
|
_tran.Rollback();
|
||||||
|
} finally {
|
||||||
|
ReturnObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
~UnitOfWork() {
|
||||||
|
this.Dispose();
|
||||||
|
}
|
||||||
|
bool _isdisposed = false;
|
||||||
|
public void Dispose() {
|
||||||
|
if (_isdisposed) return;
|
||||||
|
try {
|
||||||
|
this.Rollback();
|
||||||
|
} finally {
|
||||||
|
_isdisposed = true;
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -14,7 +14,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
<ProjectReference Include="..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
||||||
<ProjectReference Include="..\FreeSql.Repository\FreeSql.Repository.csproj" />
|
|
||||||
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user