rename DbContext Internal Method

This commit is contained in:
28810
2020-04-11 12:02:50 +08:00
parent 0154600d0a
commit bd79fc803e
9 changed files with 140 additions and 125 deletions

View File

@ -225,7 +225,7 @@ namespace FreeSql
#endif
#endregion
#region Queue Action
#region Queue PreCommand
public class EntityChangeReport
{
public class ChangeInfo
@ -244,7 +244,7 @@ namespace FreeSql
}
internal List<EntityChangeReport.ChangeInfo> _entityChangeReport = new List<EntityChangeReport.ChangeInfo>();
public enum EntityChangeType { Insert, Update, Delete, SqlRaw }
internal class ExecCommandInfo
internal class PrevCommandInfo
{
public EntityChangeType changeType { get; set; }
public IDbSet dbSet { get; set; }
@ -252,11 +252,11 @@ namespace FreeSql
public Type entityType { get; set; }
public object state { get; set; }
}
Queue<ExecCommandInfo> _actions = new Queue<ExecCommandInfo>();
Queue<PrevCommandInfo> _prevCommands = new Queue<PrevCommandInfo>();
internal int _affrows = 0;
internal void EnqueueAction(EntityChangeType changeType, IDbSet dbSet, Type stateType, Type entityType, object state) =>
_actions.Enqueue(new ExecCommandInfo { changeType = changeType, dbSet = dbSet, stateType = stateType, entityType = entityType, state = state });
internal void EnqueuePreCommand(EntityChangeType changeType, IDbSet dbSet, Type stateType, Type entityType, object state) =>
_prevCommands.Enqueue(new PrevCommandInfo { changeType = changeType, dbSet = dbSet, stateType = stateType, entityType = entityType, state = state });
#endregion
~DbContext() => this.Dispose();
@ -266,7 +266,7 @@ namespace FreeSql
if (Interlocked.Increment(ref _disposeCounter) != 1) return;
try
{
_actions.Clear();
_prevCommands.Clear();
foreach (var set in _listSet)
try

View File

@ -1,8 +1,9 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
#if net40
@ -13,59 +14,58 @@ namespace FreeSql
{
async public virtual Task<int> SaveChangesAsync()
{
await ExecCommandAsync();
await FlushCommandAsync();
return SaveChangesSuccess();
}
static Dictionary<Type, Dictionary<string, Func<object, object[], Task<int>>>> _dicExecCommandDbContextBatchAsync = new Dictionary<Type, Dictionary<string, Func<object, object[], Task<int>>>>();
async internal Task ExecCommandAsync()
static ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], Task<int>>>> _dicFlushCommandDbSetBatchAsync = new ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], Task<int>>>>();
async internal Task FlushCommandAsync()
{
if (isExecCommanding) return;
if (_actions.Any() == false) return;
isExecCommanding = true;
if (isFlushCommanding) return;
if (_prevCommands.Any() == false) return;
isFlushCommanding = true;
ExecCommandInfo oldinfo = null;
PrevCommandInfo oldinfo = null;
var states = new List<object>();
Func<string, Task<int>> dbContextBatch = methodName =>
Task<int> dbsetBatch(string method)
{
if (_dicExecCommandDbContextBatchAsync.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 tryfunc = _dicFlushCommandDbSetBatchAsync
.GetOrAdd(oldinfo.stateType, stateType => new ConcurrentDictionary<string, Func<object, object[], Task<int>>>())
.GetOrAdd(method, methodName =>
{
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(global::FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeMethod, var1Vals)),
Expression.Label(returnTarget, Expression.Default(typeof(Task<int>)))
), new[] { parm1DbSet, parm2Vals }).Compile();
trydic.Add(methodName, tryfunc);
}
var returnTarget = Expression.Label(typeof(Task<int>));
var parm1DbSet = Expression.Parameter(typeof(object));
var parm2Vals = Expression.Parameter(typeof(object[]));
var var1Vals = Expression.Variable(arrType);
return Expression.Lambda<Func<object, object[], Task<int>>>(Expression.Block(
new[] { var1Vals },
Expression.Assign(var1Vals, Expression.Convert(global::FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeMethod, var1Vals)),
Expression.Label(returnTarget, Expression.Default(typeof(Task<int>)))
), new[] { parm1DbSet, parm2Vals }).Compile();
});
return tryfunc(oldinfo.dbSet, states.ToArray());
};
Func<Task> funcDelete = async () =>
}
async Task funcDelete()
{
_affrows += await dbContextBatch("DbContextBatchRemoveAsync");
_affrows += await dbsetBatch("DbContextBatchRemoveAsync");
states.Clear();
}
async Task funcInsert()
{
_affrows += await dbsetBatch("DbContextBatchAddAsync");
states.Clear();
};
Func<Task> funcInsert = async () =>
{
_affrows += await dbContextBatch("DbContextBatchAddAsync");
states.Clear();
};
Func<bool, Task> funcUpdate = async (isLiveUpdate) =>
async Task funcUpdate(bool isLiveUpdate)
{
var affrows = 0;
if (isLiveUpdate) affrows = await dbContextBatch("DbContextBatchUpdateNowAsync");
else affrows = await dbContextBatch("DbContextBatchUpdateAsync");
if (isLiveUpdate) affrows = await dbsetBatch("DbContextBatchUpdateNowAsync");
else affrows = await dbsetBatch("DbContextBatchUpdateAsync");
if (affrows == -999)
{ //最后一个元素已被删除
states.RemoveAt(states.Count - 1);
@ -87,13 +87,13 @@ namespace FreeSql
}
};
while (_actions.Any() || states.Any())
while (_prevCommands.Any() || states.Any())
{
var info = _actions.Any() ? _actions.Dequeue() : null;
var info = _prevCommands.Any() ? _prevCommands.Dequeue() : null;
if (oldinfo == null) oldinfo = info;
var isLiveUpdate = false;
if (_actions.Any() == false && states.Any() ||
if (_prevCommands.Any() == false && states.Any() ||
info != null && oldinfo.changeType != info.changeType ||
info != null && oldinfo.stateType != info.stateType ||
info != null && oldinfo.entityType != info.entityType)
@ -130,7 +130,7 @@ namespace FreeSql
oldinfo = info;
}
}
isExecCommanding = false;
isFlushCommanding = false;
}
}
}

View File

@ -35,14 +35,14 @@ namespace FreeSql
public ISelect<T1> Select<T1>() where T1 : class
{
_resolveDbContext()?.ExecCommand();
_resolveDbContext()?.FlushCommand();
return _originalFsql.Select<T1>().WithTransaction(_resolveUnitOfWork()?.GetOrBeginTransaction(false));
}
public ISelect<T1> Select<T1>(object dywhere) where T1 : class => Select<T1>().WhereDynamic(dywhere);
public IDelete<T1> Delete<T1>() where T1 : class
{
_resolveDbContext()?.ExecCommand();
_resolveDbContext()?.FlushCommand();
return _originalFsql.Delete<T1>().WithTransaction(_resolveUnitOfWork()?.GetOrBeginTransaction());
}
public IDelete<T1> Delete<T1>(object dywhere) where T1 : class => Delete<T1>().WhereDynamic(dywhere);
@ -50,7 +50,7 @@ namespace FreeSql
public IUpdate<T1> Update<T1>() where T1 : class
{
var db = _resolveDbContext();
db?.ExecCommand();
db?.FlushCommand();
var update = _originalFsql.Update<T1>().WithTransaction(_resolveUnitOfWork()?.GetOrBeginTransaction());
if (db?.Options.NoneParameter != null) update.NoneParameter(db.Options.NoneParameter.Value);
return update;
@ -60,7 +60,7 @@ namespace FreeSql
public IInsert<T1> Insert<T1>() where T1 : class
{
var db = _resolveDbContext();
db?.ExecCommand();
db?.FlushCommand();
var insert = _originalFsql.Insert<T1>().WithTransaction(_resolveUnitOfWork()?.GetOrBeginTransaction());
if (db?.Options.NoneParameter != null) insert.NoneParameter(db.Options.NoneParameter.Value);
return insert;

View File

@ -1,8 +1,9 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
using System.Reflection;
namespace FreeSql
{
@ -26,60 +27,62 @@ namespace FreeSql
}
public virtual int SaveChanges()
{
ExecCommand();
FlushCommand();
return SaveChangesSuccess();
}
static Dictionary<Type, Dictionary<string, Func<object, object[], int>>> _dicExecCommandDbContextBatch = new Dictionary<Type, Dictionary<string, Func<object, object[], int>>>();
bool isExecCommanding = false;
internal void ExecCommand()
static ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], int>>> _dicFlushCommandDbSetBatch = new ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], int>>>();
bool isFlushCommanding = false;
/// <summary>
/// 刷新队列中的命令
/// </summary>
internal void FlushCommand()
{
if (isExecCommanding) return;
if (_actions.Any() == false) return;
isExecCommanding = true;
if (isFlushCommanding) return;
if (_prevCommands.Any() == false) return;
isFlushCommanding = true;
ExecCommandInfo oldinfo = null;
PrevCommandInfo oldinfo = null;
var states = new List<object>();
Func<string, int> dbContextBatch = methodName =>
int dbsetBatch(string method)
{
if (_dicExecCommandDbContextBatch.TryGetValue(oldinfo.stateType, out var trydic) == false)
trydic = new Dictionary<string, Func<object, object[], 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 tryfunc = _dicFlushCommandDbSetBatch
.GetOrAdd(oldinfo.stateType, stateType => new ConcurrentDictionary<string, Func<object, object[], int>>())
.GetOrAdd(method, methodName =>
{
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(int));
var parm1DbSet = Expression.Parameter(typeof(object));
var parm2Vals = Expression.Parameter(typeof(object[]));
var var1Vals = Expression.Variable(arrType);
tryfunc = Expression.Lambda<Func<object, object[], int>>(Expression.Block(
new[] { var1Vals },
Expression.Assign(var1Vals, Expression.Convert(global::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);
}
var returnTarget = Expression.Label(typeof(int));
var parm1DbSet = Expression.Parameter(typeof(object));
var parm2Vals = Expression.Parameter(typeof(object[]));
var var1Vals = Expression.Variable(arrType);
return Expression.Lambda<Func<object, object[], int>>(Expression.Block(
new[] { var1Vals },
Expression.Assign(var1Vals, Expression.Convert(global::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();
});
return tryfunc(oldinfo.dbSet, states.ToArray());
};
Action funcDelete = () =>
}
void funcDelete()
{
_affrows += dbContextBatch("DbContextBatchRemove");
_affrows += dbsetBatch("DbContextBatchRemove");
states.Clear();
}
void funcInsert()
{
_affrows += dbsetBatch("DbContextBatchAdd");
states.Clear();
};
Action funcInsert = () =>
{
_affrows += dbContextBatch("DbContextBatchAdd");
states.Clear();
};
Action<bool> funcUpdate = isLiveUpdate =>
void funcUpdate(bool isLiveUpdate)
{
var affrows = 0;
if (isLiveUpdate) affrows = dbContextBatch("DbContextBatchUpdateNow");
else affrows = dbContextBatch("DbContextBatchUpdate");
if (isLiveUpdate) affrows = dbsetBatch("DbContextBatchUpdateNow");
else affrows = dbsetBatch("DbContextBatchUpdate");
if (affrows == -999)
{ //最后一个元素已被删除
states.RemoveAt(states.Count - 1);
@ -101,13 +104,13 @@ namespace FreeSql
}
};
while (_actions.Any() || states.Any())
while (_prevCommands.Any() || states.Any())
{
var info = _actions.Any() ? _actions.Dequeue() : null;
var info = _prevCommands.Any() ? _prevCommands.Dequeue() : null;
if (oldinfo == null) oldinfo = info;
var isLiveUpdate = false;
if (_actions.Any() == false && states.Any() ||
if (_prevCommands.Any() == false && states.Any() ||
info != null && oldinfo.changeType != info.changeType ||
info != null && oldinfo.stateType != info.stateType ||
info != null && oldinfo.entityType != info.entityType)
@ -144,7 +147,7 @@ namespace FreeSql
oldinfo = info;
}
}
isExecCommanding = false;
isFlushCommanding = false;
}
}
}