Skip to content

Commit

Permalink
- 调整 BaseEntity,移除 BaseTreeEntity、Tenant 租户,改变事务习惯;
Browse files Browse the repository at this point in the history
  • Loading branch information
28810 authored and 28810 committed May 8, 2020
1 parent fc828d1 commit 6d5575d
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 605 deletions.
16 changes: 13 additions & 3 deletions Examples/base_entity/Program.cs
Expand Up @@ -37,6 +37,8 @@ public class Products : BaseEntity<Products, int>
public string title { get; set; }
}

static AsyncLocal<IUnitOfWork> _asyncUow = new AsyncLocal<IUnitOfWork>();

static void Main(string[] args)
{

Expand Down Expand Up @@ -73,7 +75,7 @@ static void Main(string[] args)
.UseMonitorCommand(cmd => Console.Write(cmd.CommandText))
.UseLazyLoading(true)
.Build();
BaseEntity.Initialization(fsql);
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion

var test01 = EMSServerModel.Model.User.Select.IncludeMany(a => a.Roles).ToList();
Expand Down Expand Up @@ -144,9 +146,17 @@ static void Main(string[] args)

Task.Run(async () =>
{
using (var uow = BaseEntity.Begin())
using (var uow = BaseEntity.Orm.CreateUnitOfWork())
{
var id = (await new User1().SaveAsync()).Id;
_asyncUow.Value = uow;
try
{
var id = (await new User1().SaveAsync()).Id;
}
finally
{
_asyncUow.Value = null;
}
uow.Commit();
}
Expand Down
33 changes: 14 additions & 19 deletions Extensions/FreeSql.Extensions.BaseEntity/BaseEntity.cs
@@ -1,6 +1,4 @@
#if netcore

using FreeSql;
using FreeSql;
using FreeSql.DataAnnotations;
using System;
using System.Data;
Expand Down Expand Up @@ -34,6 +32,8 @@ static BaseEntity()
[Column(Position = 1)]
public virtual TKey Id { get; set; }

#if net40
#else
/// <summary>
/// 根据主键值获取数据
/// </summary>
Expand All @@ -45,6 +45,7 @@ async public static Task<TEntity> FindAsync(TKey id)
(item as BaseEntity<TEntity>)?.Attach();
return item;
}
#endif

/// <summary>
/// 根据主键值获取数据
Expand All @@ -70,12 +71,11 @@ bool UpdateIsDeleted(bool value)
{
if (this.Repository == null)
return Orm.Update<TEntity>(this as TEntity)
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction())
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
.Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrows() == 1;

this.SetTenantId();
this.IsDeleted = value;
this.Repository.UnitOfWork = CurrentUnitOfWork;
this.Repository.UnitOfWork = _resolveUow?.Invoke();
return this.Repository.Update(this as TEntity) == 1;
}
/// <summary>
Expand All @@ -88,8 +88,8 @@ public virtual bool Delete(bool physicalDelete = false)
if (physicalDelete == false) return this.UpdateIsDeleted(true);
if (this.Repository == null)
return Orm.Delete<TEntity>(this as TEntity).ExecuteAffrows() == 1;
//this.SetTenantId();
this.Repository.UnitOfWork = CurrentUnitOfWork;

this.Repository.UnitOfWork = _resolveUow?.Invoke();
return this.Repository.Delete(this as TEntity) == 1;
}
/// <summary>
Expand All @@ -107,11 +107,10 @@ public virtual bool Update()
this.UpdateTime = DateTime.Now;
if (this.Repository == null)
return Orm.Update<TEntity>()
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction())
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
.SetSource(this as TEntity).ExecuteAffrows() == 1;

this.SetTenantId();
this.Repository.UnitOfWork = CurrentUnitOfWork;
this.Repository.UnitOfWork = _resolveUow?.Invoke();
return this.Repository.Update(this as TEntity) == 1;
}
/// <summary>
Expand All @@ -123,8 +122,7 @@ public virtual TEntity Insert()
if (this.Repository == null)
this.Repository = Orm.GetRepository<TEntity>();

this.SetTenantId();
this.Repository.UnitOfWork = CurrentUnitOfWork;
this.Repository.UnitOfWork = _resolveUow?.Invoke();
return this.Repository.Insert(this as TEntity);
}

Expand All @@ -138,8 +136,7 @@ public virtual TEntity Save()
if (this.Repository == null)
this.Repository = Orm.GetRepository<TEntity>();

this.SetTenantId();
this.Repository.UnitOfWork = CurrentUnitOfWork;
this.Repository.UnitOfWork = _resolveUow?.Invoke();
return this.Repository.InsertOrUpdate(this as TEntity);
}

Expand All @@ -152,10 +149,8 @@ public virtual void SaveMany(string navigatePropertyName)
if (this.Repository == null)
this.Repository = Orm.GetRepository<TEntity>();

this.Repository.UnitOfWork = CurrentUnitOfWork;
this.Repository.UnitOfWork = _resolveUow?.Invoke();
this.Repository.SaveMany(this as TEntity, navigatePropertyName);
}
}
}

#endif
}
34 changes: 18 additions & 16 deletions Extensions/FreeSql.Extensions.BaseEntity/BaseEntityAsync.cs
@@ -1,5 +1,4 @@
#if netcore


using FreeSql;
using FreeSql.DataAnnotations;
using System;
Expand Down Expand Up @@ -27,8 +26,11 @@ static BaseEntityAsync()
/// <summary>
/// 主键
/// </summary>
[Column(Position = 1)]
public virtual TKey Id { get; set; }

#if net40
#else
/// <summary>
/// 根据主键值获取数据
/// </summary>
Expand All @@ -40,6 +42,8 @@ async public static Task<TEntity> FindAsync(TKey id)
(item as BaseEntity<TEntity>)?.Attach();
return item;
}
#endif

}

/// <summary>
Expand All @@ -49,15 +53,17 @@ async public static Task<TEntity> FindAsync(TKey id)
[Table(DisableSyncStructure = true)]
public abstract class BaseEntityAsync<TEntity> : BaseEntityReadOnly<TEntity> where TEntity : class
{
#if net40
#else
async Task<bool> UpdateIsDeletedAsync(bool value)
{
if (this.Repository == null)
return await Orm.Update<TEntity>(this as TEntity)
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction())
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
.Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrowsAsync() == 1;

this.IsDeleted = value;
this.Repository.UnitOfWork = CurrentUnitOfWork;
this.Repository.UnitOfWork = _resolveUow?.Invoke();
return await this.Repository.UpdateAsync(this as TEntity) == 1;
}
/// <summary>
Expand All @@ -70,8 +76,8 @@ async public virtual Task<bool> DeleteAsync(bool physicalDelete = false)
if (physicalDelete == false) return await this.UpdateIsDeletedAsync(true);
if (this.Repository == null)
return await Orm.Delete<TEntity>(this as TEntity).ExecuteAffrowsAsync() == 1;
//this.SetTenantId();
this.Repository.UnitOfWork = CurrentUnitOfWork;

this.Repository.UnitOfWork = _resolveUow?.Invoke();
return await this.Repository.DeleteAsync(this as TEntity) == 1;
}
/// <summary>
Expand All @@ -89,11 +95,10 @@ async public virtual Task<bool> UpdateAsync()
this.UpdateTime = DateTime.Now;
if (this.Repository == null)
return await Orm.Update<TEntity>()
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction())
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
.SetSource(this as TEntity).ExecuteAffrowsAsync() == 1;

this.SetTenantId();
this.Repository.UnitOfWork = CurrentUnitOfWork;
this.Repository.UnitOfWork = _resolveUow?.Invoke();
return await this.Repository.UpdateAsync(this as TEntity) == 1;
}
/// <summary>
Expand All @@ -105,8 +110,7 @@ public virtual Task<TEntity> InsertAsync()
if (this.Repository == null)
this.Repository = Orm.GetRepository<TEntity>();

this.SetTenantId();
this.Repository.UnitOfWork = CurrentUnitOfWork;
this.Repository.UnitOfWork = _resolveUow?.Invoke();
return this.Repository.InsertAsync(this as TEntity);
}

Expand All @@ -120,8 +124,7 @@ public virtual Task<TEntity> SaveAsync()
if (this.Repository == null)
this.Repository = Orm.GetRepository<TEntity>();

this.SetTenantId();
this.Repository.UnitOfWork = CurrentUnitOfWork;
this.Repository.UnitOfWork = _resolveUow?.Invoke();
return this.Repository.InsertOrUpdateAsync(this as TEntity);
}

Expand All @@ -134,10 +137,9 @@ public virtual Task SaveManyAsync(string navigatePropertyName)
if (this.Repository == null)
this.Repository = Orm.GetRepository<TEntity>();

this.Repository.UnitOfWork = CurrentUnitOfWork;
this.Repository.UnitOfWork = _resolveUow?.Invoke();
return this.Repository.SaveManyAsync(this as TEntity, navigatePropertyName);
}
#endif
}
}

#endif

0 comments on commit 6d5575d

Please sign in to comment.