mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 04:18:16 +08:00
- 优化 DbContext/Repository Orm 属性进行 CURD 与自身事务相同【新突破】;#270
This commit is contained in:
@ -10,13 +10,9 @@ namespace FreeSql
|
||||
{
|
||||
public abstract partial class DbContext : IDisposable
|
||||
{
|
||||
internal IFreeSql _ormPriv;
|
||||
|
||||
/// <summary>
|
||||
/// 注意:IFreeSql 属于顶级对象,事务无法自动传递。<para></para>
|
||||
/// 手工传递事务:ISelect/IInsert/IDelete/IUpdate 可以使用 WithTransaction(uow.GetOrBeginTransaction())
|
||||
/// </summary>
|
||||
public IFreeSql Orm => _ormPriv ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
|
||||
internal DbContextScopedFreeSql _ormScoped;
|
||||
internal IFreeSql OrmOriginal => _ormScoped?._originalFsql ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
|
||||
public IFreeSql Orm => _ormScoped ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
|
||||
|
||||
#region Property UnitOfWork
|
||||
internal bool _isUseUnitOfWork = true; //是否创建工作单元事务
|
||||
@ -28,7 +24,7 @@ namespace FreeSql
|
||||
{
|
||||
if (_uowPriv != null) return _uowPriv;
|
||||
if (_isUseUnitOfWork == false) return null;
|
||||
return _uowPriv = new UnitOfWork(Orm);
|
||||
return _uowPriv = new UnitOfWork(OrmOriginal);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
@ -43,7 +39,7 @@ namespace FreeSql
|
||||
if (_optionsPriv == null)
|
||||
{
|
||||
_optionsPriv = new DbContextOptions();
|
||||
if (FreeSqlDbContextExtensions._dicSetDbContextOptions.TryGetValue(Orm.Ado.Identifier, out var opt))
|
||||
if (FreeSqlDbContextExtensions._dicSetDbContextOptions.TryGetValue(OrmOriginal.Ado.Identifier, out var opt))
|
||||
{
|
||||
_optionsPriv.EnableAddOrUpdateNavigateList = opt.EnableAddOrUpdateNavigateList;
|
||||
_optionsPriv.OnEntityChange = opt.OnEntityChange;
|
||||
@ -63,17 +59,17 @@ namespace FreeSql
|
||||
protected DbContext() : this(null, null) { }
|
||||
protected DbContext(IFreeSql fsql, DbContextOptions options)
|
||||
{
|
||||
_ormPriv = fsql;
|
||||
_ormScoped = DbContextScopedFreeSql.Create(fsql, () => this, () => UnitOfWork);
|
||||
_optionsPriv = options;
|
||||
|
||||
if (_ormPriv == null)
|
||||
if (_ormScoped == null)
|
||||
{
|
||||
var builder = new DbContextOptionsBuilder();
|
||||
OnConfiguring(builder);
|
||||
_ormPriv = builder._fsql;
|
||||
_ormScoped = DbContextScopedFreeSql.Create(builder._fsql, () => this, () => UnitOfWork);
|
||||
_optionsPriv = builder._options;
|
||||
}
|
||||
if (_ormPriv != null) InitPropSets();
|
||||
if (_ormScoped != null) InitPropSets();
|
||||
}
|
||||
protected virtual void OnConfiguring(DbContextOptionsBuilder builder) { }
|
||||
|
||||
@ -113,7 +109,7 @@ namespace FreeSql
|
||||
#region DbSet 快速代理
|
||||
void CheckEntityTypeOrThrow(Type entityType)
|
||||
{
|
||||
if (Orm.CodeFirst.GetTableByEntity(entityType) == null)
|
||||
if (OrmOriginal.CodeFirst.GetTableByEntity(entityType) == null)
|
||||
throw new ArgumentException($"参数 data 类型错误 {entityType.FullName} ");
|
||||
}
|
||||
/// <summary>
|
||||
|
74
FreeSql.DbContext/DbContext/DbContextScopedFreeSql.cs
Normal file
74
FreeSql.DbContext/DbContext/DbContextScopedFreeSql.cs
Normal file
@ -0,0 +1,74 @@
|
||||
using FreeSql;
|
||||
using FreeSql.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
|
||||
namespace FreeSql
|
||||
{
|
||||
class DbContextScopedFreeSql : IFreeSql
|
||||
{
|
||||
public IFreeSql _originalFsql;
|
||||
Func<DbContext> _resolveDbContext;
|
||||
Func<IUnitOfWork> _resolveUnitOfWork;
|
||||
DbContextScopedFreeSql() { }
|
||||
|
||||
public static DbContextScopedFreeSql Create(IFreeSql fsql, Func<DbContext> resolveDbContext, Func<IUnitOfWork> resolveUnitOfWork)
|
||||
{
|
||||
if (fsql == null) return null;
|
||||
var scopedfsql = fsql as DbContextScopedFreeSql;
|
||||
if (scopedfsql == null) return new DbContextScopedFreeSql { _originalFsql = fsql, _resolveDbContext = resolveDbContext, _resolveUnitOfWork = resolveUnitOfWork };
|
||||
return Create(scopedfsql._originalFsql, resolveDbContext, resolveUnitOfWork);
|
||||
}
|
||||
|
||||
public IAdo Ado => _originalFsql.Ado;
|
||||
public IAop Aop => _originalFsql.Aop;
|
||||
public ICodeFirst CodeFirst => _originalFsql.CodeFirst;
|
||||
public IDbFirst DbFirst => _originalFsql.DbFirst;
|
||||
public GlobalFilter GlobalFilter => _originalFsql.GlobalFilter;
|
||||
public void Dispose() { }
|
||||
|
||||
public void Transaction(Action handler) => _originalFsql.Transaction(handler);
|
||||
public void Transaction(TimeSpan timeout, Action handler) => _originalFsql.Transaction(timeout, handler);
|
||||
public void Transaction(IsolationLevel isolationLevel, TimeSpan timeout, Action handler) => _originalFsql.Transaction(isolationLevel, timeout, handler);
|
||||
|
||||
public ISelect<T1> Select<T1>() where T1 : class
|
||||
{
|
||||
_resolveDbContext()?.ExecCommand();
|
||||
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();
|
||||
return _originalFsql.Delete<T1>().WithTransaction(_resolveUnitOfWork()?.GetOrBeginTransaction());
|
||||
}
|
||||
public IDelete<T1> Delete<T1>(object dywhere) where T1 : class => Delete<T1>().WhereDynamic(dywhere);
|
||||
|
||||
public IUpdate<T1> Update<T1>() where T1 : class
|
||||
{
|
||||
var db = _resolveDbContext();
|
||||
db?.ExecCommand();
|
||||
var update = _originalFsql.Update<T1>().WithTransaction(_resolveUnitOfWork()?.GetOrBeginTransaction());
|
||||
if (db?.Options.NoneParameter != null) update.NoneParameter(db.Options.NoneParameter.Value);
|
||||
return update;
|
||||
}
|
||||
public IUpdate<T1> Update<T1>(object dywhere) where T1 : class => Update<T1>().WhereDynamic(dywhere);
|
||||
|
||||
public IInsert<T1> Insert<T1>() where T1 : class
|
||||
{
|
||||
var db = _resolveDbContext();
|
||||
db?.ExecCommand();
|
||||
var insert = _originalFsql.Insert<T1>().WithTransaction(_resolveUnitOfWork()?.GetOrBeginTransaction());
|
||||
if (db?.Options.NoneParameter != null) insert.NoneParameter(db.Options.NoneParameter.Value);
|
||||
return insert;
|
||||
}
|
||||
public IInsert<T1> Insert<T1>(T1 source) where T1 : class => Insert<T1>().AppendData(source);
|
||||
public IInsert<T1> Insert<T1>(T1[] source) where T1 : class => Insert<T1>().AppendData(source);
|
||||
public IInsert<T1> Insert<T1>(List<T1> source) where T1 : class => Insert<T1>().AppendData(source);
|
||||
public IInsert<T1> Insert<T1>(IEnumerable<T1> source) where T1 : class => Insert<T1>().AppendData(source);
|
||||
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ namespace FreeSql
|
||||
|
||||
public FreeContext(IFreeSql orm)
|
||||
{
|
||||
_ormPriv = orm;
|
||||
_ormScoped = DbContextScopedFreeSql.Create(orm, () => this, () => UnitOfWork);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user