mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
v0.8.5
This commit is contained in:
@ -7,65 +7,129 @@ using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
/// <summary>
|
||||
/// 包括 CreateTime/UpdateTime/IsDeleted 的实体基类
|
||||
/// </summary>
|
||||
[Table(DisableSyncStructure = true)]
|
||||
public abstract class BaseEntity
|
||||
namespace FreeSql
|
||||
{
|
||||
static IFreeSql _ormPriv;
|
||||
/// <summary>
|
||||
/// 全局 IFreeSql orm 对象
|
||||
/// 包括 CreateTime/UpdateTime/IsDeleted、CRUD 方法、以及 ID 主键定义 的实体基类
|
||||
/// <para></para>
|
||||
/// 当 TKey 为 int/long 时,Id 主键被设为自增值主键
|
||||
/// </summary>
|
||||
public static IFreeSql Orm => _ormPriv ?? throw new Exception(@"使用前请初始化 BaseEntity.Initialization(new FreeSqlBuilder()
|
||||
.UseAutoSyncStructure(true)
|
||||
.UseConnectionString(DataType.Sqlite, ""data source=test.db;max pool size=5"")
|
||||
.Build());");
|
||||
|
||||
/// <summary>
|
||||
/// 初始化BaseEntity
|
||||
/// BaseEntity.Initialization(new FreeSqlBuilder()
|
||||
/// <para></para>
|
||||
/// .UseAutoSyncStructure(true)
|
||||
/// <para></para>
|
||||
/// .UseConnectionString(DataType.Sqlite, "data source=test.db;max pool size=5")
|
||||
/// <para></para>
|
||||
/// .Build());
|
||||
/// </summary>
|
||||
/// <param name="fsql">IFreeSql orm 对象</param>
|
||||
public static void Initialization(IFreeSql fsql)
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
[Table(DisableSyncStructure = true)]
|
||||
public abstract class BaseEntity<TEntity, TKey> : BaseEntity<TEntity> where TEntity : class
|
||||
{
|
||||
_ormPriv = fsql;
|
||||
_ormPriv.Aop.CurdBefore += (s, e) => Trace.WriteLine(e.Sql + "\r\n");
|
||||
static BaseEntity()
|
||||
{
|
||||
var tkeyType = typeof(TKey)?.NullableTypeOrThis();
|
||||
if (tkeyType == typeof(int) || tkeyType == typeof(long))
|
||||
Orm.CodeFirst.ConfigEntity(typeof(TEntity),
|
||||
t => t.Property("Id").IsIdentity(true));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主键
|
||||
/// </summary>
|
||||
public virtual TKey Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键值获取数据
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
async public static Task<TEntity> FindAsync(TKey id)
|
||||
{
|
||||
var item = await Select.WhereDynamic(id).FirstAsync();
|
||||
(item as BaseEntity<TEntity>)?.Attach();
|
||||
return item;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键值获取数据
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public static TEntity Find(TKey id)
|
||||
{
|
||||
var item = Select.WhereDynamic(id).First();
|
||||
(item as BaseEntity<TEntity>)?.Attach();
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// 包括 CreateTime/UpdateTime/IsDeleted、以及 CRUD 异步和同步方法的实体基类
|
||||
/// </summary>
|
||||
public DateTime CreateTime { get; set; }
|
||||
/// <summary>
|
||||
/// 更新时间
|
||||
/// </summary>
|
||||
public DateTime UpdateTime { get; set; }
|
||||
/// <summary>
|
||||
/// 逻辑删除
|
||||
/// </summary>
|
||||
public bool IsDeleted { 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)
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
[Table(DisableSyncStructure = true)]
|
||||
public abstract class BaseEntity<TEntity> : BaseEntityAsync<TEntity> where TEntity : class
|
||||
{
|
||||
var uow = Orm.CreateUnitOfWork();
|
||||
uow.IsolationLevel = level;
|
||||
return uow;
|
||||
bool UpdateIsDeleted(bool value)
|
||||
{
|
||||
if (this.Repository == null)
|
||||
return Orm.Update<TEntity>(this as TEntity)
|
||||
.WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction())
|
||||
.Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrows() == 1;
|
||||
|
||||
this.SetTenantId();
|
||||
this.IsDeleted = value;
|
||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
||||
return this.Repository.Update(this as TEntity) == 1;
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool Delete() => this.UpdateIsDeleted(true);
|
||||
/// <summary>
|
||||
/// 恢复删除的数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool Restore() => this.UpdateIsDeleted(false);
|
||||
|
||||
/// <summary>
|
||||
/// 更新数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool Update()
|
||||
{
|
||||
this.UpdateTime = DateTime.Now;
|
||||
if (this.Repository == null)
|
||||
return Orm.Update<TEntity>()
|
||||
.WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction())
|
||||
.SetSource(this as TEntity).ExecuteAffrows() == 1;
|
||||
|
||||
this.SetTenantId();
|
||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
||||
return this.Repository.Update(this as TEntity) == 1;
|
||||
}
|
||||
/// <summary>
|
||||
/// 插入数据
|
||||
/// </summary>
|
||||
public virtual TEntity Insert()
|
||||
{
|
||||
this.CreateTime = DateTime.Now;
|
||||
if (this.Repository == null)
|
||||
this.Repository = Orm.GetRepository<TEntity>();
|
||||
|
||||
this.SetTenantId();
|
||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
||||
return this.Repository.Insert(this as TEntity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新或插入
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual TEntity Save()
|
||||
{
|
||||
this.UpdateTime = DateTime.Now;
|
||||
if (this.Repository == null)
|
||||
this.Repository = Orm.GetRepository<TEntity>();
|
||||
|
||||
this.SetTenantId();
|
||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
||||
return this.Repository.InsertOrUpdate(this as TEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user