mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
FreeSql.DbContext 融合 Repository + UnitOfWork
This commit is contained in:
@ -7,29 +7,41 @@ using System.Linq.Expressions;
|
||||
|
||||
namespace FreeSql {
|
||||
|
||||
internal class BaseDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
|
||||
internal class DbContextDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
|
||||
|
||||
public BaseDbSet(DbContext ctx) {
|
||||
if (ctx != null) {
|
||||
_ctx = ctx;
|
||||
_uow = ctx._uow;
|
||||
_fsql = ctx._fsql;
|
||||
}
|
||||
public DbContextDbSet(DbContext ctx) {
|
||||
_ctx = ctx;
|
||||
_uow = ctx._uow;
|
||||
_fsql = ctx._fsql;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract partial class DbSet<TEntity> where TEntity : class {
|
||||
public abstract partial class DbSet<TEntity> : IDisposable where TEntity : class {
|
||||
|
||||
internal DbContext _ctx;
|
||||
internal UnitOfWork _uow;
|
||||
internal IUnitOfWork _uow;
|
||||
internal IFreeSql _fsql;
|
||||
bool IsNoneDbContext => _ctx == null;
|
||||
|
||||
protected virtual ISelect<TEntity> OrmSelect(object dywhere) {
|
||||
DbContextExecCommand(); //查询前先提交,否则会出脏读
|
||||
return _fsql.Select<TEntity>(dywhere).WithTransaction(_uow?.GetOrBeginTransaction(false)).TrackToList(TrackToList);
|
||||
}
|
||||
|
||||
~DbSet() {
|
||||
this.Dispose();
|
||||
}
|
||||
bool _isdisposed = false;
|
||||
public void Dispose() {
|
||||
if (_isdisposed) return;
|
||||
try {
|
||||
this._dicUpdateTimes.Clear();
|
||||
this._states.Clear();
|
||||
} finally {
|
||||
_isdisposed = true;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual IInsert<TEntity> OrmInsert() => _fsql.Insert<TEntity>().WithTransaction(_uow?.GetOrBeginTransaction());
|
||||
protected virtual IInsert<TEntity> OrmInsert(TEntity data) => _fsql.Insert<TEntity>(data).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||
protected virtual IInsert<TEntity> OrmInsert(IEnumerable<TEntity> data) => _fsql.Insert<TEntity>(data).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||
@ -38,12 +50,10 @@ namespace FreeSql {
|
||||
protected virtual IDelete<TEntity> OrmDelete(object dywhere) => _fsql.Delete<TEntity>(dywhere).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||
|
||||
internal void EnqueueToDbContext(DbContext.ExecCommandInfoType actionType, EntityState state) {
|
||||
if (IsNoneDbContext == false)
|
||||
_ctx.EnqueueAction(actionType, this, typeof(EntityState), state);
|
||||
_ctx.EnqueueAction(actionType, this, typeof(EntityState), state);
|
||||
}
|
||||
internal void IncrAffrows(long affrows) {
|
||||
if (IsNoneDbContext == false)
|
||||
_ctx._affrows += affrows;
|
||||
internal void IncrAffrows(int affrows) {
|
||||
_ctx._affrows += affrows;
|
||||
}
|
||||
internal void TrackToList(object list) {
|
||||
if (list == null) return;
|
||||
|
@ -8,11 +8,8 @@ namespace FreeSql {
|
||||
partial class DbSet<TEntity> {
|
||||
|
||||
Task DbContextExecCommandAsync() {
|
||||
if (IsNoneDbContext == false) {
|
||||
_dicUpdateTimes.Clear();
|
||||
return _ctx.ExecCommandAsync();
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
_dicUpdateTimes.Clear();
|
||||
return _ctx.ExecCommandAsync();
|
||||
}
|
||||
|
||||
async Task<int> DbContextBetchAddAsync(EntityState[] adds) {
|
||||
@ -59,10 +56,7 @@ namespace FreeSql {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (IsNoneDbContext)
|
||||
IncrAffrows(await OrmInsert(data).ExecuteAffrowsAsync());
|
||||
else
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(data));
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(data));
|
||||
}
|
||||
}
|
||||
public Task AddAsync(TEntity data) => AddPrivAsync(data, true);
|
||||
@ -94,12 +88,9 @@ namespace FreeSql {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (IsNoneDbContext)
|
||||
IncrAffrows(await OrmInsert(data).ExecuteAffrowsAsync());
|
||||
else
|
||||
//进入队列,等待 SaveChanges 时执行
|
||||
foreach (var s in data)
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(s));
|
||||
//进入队列,等待 SaveChanges 时执行
|
||||
foreach (var s in data)
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(s));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
@ -154,56 +145,6 @@ namespace FreeSql {
|
||||
//等待下次对比再保存
|
||||
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
|
||||
@ -212,28 +153,6 @@ namespace FreeSql {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,8 @@ namespace FreeSql {
|
||||
partial class DbSet<TEntity> {
|
||||
|
||||
void DbContextExecCommand() {
|
||||
if (IsNoneDbContext == false) {
|
||||
_dicUpdateTimes.Clear();
|
||||
_ctx.ExecCommand();
|
||||
}
|
||||
_dicUpdateTimes.Clear();
|
||||
_ctx.ExecCommand();
|
||||
}
|
||||
|
||||
int DbContextBetchAdd(EntityState[] adds) {
|
||||
@ -56,12 +54,8 @@ namespace FreeSql {
|
||||
}
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (IsNoneDbContext)
|
||||
IncrAffrows(OrmInsert(data).ExecuteAffrows());
|
||||
else
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(data));
|
||||
}
|
||||
} else
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(data));
|
||||
}
|
||||
public void Add(TEntity data) => AddPriv(data, true);
|
||||
public void AddRange(IEnumerable<TEntity> data) {
|
||||
@ -92,12 +86,9 @@ namespace FreeSql {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (IsNoneDbContext)
|
||||
IncrAffrows(OrmInsert(data).ExecuteAffrows());
|
||||
else
|
||||
//进入队列,等待 SaveChanges 时执行
|
||||
foreach (var s in data)
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(s));
|
||||
//进入队列,等待 SaveChanges 时执行
|
||||
foreach (var s in data)
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(s));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
@ -154,42 +145,9 @@ namespace FreeSql {
|
||||
}
|
||||
|
||||
Dictionary<TEntity, byte> _dicUpdateTimes = new Dictionary<TEntity, byte>();
|
||||
internal int UpdateAffrows(TEntity data) => UpdateRangeAffrows(new[] { data });
|
||||
internal int UpdateRangeAffrows(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 = 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;
|
||||
}
|
||||
public void Update(TEntity data) => UpdateRange(new[] { data });
|
||||
public void UpdateRange(IEnumerable<TEntity> data) {
|
||||
if (CanUpdate(data, true) == false) return;
|
||||
foreach (var item in data) {
|
||||
if (_dicUpdateTimes.ContainsKey(item))
|
||||
DbContextExecCommand();
|
||||
@ -199,10 +157,7 @@ namespace FreeSql {
|
||||
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
|
||||
@ -212,27 +167,17 @@ namespace FreeSql {
|
||||
return Math.Max(dels.Length, affrows);
|
||||
}
|
||||
|
||||
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>();
|
||||
public void Remove(TEntity data) => RemoveRange(new[] { data });
|
||||
public void RemoveRange(IEnumerable<TEntity> data) {
|
||||
if (CanRemove(data, true) == false) return;
|
||||
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 = DbContextBetchRemove(dels.ToArray());
|
||||
IncrAffrows(affrows);
|
||||
return affrows;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
public void Remove(TEntity data) => RemoveAffrows(data);
|
||||
public void RemoveRange(IEnumerable<TEntity> data) => RemoveRangeAffrows(data);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user