mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 04:18:16 +08:00
移除 FreeSql.DbContext,成为独立项目
This commit is contained in:
@ -1,87 +0,0 @@
|
||||
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;
|
||||
|
||||
namespace FreeSql {
|
||||
public abstract partial class DbContext : IDisposable {
|
||||
|
||||
internal IFreeSql _orm;
|
||||
internal IFreeSql _fsql => _orm ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
|
||||
|
||||
IUnitOfWork _uowPriv;
|
||||
internal IUnitOfWork _uow => _isUseUnitOfWork ? (_uowPriv ?? (_uowPriv = new UnitOfWork(_fsql))) : null;
|
||||
internal bool _isUseUnitOfWork = true; //不使用工作单元事务
|
||||
|
||||
static ConcurrentDictionary<Type, PropertyInfo[]> _dicGetDbSetProps = new ConcurrentDictionary<Type, PropertyInfo[]>();
|
||||
protected DbContext() {
|
||||
|
||||
var builder = new DbContextOptionsBuilder();
|
||||
OnConfiguring(builder);
|
||||
_orm = builder._fsql;
|
||||
|
||||
var props = _dicGetDbSetProps.GetOrAdd(this.GetType(), tp =>
|
||||
tp.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
|
||||
.Where(a => a.PropertyType.IsGenericType &&
|
||||
a.PropertyType == typeof(DbSet<>).MakeGenericType(a.PropertyType.GenericTypeArguments[0])).ToArray());
|
||||
|
||||
foreach (var prop in props) {
|
||||
var set = this.Set(prop.PropertyType.GenericTypeArguments[0]);
|
||||
|
||||
prop.SetValue(this, set);
|
||||
AllSets.Add(prop.Name, set);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnConfiguring(DbContextOptionsBuilder builder) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected Dictionary<Type, object> _dicSet = new Dictionary<Type, object>();
|
||||
public DbSet<TEntity> Set<TEntity>() where TEntity : class => this.Set(typeof(TEntity)) as DbSet<TEntity>;
|
||||
public virtual object Set(Type entityType) {
|
||||
if (_dicSet.ContainsKey(entityType)) return _dicSet[entityType];
|
||||
var sd = Activator.CreateInstance(typeof(DbContextDbSet<>).MakeGenericType(entityType), this);
|
||||
_dicSet.Add(entityType, sd);
|
||||
return sd;
|
||||
}
|
||||
protected Dictionary<string, object> AllSets { get; } = new Dictionary<string, object>();
|
||||
|
||||
internal class ExecCommandInfo {
|
||||
public ExecCommandInfoType actionType { get; set; }
|
||||
public object dbSet { get; set; }
|
||||
public Type stateType { get; set; }
|
||||
public object state { get; set; }
|
||||
}
|
||||
internal enum ExecCommandInfoType { Insert, Update, Delete }
|
||||
Queue<ExecCommandInfo> _actions = new Queue<ExecCommandInfo>();
|
||||
internal int _affrows = 0;
|
||||
|
||||
internal void EnqueueAction(ExecCommandInfoType actionType, object dbSet, Type stateType, object state) {
|
||||
_actions.Enqueue(new ExecCommandInfo { actionType = actionType, dbSet = dbSet, stateType = stateType, state = state });
|
||||
}
|
||||
|
||||
~DbContext() {
|
||||
this.Dispose();
|
||||
}
|
||||
bool _isdisposed = false;
|
||||
public void Dispose() {
|
||||
if (_isdisposed) return;
|
||||
try {
|
||||
_actions.Clear();
|
||||
_dicSet.Clear();
|
||||
AllSets.Clear();
|
||||
|
||||
_uow?.Rollback();
|
||||
} finally {
|
||||
_isdisposed = true;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,118 +0,0 @@
|
||||
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<int> SaveChangesAsync() {
|
||||
await ExecCommandAsync();
|
||||
_uow?.Commit();
|
||||
var ret = _affrows;
|
||||
_affrows = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
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() {
|
||||
if (_actions.Any() == false) return;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Text;
|
||||
|
||||
namespace FreeSql {
|
||||
public class DbContextOptionsBuilder {
|
||||
|
||||
internal IFreeSql _fsql;
|
||||
|
||||
public DbContextOptionsBuilder UseFreeSql(IFreeSql orm) {
|
||||
_fsql = orm;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,117 +0,0 @@
|
||||
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;
|
||||
|
||||
namespace FreeSql {
|
||||
partial class DbContext {
|
||||
|
||||
public int SaveChanges() {
|
||||
ExecCommand();
|
||||
_uow?.Commit();
|
||||
var ret = _affrows;
|
||||
_affrows = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Dictionary<Type, Dictionary<string, Func<object, object[], int>>> _dicExecCommandDbContextBetch = new Dictionary<Type, Dictionary<string, Func<object, object[], int>>>();
|
||||
internal void ExecCommand() {
|
||||
if (_actions.Any() == false) return;
|
||||
ExecCommandInfo oldinfo = null;
|
||||
var states = new List<object>();
|
||||
|
||||
Func<string, int> dbContextBetch = methodName => {
|
||||
if (_dicExecCommandDbContextBetch.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 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(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());
|
||||
};
|
||||
Action funcDelete = () => {
|
||||
_affrows += dbContextBetch("DbContextBetchRemove");
|
||||
states.Clear();
|
||||
};
|
||||
Action funcInsert = () => {
|
||||
_affrows += dbContextBetch("DbContextBetchAdd");
|
||||
states.Clear();
|
||||
};
|
||||
Action<bool> funcUpdate = isLiveUpdate => {
|
||||
var affrows = 0;
|
||||
if (isLiveUpdate) affrows = dbContextBetch("DbContextBetchUpdateNow");
|
||||
else affrows = dbContextBetch("DbContextBetchUpdate");
|
||||
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:
|
||||
funcInsert();
|
||||
break;
|
||||
case ExecCommandInfoType.Delete:
|
||||
funcDelete();
|
||||
break;
|
||||
}
|
||||
isLiveUpdate = true;
|
||||
}
|
||||
|
||||
if (isLiveUpdate || oldinfo.actionType == ExecCommandInfoType.Update) {
|
||||
if (states.Any())
|
||||
funcUpdate(isLiveUpdate);
|
||||
}
|
||||
|
||||
if (info != null) {
|
||||
states.Add(info.state);
|
||||
oldinfo = info;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user