- 调整 BaseEntity,移除 BaseTreeEntity、Tenant 租户,改变事务习惯;

This commit is contained in:
28810
2020-05-08 14:49:24 +08:00
parent fc828d15a6
commit 6d5575def1
12 changed files with 128 additions and 605 deletions

View File

@ -1,11 +1,10 @@
#if netcore

using FreeSql.DataAnnotations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Data;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
@ -27,6 +26,7 @@ namespace FreeSql
.UseAutoSyncStructure(true)
.UseConnectionString(DataType.Sqlite, ""data source=test.db;max pool size=5"")
.Build());");
internal static Func<IUnitOfWork> _resolveUow;
/// <summary>
/// 初始化BaseEntity
@ -39,7 +39,8 @@ namespace FreeSql
/// .Build());
/// </summary>
/// <param name="fsql">IFreeSql orm 对象</param>
public static void Initialization(IFreeSql fsql)
/// <param name="resolveUow">工作单元(事务)委托,如果不使用事务请传 null<para></para>解释由于AsyncLocal平台兼容不好所以交给外部管理</param>
public static void Initialization(IFreeSql fsql, Func<IUnitOfWork> resolveUow)
{
_ormPriv = fsql;
_ormPriv.Aop.CurdBefore += (s, e) => Trace.WriteLine($"\r\n线程{Thread.CurrentThread.ManagedThreadId}: {e.Sql}\r\n");
@ -51,6 +52,7 @@ namespace FreeSql
_ormPriv.CodeFirst.ConfigEntity(cei.EntityType, cei.Fluent);
}
}
_resolveUow = resolveUow;
}
class ConfigEntityInfo
@ -75,72 +77,25 @@ namespace FreeSql
/// 创建时间
/// </summary>
[Column(Position = -4)]
public DateTime CreateTime { get; set; } = DateTime.Now;
public virtual DateTime CreateTime { get; set; } = DateTime.Now;
/// <summary>
/// 更新时间
/// </summary>
[Column(Position = -3)]
public DateTime UpdateTime { get; set; }
public virtual DateTime UpdateTime { get; set; }
/// <summary>
/// 逻辑删除
/// </summary>
[Column(Position = -2)]
public bool IsDeleted { get; set; }
public virtual bool IsDeleted { get; set; }
/// <summary>
/// 排序
/// </summary>
[Column(Position = -1)]
public int Sort { get; set; }
/// <summary>
/// 开启工作单元事务
/// </summary>
/// <returns></returns>
public static IUnitOfWork Begin() => Begin(null);
/// <summary>
/// 开启工作单元事务
/// </summary>
/// <param name="level">事务等级</param>
/// <returns></returns>
public static IUnitOfWork Begin(IsolationLevel? level)
{
var uow = Orm.CreateUnitOfWork();
uow.IsolationLevel = level;
CurrentUnitOfWork = uow;
return uow;
}
static readonly AsyncLocal<IUnitOfWork> _AsyncUnitOfWork = new AsyncLocal<IUnitOfWork>();
static readonly AsyncLocal<string> _AsyncTenantId = new AsyncLocal<string>();
/// <summary>
/// 获取或设置当前租户id
/// </summary>
public static string CurrentTenantId
{
get => _AsyncTenantId.Value;
set => _AsyncTenantId.Value = value;
}
/// <summary>
/// 获取或设置当前租户id
/// </summary>
public static IUnitOfWork CurrentUnitOfWork
{
get => _AsyncUnitOfWork.Value;
set => _AsyncUnitOfWork.Value = value;
}
}
/// <summary>
/// 租户
/// </summary>
public interface ITenant
{
/// <summary>
/// 租户id
/// </summary>
string TenantId { get; set; }
public virtual int Sort { get; set; }
}
[Table(DisableSyncStructure = true)]
public abstract class BaseEntityReadOnly<TEntity> : BaseEntity where TEntity : class
{
/// <summary>
@ -153,9 +108,7 @@ namespace FreeSql
{
var select = Orm.Select<TEntity>()
.TrackToList(TrackToList) //自动为每个元素 Attach
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction(false));
if (string.IsNullOrEmpty(CurrentTenantId) == false)
select.WhereCascade(a => (a as ITenant).TenantId == CurrentTenantId);
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction(false));
return select.WhereCascade(a => (a as BaseEntity).IsDeleted == false);
}
}
@ -169,6 +122,7 @@ namespace FreeSql
var ie = list as IEnumerable;
if (ie == null) return;
var isFirst = true;
IBaseRepository<TEntity> berepo = null;
foreach (var item in ie)
{
if (item == null) return;
@ -181,27 +135,29 @@ namespace FreeSql
if (Orm.CodeFirst.GetTableByEntity(itemType)?.Primarys.Any() != true) return;
if (item is BaseEntity<TEntity> == false) return;
}
(item as BaseEntity<TEntity>)?.Attach();
var beitem = item as BaseEntity<TEntity>;
if (beitem != null)
{
if (berepo == null) berepo = Orm.GetRepository<TEntity>();
beitem.Repository = berepo;
beitem.Attach();
}
}
return;
}
if (ls.Any() == false) return;
if (ls.FirstOrDefault() is BaseEntity<TEntity> == false) return;
if (Orm.CodeFirst.GetTableByEntity(typeof(TEntity))?.Primarys.Any() != true) return;
IBaseRepository<TEntity> repo = null;
foreach (var item in ls)
(item as BaseEntity<TEntity>)?.Attach();
}
/// <summary>
/// 设置当前租户id
/// </summary>
protected void SetTenantId()
{
if (string.IsNullOrEmpty(CurrentTenantId) == false)
{
var ten = this as ITenant;
if (ten != null)
ten.TenantId = CurrentTenantId;
var beitem = item as BaseEntity<TEntity>;
if (beitem != null)
{
if (repo == null) repo = Orm.GetRepository<TEntity>();
beitem.Repository = repo;
beitem.Attach();
}
}
}
@ -233,11 +189,8 @@ namespace FreeSql
this.Repository = Orm.GetRepository<TEntity>();
var item = this as TEntity;
this.SetTenantId();
this.Repository.Attach(item);
return item;
}
}
}
#endif