From 5dd8210653731fa0429f59591a9d2ddc13036cde Mon Sep 17 00:00:00 2001 From: AlexLEWIS Date: Fri, 6 Aug 2021 16:55:44 +0800 Subject: [PATCH 1/4] Code cleanup. --- .../BaseEntity.cs | 95 +++++++++------- .../BaseEntityAsync.cs | 103 ++++++++++-------- .../BaseEntityReadOnly.cs | 84 ++++++++------ .../FreeSql.Extensions.BaseEntity.csproj | 5 +- 4 files changed, 164 insertions(+), 123 deletions(-) diff --git a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntity.cs b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntity.cs index 40382f84..76d2d2a5 100644 --- a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntity.cs +++ b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntity.cs @@ -1,12 +1,15 @@ -using FreeSql; +#if NET40 +using FreeSql.DataAnnotations; +using System; + +#else using FreeSql.DataAnnotations; using System; -using System.Data; -using System.Diagnostics; -using System.Linq.Expressions; -using System.Threading; using System.Threading.Tasks; +#endif + +// ReSharper disable once CheckNamespace namespace FreeSql { /// @@ -21,9 +24,9 @@ namespace FreeSql { static BaseEntity() { - var tkeyType = typeof(TKey)?.NullableTypeOrThis(); - if (tkeyType == typeof(int) || tkeyType == typeof(long)) - BaseEntity.ConfigEntity(typeof(TEntity), t => t.Property("Id").IsIdentity(true)); + var keyType = typeof(TKey).NullableTypeOrThis(); + if (keyType == typeof(int) || keyType == typeof(long)) + ConfigEntity(typeof(TEntity), t => t.Property("Id").IsIdentity(true)); } /// @@ -32,14 +35,13 @@ namespace FreeSql [Column(Position = 1)] public virtual TKey Id { get; set; } -#if net40 -#else +#if !NET40 /// /// 根据主键值获取数据 /// /// /// - async public static Task FindAsync(TKey id) + public static async Task FindAsync(TKey id) { var item = await Select.WhereDynamic(id).FirstAsync(); (item as BaseEntity)?.Attach(); @@ -69,15 +71,18 @@ namespace FreeSql { bool UpdateIsDeleted(bool value) { - if (this.Repository == null) + if (Repository is null) + { return Orm.Update(this as TEntity) - .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) - .Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrows() == 1; + .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) + .Set(a => (a as BaseEntity).IsDeleted, IsDeleted = value).ExecuteAffrows() == 1; + } - this.IsDeleted = value; - this.Repository.UnitOfWork = _resolveUow?.Invoke(); - return this.Repository.Update(this as TEntity) == 1; + IsDeleted = value; + Repository.UnitOfWork = _resolveUow?.Invoke(); + return Repository.Update(this as TEntity) == 1; } + /// /// 删除数据 /// @@ -85,18 +90,21 @@ namespace FreeSql /// public virtual bool Delete(bool physicalDelete = false) { - if (physicalDelete == false) return this.UpdateIsDeleted(true); - if (this.Repository == null) + if (physicalDelete == false) + return UpdateIsDeleted(true); + + if (Repository is null) return Orm.Delete(this as TEntity).ExecuteAffrows() == 1; - this.Repository.UnitOfWork = _resolveUow?.Invoke(); - return this.Repository.Delete(this as TEntity) == 1; + Repository.UnitOfWork = _resolveUow?.Invoke(); + return Repository.Delete(this as TEntity) == 1; } + /// /// 恢复删除的数据 /// /// - public virtual bool Restore() => this.UpdateIsDeleted(false); + public virtual bool Restore() => UpdateIsDeleted(false); /// /// 更新数据 @@ -104,26 +112,29 @@ namespace FreeSql /// public virtual bool Update() { - this.UpdateTime = DateTime.Now; - if (this.Repository == null) + UpdateTime = DateTime.Now; + if (Repository is null) + { return Orm.Update() - .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) - .SetSource(this as TEntity).ExecuteAffrows() == 1; + .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) + .SetSource(this as TEntity).ExecuteAffrows() == 1; + } - this.Repository.UnitOfWork = _resolveUow?.Invoke(); - return this.Repository.Update(this as TEntity) == 1; + Repository.UnitOfWork = _resolveUow?.Invoke(); + return Repository.Update(this as TEntity) == 1; } + /// /// 插入数据 /// public virtual TEntity Insert() { - this.CreateTime = DateTime.Now; - if (this.Repository == null) - this.Repository = Orm.GetRepository(); + CreateTime = DateTime.Now; + if (Repository is null) + Repository = Orm.GetRepository(); - this.Repository.UnitOfWork = _resolveUow?.Invoke(); - return this.Repository.Insert(this as TEntity); + Repository.UnitOfWork = _resolveUow?.Invoke(); + return Repository.Insert(this as TEntity); } /// @@ -132,12 +143,12 @@ namespace FreeSql /// public virtual TEntity Save() { - this.UpdateTime = DateTime.Now; - if (this.Repository == null) - this.Repository = Orm.GetRepository(); + UpdateTime = DateTime.Now; + if (Repository is null) + Repository = Orm.GetRepository(); - this.Repository.UnitOfWork = _resolveUow?.Invoke(); - return this.Repository.InsertOrUpdate(this as TEntity); + Repository.UnitOfWork = _resolveUow?.Invoke(); + return Repository.InsertOrUpdate(this as TEntity); } /// @@ -146,11 +157,11 @@ namespace FreeSql /// 导航属性名 public virtual void SaveMany(string navigatePropertyName) { - if (this.Repository == null) - this.Repository = Orm.GetRepository(); + if (Repository is null) + Repository = Orm.GetRepository(); - this.Repository.UnitOfWork = _resolveUow?.Invoke(); - this.Repository.SaveMany(this as TEntity, navigatePropertyName); + Repository.UnitOfWork = _resolveUow?.Invoke(); + Repository.SaveMany(this as TEntity, navigatePropertyName); } } } \ No newline at end of file diff --git a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityAsync.cs b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityAsync.cs index ca589470..3821a812 100644 --- a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityAsync.cs +++ b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityAsync.cs @@ -1,9 +1,14 @@ - -using FreeSql; +#if NET40 +using FreeSql.DataAnnotations; + +#else using FreeSql.DataAnnotations; using System; using System.Threading.Tasks; +#endif + +// ReSharper disable once CheckNamespace namespace FreeSql { /// @@ -18,32 +23,30 @@ namespace FreeSql { static BaseEntityAsync() { - var tkeyType = typeof(TKey)?.NullableTypeOrThis(); - if (tkeyType == typeof(int) || tkeyType == typeof(long)) - BaseEntity.ConfigEntity(typeof(TEntity), t => t.Property("Id").IsIdentity(true)); + var keyType = typeof(TKey).NullableTypeOrThis(); + if (keyType == typeof(int) || keyType == typeof(long)) + ConfigEntity(typeof(TEntity), t => t.Property("Id").IsIdentity(true)); } /// /// 主键 /// - [Column(Position = 1)] + [Column(Position = 1)] public virtual TKey Id { get; set; } -#if net40 -#else +#if !NET40 /// /// 根据主键值获取数据 /// /// /// - async public static Task FindAsync(TKey id) + public static async Task FindAsync(TKey id) { var item = await Select.WhereDynamic(id).FirstAsync(); (item as BaseEntity)?.Attach(); return item; } #endif - } /// @@ -53,65 +56,73 @@ namespace FreeSql [Table(DisableSyncStructure = true)] public abstract class BaseEntityAsync : BaseEntityReadOnly where TEntity : class { -#if net40 -#else +#if !NET40 async Task UpdateIsDeletedAsync(bool value) { - if (this.Repository == null) + if (Repository is null) + { return await Orm.Update(this as TEntity) - .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) - .Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrowsAsync() == 1; + .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) + .Set(a => (a as BaseEntity).IsDeleted, IsDeleted = value).ExecuteAffrowsAsync() == 1; + } - this.IsDeleted = value; - this.Repository.UnitOfWork = _resolveUow?.Invoke(); - return await this.Repository.UpdateAsync(this as TEntity) == 1; + IsDeleted = value; + Repository.UnitOfWork = _resolveUow?.Invoke(); + return await Repository.UpdateAsync(this as TEntity) == 1; } + /// /// 删除数据 /// /// 是否物理删除 /// - async public virtual Task DeleteAsync(bool physicalDelete = false) + public virtual async Task DeleteAsync(bool physicalDelete = false) { - if (physicalDelete == false) return await this.UpdateIsDeletedAsync(true); - if (this.Repository == null) + if (physicalDelete == false) + return await UpdateIsDeletedAsync(true); + + if (Repository is null) return await Orm.Delete(this as TEntity).ExecuteAffrowsAsync() == 1; - this.Repository.UnitOfWork = _resolveUow?.Invoke(); - return await this.Repository.DeleteAsync(this as TEntity) == 1; + Repository.UnitOfWork = _resolveUow?.Invoke(); + return await Repository.DeleteAsync(this as TEntity) == 1; } + /// /// 恢复删除的数据 /// /// - public virtual Task RestoreAsync() => this.UpdateIsDeletedAsync(false); + public virtual Task RestoreAsync() => UpdateIsDeletedAsync(false); /// /// 更新数据 /// /// - async public virtual Task UpdateAsync() + public virtual async Task UpdateAsync() { - this.UpdateTime = DateTime.Now; - if (this.Repository == null) + UpdateTime = DateTime.Now; + if (Repository is null) + { return await Orm.Update() - .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) - .SetSource(this as TEntity).ExecuteAffrowsAsync() == 1; + .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) + .SetSource(this as TEntity).ExecuteAffrowsAsync() == 1; + } - this.Repository.UnitOfWork = _resolveUow?.Invoke(); - return await this.Repository.UpdateAsync(this as TEntity) == 1; + Repository.UnitOfWork = _resolveUow?.Invoke(); + return await Repository.UpdateAsync(this as TEntity) == 1; } + /// /// 插入数据 /// public virtual Task InsertAsync() { - this.CreateTime = DateTime.Now; - if (this.Repository == null) - this.Repository = Orm.GetRepository(); + CreateTime = DateTime.Now; + if (Repository is null) + Repository = Orm.GetRepository(); - this.Repository.UnitOfWork = _resolveUow?.Invoke(); - return this.Repository.InsertAsync(this as TEntity); + Repository.UnitOfWork = _resolveUow?.Invoke(); + return Repository.InsertAsync(this as TEntity); } /// @@ -120,12 +131,12 @@ namespace FreeSql /// public virtual Task SaveAsync() { - this.UpdateTime = DateTime.Now; - if (this.Repository == null) - this.Repository = Orm.GetRepository(); + UpdateTime = DateTime.Now; + if (Repository is null) + Repository = Orm.GetRepository(); - this.Repository.UnitOfWork = _resolveUow?.Invoke(); - return this.Repository.InsertOrUpdateAsync(this as TEntity); + Repository.UnitOfWork = _resolveUow?.Invoke(); + return Repository.InsertOrUpdateAsync(this as TEntity); } /// @@ -134,12 +145,12 @@ namespace FreeSql /// 导航属性名 public virtual Task SaveManyAsync(string navigatePropertyName) { - if (this.Repository == null) - this.Repository = Orm.GetRepository(); + if (Repository is null) + Repository = Orm.GetRepository(); - this.Repository.UnitOfWork = _resolveUow?.Invoke(); - return this.Repository.SaveManyAsync(this as TEntity, navigatePropertyName); + Repository.UnitOfWork = _resolveUow?.Invoke(); + return Repository.SaveManyAsync(this as TEntity, navigatePropertyName); } #endif } -} +} \ No newline at end of file diff --git a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs index b3dd1c8a..13c44e92 100644 --- a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs +++ b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs @@ -1,15 +1,14 @@ - -using FreeSql.DataAnnotations; +using FreeSql.DataAnnotations; using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Data.Common; using System.Diagnostics; using System.Linq; using System.Linq.Expressions; using System.Threading; +// ReSharper disable once CheckNamespace namespace FreeSql { /// @@ -19,6 +18,7 @@ namespace FreeSql public abstract class BaseEntity { internal static IFreeSql _ormPriv; + /// /// 全局 IFreeSql orm 对象 /// @@ -26,6 +26,7 @@ namespace FreeSql .UseAutoSyncStructure(true) .UseConnectionString(DataType.Sqlite, ""data source=test.db;max pool size=5"") .Build());"); + internal static Func _resolveUow; /// @@ -52,21 +53,24 @@ namespace FreeSql _ormPriv.CodeFirst.ConfigEntity(cei.EntityType, cei.Fluent); } } + _resolveUow = resolveUow; } - + class ConfigEntityInfo { public Type EntityType; public Action Fluent; } - static ConcurrentQueue _configEntityQueues = new ConcurrentQueue(); - static object _configEntityLock = new object(); + + static ConcurrentQueue _configEntityQueues = new(); + static object _configEntityLock = new(); + internal static void ConfigEntity(Type entityType, Action fluent) { lock (_configEntityLock) { - if (_ormPriv == null) + if (_ormPriv is null) _configEntityQueues.Enqueue(new ConfigEntityInfo { EntityType = entityType, Fluent = fluent }); else _ormPriv.CodeFirst.ConfigEntity(entityType, fluent); @@ -78,16 +82,19 @@ namespace FreeSql /// [Column(Position = -4)] public virtual DateTime CreateTime { get; set; } = DateTime.Now; + /// /// 更新时间 /// [Column(Position = -3)] public virtual DateTime UpdateTime { get; set; } + /// /// 逻辑删除 /// [Column(Position = -2)] public virtual bool IsDeleted { get; set; } + /// /// 排序 /// @@ -107,25 +114,29 @@ namespace FreeSql get { var select = Orm.Select() - .TrackToList(TrackToList) //自动为每个元素 Attach - .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction(false)); + .TrackToList(TrackToList) //自动为每个元素 Attach + .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction(false)); return select.WhereCascade(a => (a as BaseEntity).IsDeleted == false); } } static void TrackToList(object list) { - if (list == null) return; - var ls = list as IList; - if (ls == null) + if (list is null) + return; + + if (list is not IList ls) { - var ie = list as IEnumerable; - if (ie == null) return; + if (list is not IEnumerable ie) + return; + var isFirst = true; + IBaseRepository berepo = null; + foreach (var item in ie) { - if (item == null) return; + if (item is null) return; if (isFirst) { isFirst = false; @@ -135,28 +146,36 @@ namespace FreeSql if (Orm.CodeFirst.GetTableByEntity(itemType)?.Primarys.Any() != true) return; if (item is BaseEntity == false) return; } - var beitem = item as BaseEntity; - if (beitem != null) + + if (item is BaseEntity entity) { - if (berepo == null) berepo = Orm.GetRepository(); - beitem.Repository = berepo; - beitem.Attach(); + berepo ??= Orm.GetRepository(); + entity.Repository = berepo; + entity.Attach(); } } + return; } - if (ls.Any() == false) return; - if (ls.FirstOrDefault() is BaseEntity == false) return; - if (Orm.CodeFirst.GetTableByEntity(typeof(TEntity))?.Primarys.Any() != true) return; + + if (ls.Any() == false) + return; + + if (ls.FirstOrDefault() is not BaseEntity) + return; + + if (Orm.CodeFirst.GetTableByEntity(typeof(TEntity))?.Primarys.Any() != true) + return; + IBaseRepository repo = null; + foreach (var item in ls) { - var beitem = item as BaseEntity; - if (beitem != null) + if (item is BaseEntity entity) { - if (repo == null) repo = Orm.GetRepository(); - beitem.Repository = repo; - beitem.Attach(); + repo ??= Orm.GetRepository(); + entity.Repository = repo; + entity.Attach(); } } } @@ -167,6 +186,7 @@ namespace FreeSql /// lambda表达式 /// public static ISelect Where(Expression> exp) => Select.Where(exp); + /// /// 查询条件,Where(true, a => a.Id > 10),支导航对象查询,Where(true, a => a.Author.Email == "2881099@qq.com") /// @@ -185,12 +205,10 @@ namespace FreeSql /// public TEntity Attach() { - if (this.Repository == null) - this.Repository = Orm.GetRepository(); - + Repository ??= Orm.GetRepository(); var item = this as TEntity; - this.Repository.Attach(item); + Repository.Attach(item); return item; } } -} +} \ No newline at end of file diff --git a/Extensions/FreeSql.Extensions.BaseEntity/FreeSql.Extensions.BaseEntity.csproj b/Extensions/FreeSql.Extensions.BaseEntity/FreeSql.Extensions.BaseEntity.csproj index bfd13274..8a9e2002 100644 --- a/Extensions/FreeSql.Extensions.BaseEntity/FreeSql.Extensions.BaseEntity.csproj +++ b/Extensions/FreeSql.Extensions.BaseEntity/FreeSql.Extensions.BaseEntity.csproj @@ -6,8 +6,8 @@ true FreeSql;ncc;YeXiangQin BaseEntity 是一种极简单的 CodeFirst 开发方式,特别对单表或多表CRUD,利用继承节省了每个实体类的重复属性(创建时间、ID等字段),软件删除等功能,进行 crud 操作时不必时常考虑仓储的使用. - https://github.com/2881099/FreeSql/tree/master/Extensions/FreeSql.Extensions.BaseEntity - https://github.com/2881099/FreeSql/tree/master/Extensions/FreeSql.Extensions.BaseEntity + https://github.com/dotnetcore/FreeSql/tree/master/Extensions/FreeSql.Extensions.BaseEntity + https://github.com/dotnetcore/FreeSql/tree/master/Extensions/FreeSql.Extensions.BaseEntity git MIT FreeSql;ORM;BaseEntity @@ -19,6 +19,7 @@ true key.snk false + latest From 73d2bfc680da517b626e11b9c4fc75429aa44dd9 Mon Sep 17 00:00:00 2001 From: AlexLEWIS Date: Fri, 6 Aug 2021 17:07:36 +0800 Subject: [PATCH 2/4] Code cleanup. --- .../BaseEntityReadOnly.cs | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs index 13c44e92..da2475d1 100644 --- a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs +++ b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs @@ -8,7 +8,9 @@ using System.Linq; using System.Linq.Expressions; using System.Threading; -// ReSharper disable once CheckNamespace +// ReSharper disable CheckNamespace +// ReSharper disable InconsistentNaming +// ReSharper disable InconsistentlySynchronizedField namespace FreeSql { /// @@ -19,13 +21,16 @@ namespace FreeSql { internal static IFreeSql _ormPriv; + private const string ErrorMessageTemplate = @"使用前请初始化: +BaseEntity.Initialization(new FreeSqlBuilder() + .UseAutoSyncStructure(true) + .UseConnectionString(DataType.Sqlite, ""data source=test.db;max pool size=5"") + .Build());"; + /// /// 全局 IFreeSql orm 对象 /// - 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());"); + public static IFreeSql Orm => _ormPriv ?? throw new Exception(ErrorMessageTemplate); internal static Func _resolveUow; @@ -63,8 +68,8 @@ namespace FreeSql public Action Fluent; } - static ConcurrentQueue _configEntityQueues = new(); - static object _configEntityLock = new(); + static readonly ConcurrentQueue _configEntityQueues = new(); + static readonly object _configEntityLock = new(); internal static void ConfigEntity(Type entityType, Action fluent) { @@ -131,26 +136,29 @@ namespace FreeSql return; var isFirst = true; - - IBaseRepository berepo = null; + IBaseRepository baseRepo = null; foreach (var item in ie) { - if (item is null) return; + if (item is null) + { + return; + } + if (isFirst) { isFirst = false; var itemType = item.GetType(); if (itemType == typeof(object)) return; - if (itemType.FullName.Contains("FreeSqlLazyEntity__")) itemType = itemType.BaseType; + if (itemType.FullName!.Contains("FreeSqlLazyEntity__")) itemType = itemType.BaseType; if (Orm.CodeFirst.GetTableByEntity(itemType)?.Primarys.Any() != true) return; - if (item is BaseEntity == false) return; + if (item is not BaseEntity) return; } if (item is BaseEntity entity) { - berepo ??= Orm.GetRepository(); - entity.Repository = berepo; + baseRepo ??= Orm.GetRepository(); + entity.Repository = baseRepo; entity.Attach(); } } From 6789c3f3ee69d200e73b245c49c83f2f7608a54a Mon Sep 17 00:00:00 2001 From: AlexLEWIS Date: Fri, 6 Aug 2021 17:23:52 +0800 Subject: [PATCH 3/4] Add English comments. --- .../BaseEntity.cs | 31 ++++++++++++------- .../BaseEntityAsync.cs | 30 +++++++++++------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntity.cs b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntity.cs index 76d2d2a5..8986e5b5 100644 --- a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntity.cs +++ b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntity.cs @@ -13,8 +13,12 @@ using System.Threading.Tasks; namespace FreeSql { /// + /// Entity base class, including CreateTime/UpdateTime/IsDeleted, the CRUD methods, and ID primary key definition. + /// /// 包括 CreateTime/UpdateTime/IsDeleted、CRUD 方法、以及 ID 主键定义 的实体基类 /// + /// When TKey is int/long, the Id is set to be an auto-incremented primary key + /// /// 当 TKey 为 int/long 时,Id 主键被设为自增值主键 /// /// @@ -30,6 +34,7 @@ namespace FreeSql } /// + /// Primary key
/// 主键 ///
[Column(Position = 1)] @@ -37,6 +42,7 @@ namespace FreeSql #if !NET40 /// + /// Get data based on the value of the primary key
/// 根据主键值获取数据 ///
/// @@ -50,6 +56,7 @@ namespace FreeSql #endif /// + /// Get data based on the value of the primary key
/// 根据主键值获取数据 ///
/// @@ -63,6 +70,8 @@ namespace FreeSql } /// + /// Entity base class, including CreateTime/UpdateTime/IsDeleted, and sync/async CRUD methods. + /// /// 包括 CreateTime/UpdateTime/IsDeleted、以及 CRUD 异步和同步方法的实体基类 /// /// @@ -84,9 +93,10 @@ namespace FreeSql } /// + /// To delete data
/// 删除数据 ///
- /// 是否物理删除 + /// To flag whether to delete the physical level of the data /// public virtual bool Delete(bool physicalDelete = false) { @@ -101,12 +111,14 @@ namespace FreeSql } /// + /// To recover deleted data
/// 恢复删除的数据 ///
/// public virtual bool Restore() => UpdateIsDeleted(false); /// + /// To update data
/// 更新数据 ///
/// @@ -125,41 +137,38 @@ namespace FreeSql } /// + /// To insert data
/// 插入数据 ///
public virtual TEntity Insert() { CreateTime = DateTime.Now; - if (Repository is null) - Repository = Orm.GetRepository(); - + Repository ??= Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.Insert(this as TEntity); } /// + /// To insert or update data
/// 更新或插入 ///
/// public virtual TEntity Save() { UpdateTime = DateTime.Now; - if (Repository is null) - Repository = Orm.GetRepository(); - + Repository ??= Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.InsertOrUpdate(this as TEntity); } /// + /// To completely save the navigation properties of the entity in the form of sub-tables.
/// 【完整】保存导航属性,子表 ///
- /// 导航属性名 + /// Navigation property name public virtual void SaveMany(string navigatePropertyName) { - if (Repository is null) - Repository = Orm.GetRepository(); - + Repository ??= Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); Repository.SaveMany(this as TEntity, navigatePropertyName); } diff --git a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityAsync.cs b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityAsync.cs index 3821a812..58f1300e 100644 --- a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityAsync.cs +++ b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityAsync.cs @@ -12,8 +12,12 @@ using System.Threading.Tasks; namespace FreeSql { /// + /// Entity base class, including CreateTime/UpdateTime/IsDeleted, the async CRUD methods, and ID primary key definition. + /// /// 包括 CreateTime/UpdateTime/IsDeleted、CRUD 异步方法、以及 ID 主键定义 的实体基类 /// + /// When TKey is int/long, the Id is set to be an auto-incremented primary key + /// /// 当 TKey 为 int/long 时,Id 主键被设为自增值主键 /// /// @@ -29,6 +33,7 @@ namespace FreeSql } /// + /// Primary key
/// 主键 ///
[Column(Position = 1)] @@ -36,6 +41,7 @@ namespace FreeSql #if !NET40 /// + /// Get data based on the value of the primary key
/// 根据主键值获取数据 ///
/// @@ -50,6 +56,8 @@ namespace FreeSql } /// + /// Entity base class, including CreateTime/UpdateTime/IsDeleted, and async CRUD methods. + /// /// 包括 CreateTime/UpdateTime/IsDeleted、以及 CRUD 异步方法的实体基类 /// /// @@ -72,9 +80,10 @@ namespace FreeSql } /// + /// To delete data
/// 删除数据 ///
- /// 是否物理删除 + /// To flag whether to delete the physical level of the data /// public virtual async Task DeleteAsync(bool physicalDelete = false) { @@ -89,12 +98,14 @@ namespace FreeSql } /// + /// To recover deleted data
/// 恢复删除的数据 ///
/// public virtual Task RestoreAsync() => UpdateIsDeletedAsync(false); /// + /// To update data
/// 更新数据 ///
/// @@ -113,41 +124,38 @@ namespace FreeSql } /// + /// To insert data
/// 插入数据 ///
public virtual Task InsertAsync() { CreateTime = DateTime.Now; - if (Repository is null) - Repository = Orm.GetRepository(); - + Repository ??= Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.InsertAsync(this as TEntity); } /// + /// To insert or update data
/// 更新或插入 ///
/// public virtual Task SaveAsync() { UpdateTime = DateTime.Now; - if (Repository is null) - Repository = Orm.GetRepository(); - + Repository ??= Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.InsertOrUpdateAsync(this as TEntity); } /// + /// To completely save the navigation properties of the entity in the form of sub-tables.
/// 【完整】保存导航属性,子表 ///
- /// 导航属性名 + /// Navigation property name public virtual Task SaveManyAsync(string navigatePropertyName) { - if (Repository is null) - Repository = Orm.GetRepository(); - + Repository ??= Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.SaveManyAsync(this as TEntity, navigatePropertyName); } From 902f1e45f13b8cf810d89ee19b10e2fbb5a68210 Mon Sep 17 00:00:00 2001 From: AlexLEWIS Date: Sun, 8 Aug 2021 22:26:10 +0800 Subject: [PATCH 4/4] Add English comments. --- .../BaseEntityReadOnly.cs | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs index da2475d1..8b10be63 100644 --- a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs +++ b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs @@ -14,6 +14,8 @@ using System.Threading; namespace FreeSql { /// + /// Entity base class, including CreateTime/UpdateTime/IsDeleted. + /// /// 包括 CreateTime/UpdateTime/IsDeleted 的实体基类 /// [Table(DisableSyncStructure = true)] @@ -28,23 +30,24 @@ BaseEntity.Initialization(new FreeSqlBuilder() .Build());"; /// - /// 全局 IFreeSql orm 对象 + /// Global IFreeSql ORM Object
+ /// 全局 IFreeSql ORM 对象 ///
public static IFreeSql Orm => _ormPriv ?? throw new Exception(ErrorMessageTemplate); internal static Func _resolveUow; /// - /// 初始化BaseEntity - /// BaseEntity.Initialization(new FreeSqlBuilder() + /// To initialize the BaseEntity
+ /// 初始化 BaseEntity /// - /// .UseAutoSyncStructure(true) - /// - /// .UseConnectionString(DataType.Sqlite, "data source=test.db;max pool size=5") - /// - /// .Build()); + /// BaseEntity.Initialization(
+ /// new FreeSqlBuilder()
+ /// .UseAutoSyncStructure(true)
+ /// .UseConnectionString(DataType.Sqlite, "data source=test.db;max pool size=5")
+ /// .Build()); ///
- /// IFreeSql orm 对象 + /// IFreeSql ORM Object /// 工作单元(事务)委托,如果不使用事务请传 null解释:由于AsyncLocal平台兼容不好,所以交给外部管理 public static void Initialization(IFreeSql fsql, Func resolveUow) { @@ -83,34 +86,45 @@ BaseEntity.Initialization(new FreeSqlBuilder() } /// + /// Created time
/// 创建时间 ///
[Column(Position = -4)] public virtual DateTime CreateTime { get; set; } = DateTime.Now; /// + /// Updated time
/// 更新时间 ///
[Column(Position = -3)] public virtual DateTime UpdateTime { get; set; } /// + /// Logical Delete
/// 逻辑删除 ///
[Column(Position = -2)] public virtual bool IsDeleted { get; set; } /// + /// Sort
/// 排序 ///
[Column(Position = -1)] public virtual int Sort { get; set; } } + /// + /// A readonly entity base class, including CreateTime/UpdateTime/IsDeleted. + /// + /// 包括 CreateTime/UpdateTime/IsDeleted 的只读实体基类 + /// + /// [Table(DisableSyncStructure = true)] public abstract class BaseEntityReadOnly : BaseEntity where TEntity : class { /// + /// To query data
/// 查询数据 ///
/// @@ -189,14 +203,22 @@ BaseEntity.Initialization(new FreeSqlBuilder() } /// - /// 查询条件,Where(a => a.Id > 10),支持导航对象查询,Where(a => a.Author.Email == "2881099@qq.com") + /// Query conditions
+ /// 查询条件,Where(a => a.Id> 10) + /// + /// Support navigation object query
+ /// 支持导航对象查询,Where(a => a.Author.Email == "2881099@qq.com") ///
/// lambda表达式 /// public static ISelect Where(Expression> exp) => Select.Where(exp); /// - /// 查询条件,Where(true, a => a.Id > 10),支导航对象查询,Where(true, a => a.Author.Email == "2881099@qq.com") + /// Query conditions
+ /// 查询条件,Where(true, a => a.Id > 10) + /// + /// Support navigation object query
+ /// 支导航对象查询,Where(true, a => a.Author.Email == "2881099@qq.com") ///
/// true 时生效 /// lambda表达式 @@ -204,12 +226,14 @@ BaseEntity.Initialization(new FreeSqlBuilder() public static ISelect WhereIf(bool condition, Expression> exp) => Select.WhereIf(condition, exp); /// + /// Repository object.
/// 仓储对象 ///
protected IBaseRepository Repository { get; set; } /// - /// 附加实体,在更新数据时,只更新变化的部分 + /// To Attach entities. When updating data, only the changed part is updated.
+ /// 附加实体。在更新数据时,只更新变化的部分 ///
public TEntity Attach() {