mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
BaseEntity + 异步事务测试
This commit is contained in:
@ -2,8 +2,10 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
[Table(DisableSyncStructure = true)]
|
||||
public abstract class BaseEntity
|
||||
@ -13,7 +15,7 @@ public abstract class BaseEntity
|
||||
var orm = new FreeSqlBuilder()
|
||||
.UseAutoSyncStructure(true)
|
||||
.UseNoneCommandParameter(true)
|
||||
.UseConnectionString(DataType.Sqlite, "data source=test.db;max pool size=2")
|
||||
.UseConnectionString(DataType.Sqlite, "data source=test.db;max pool size=5")
|
||||
//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=2")
|
||||
//.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2")
|
||||
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=2")
|
||||
@ -36,71 +38,108 @@ public abstract class BaseEntity
|
||||
/// 逻辑删除
|
||||
/// </summary>
|
||||
public bool IsDeleted { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 开启工作单元事务
|
||||
/// </summary>
|
||||
/// <param name="level"></param>
|
||||
/// <returns></returns>
|
||||
public static IUnitOfWork Begin() => Begin(null);
|
||||
public static IUnitOfWork Begin(IsolationLevel? level)
|
||||
{
|
||||
var uow = new BaseEntityUnitOfWork(Orm);
|
||||
uow.IsolationLevel = level;
|
||||
return uow;
|
||||
}
|
||||
protected static IUnitOfWork CurrentUow => BaseEntityUnitOfWork._asyncUow.Value;
|
||||
}
|
||||
|
||||
[Table(DisableSyncStructure = true)]
|
||||
public abstract class BaseEntity<TEntity> : BaseEntity where TEntity : class
|
||||
{
|
||||
public static ISelect<TEntity> Select => Orm.Select<TEntity>().WhereCascade(a => (a as BaseEntity<TEntity>).IsDeleted == false);
|
||||
public static ISelect<TEntity> Select => Orm.Select<TEntity>()
|
||||
.WithTransaction(CurrentUow?.GetOrBeginTransaction(false))
|
||||
.WhereCascade(a => (a as BaseEntity<TEntity>).IsDeleted == false);
|
||||
public static ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => Select.Where(exp);
|
||||
public static ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => Select.WhereIf(condition, exp);
|
||||
|
||||
[JsonIgnore]
|
||||
protected IBaseRepository<TEntity> Repository { get; set; }
|
||||
|
||||
bool UpdateIsDeleted(bool value)
|
||||
async Task<bool> UpdateIsDeleted(bool value)
|
||||
{
|
||||
if (this.Repository == null)
|
||||
return Orm.Update<TEntity>(this as TEntity).Set(a => (a as BaseEntity<TEntity>).IsDeleted, this.IsDeleted = value).ExecuteAffrows() == 1;
|
||||
return await Orm.Update<TEntity>(this as TEntity)
|
||||
.WithTransaction(CurrentUow?.GetOrBeginTransaction())
|
||||
.Set(a => (a as BaseEntity<TEntity>).IsDeleted, this.IsDeleted = value).ExecuteAffrowsAsync() == 1;
|
||||
|
||||
this.IsDeleted = value;
|
||||
return this.Repository.Update(this as TEntity) == 1;
|
||||
this.Repository.UnitOfWork = CurrentUow;
|
||||
return await this.Repository.UpdateAsync(this as TEntity) == 1;
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool Delete() => this.UpdateIsDeleted(true);
|
||||
public virtual Task<bool> Delete() => this.UpdateIsDeleted(true);
|
||||
/// <summary>
|
||||
/// 恢复删除的数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool Restore() => this.UpdateIsDeleted(false);
|
||||
public virtual Task<bool> Restore() => this.UpdateIsDeleted(false);
|
||||
|
||||
/// <summary>
|
||||
/// 附加实体,在更新数据时,只更新变化的部分
|
||||
/// </summary>
|
||||
public void Attach()
|
||||
public TEntity Attach()
|
||||
{
|
||||
if (this.Repository == null) this.Repository = Orm.GetRepository<TEntity>();
|
||||
this.Repository.Attach(this as TEntity);
|
||||
if (this.Repository == null)
|
||||
this.Repository = Orm.GetRepository<TEntity>();
|
||||
|
||||
var item = this as TEntity;
|
||||
this.Repository.Attach(item);
|
||||
return item;
|
||||
}
|
||||
/// <summary>
|
||||
/// 更新数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool Update()
|
||||
async public virtual Task<bool> Update()
|
||||
{
|
||||
this.UpdateTime = DateTime.Now;
|
||||
if (this.Repository == null)
|
||||
return Orm.Update<TEntity>().SetSource(this as TEntity).ExecuteAffrows() == 1;
|
||||
return this.Repository.Update(this as TEntity) == 1;
|
||||
return await Orm.Update<TEntity>()
|
||||
.WithTransaction(CurrentUow?.GetOrBeginTransaction())
|
||||
.SetSource(this as TEntity).ExecuteAffrowsAsync() == 1;
|
||||
|
||||
this.Repository.UnitOfWork = CurrentUow;
|
||||
return await this.Repository.UpdateAsync(this as TEntity) == 1;
|
||||
}
|
||||
/// <summary>
|
||||
/// 插入数据
|
||||
/// </summary>
|
||||
public virtual void Insert()
|
||||
async public virtual Task Insert()
|
||||
{
|
||||
if (this.Repository == null) this.Repository = Orm.GetRepository<TEntity>();
|
||||
this.Repository.Insert(this as TEntity);
|
||||
this.CreateTime = DateTime.Now;
|
||||
if (this.Repository == null)
|
||||
this.Repository = Orm.GetRepository<TEntity>();
|
||||
|
||||
this.Repository.UnitOfWork = CurrentUow;
|
||||
await this.Repository.InsertAsync(this as TEntity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新或插入
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual void Save()
|
||||
async public virtual Task Save()
|
||||
{
|
||||
if (this.Repository == null) this.Repository = Orm.GetRepository<TEntity>();
|
||||
this.Repository.InsertOrUpdate(this as TEntity);
|
||||
this.UpdateTime = DateTime.Now;
|
||||
if (this.Repository == null)
|
||||
this.Repository = Orm.GetRepository<TEntity>();
|
||||
|
||||
this.Repository.UnitOfWork = CurrentUow;
|
||||
await this.Repository.InsertOrUpdateAsync(this as TEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@ -125,43 +164,10 @@ public abstract class BaseEntity<TEntity, TKey> : BaseEntity<TEntity> where TEnt
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public static TEntity Find(TKey id)
|
||||
async public static Task<TEntity> Find(TKey id)
|
||||
{
|
||||
var item = Select.WhereDynamic(id).First();
|
||||
var item = await Select.WhereDynamic(id).FirstAsync();
|
||||
(item as BaseEntity<TEntity>)?.Attach();
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
[Table(DisableSyncStructure = true)]
|
||||
public abstract class BaseEntity<TEntity, TKey1, TKey2> : BaseEntity<TEntity> where TEntity : class
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 主键1
|
||||
/// </summary>
|
||||
[Column(IsPrimary = true)]
|
||||
public virtual TKey1 PkId1 { get; set; }
|
||||
/// <summary>
|
||||
/// 主键2
|
||||
/// </summary>
|
||||
[Column(IsPrimary = true)]
|
||||
public virtual TKey2 PkId2 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键值获取数据
|
||||
/// </summary>
|
||||
/// <param name="pkid1">主键1</param>
|
||||
/// <param name="pkid2">主键2</param>
|
||||
/// <returns></returns>
|
||||
public static TEntity Find(TKey1 pkid1, TKey1 pkid2)
|
||||
{
|
||||
var item = Select.WhereDynamic(new
|
||||
{
|
||||
PkId1 = pkid1,
|
||||
PkId2 = pkid2
|
||||
}).First();
|
||||
(item as BaseEntity<TEntity>).Attach();
|
||||
return item;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user