diff --git a/src/Infrastructure/Domains/EntityBase.cs b/src/Infrastructure/Domains/EntityBase.cs new file mode 100644 index 0000000..a045181 --- /dev/null +++ b/src/Infrastructure/Domains/EntityBase.cs @@ -0,0 +1,19 @@ +namespace Infrastructure.Domains; + +[SugarTable] +public abstract class EntityBase : IEntityBase where T : class, IEquatable +{ + public bool IsDeleted { get; set; } + + public T Id { get; set; } + + public DateTime CreatedDate { get; set; } + + public string? Name { get; set; } + + public DateTime? UpdatedDate { get; set; } + + public T CreatorId { get; set; } + + public T? UpdaterId { get; set; } +} \ No newline at end of file diff --git a/src/Infrastructure/Domains/IDateAbility.cs b/src/Infrastructure/Domains/IDateAbility.cs new file mode 100644 index 0000000..e1ba4da --- /dev/null +++ b/src/Infrastructure/Domains/IDateAbility.cs @@ -0,0 +1,8 @@ +namespace Infrastructure.Domains; + +public interface IDateAbility +{ + DateTime CreatedDate { get; set; } + + DateTime? UpdatedDate { get; set; } +} \ No newline at end of file diff --git a/src/Infrastructure/Domains/IDeletable.cs b/src/Infrastructure/Domains/IDeletable.cs new file mode 100644 index 0000000..a6ad853 --- /dev/null +++ b/src/Infrastructure/Domains/IDeletable.cs @@ -0,0 +1,9 @@ +namespace Infrastructure.Domains; + +/// +/// 实体类软删除接口,用于filter +/// +public interface IDeletable +{ + bool IsDeleted { get; set; } +} \ No newline at end of file diff --git a/src/Infrastructure/Domains/IEntityBase.cs b/src/Infrastructure/Domains/IEntityBase.cs new file mode 100644 index 0000000..d8f4b8e --- /dev/null +++ b/src/Infrastructure/Domains/IEntityBase.cs @@ -0,0 +1,14 @@ +namespace Infrastructure.Domains; + +public interface IEntityBase : IPrimaryKey, IDateAbility,IOperateAbility, IDeletable where T : IEquatable +{ + public string? Name { get; set; } + +} + +public interface IOperateAbility where T : IEquatable +{ + T CreatorId { get; set; } + + T? UpdaterId { get; set; } +} \ No newline at end of file diff --git a/src/Infrastructure/Domains/IPrimaryKey.cs b/src/Infrastructure/Domains/IPrimaryKey.cs new file mode 100644 index 0000000..32afee7 --- /dev/null +++ b/src/Infrastructure/Domains/IPrimaryKey.cs @@ -0,0 +1,6 @@ +namespace Infrastructure.Domains; + +public interface IPrimaryKey where T : IEquatable +{ + [SugarColumn(IsPrimaryKey = true)] public T Id { get; set; } +} \ No newline at end of file diff --git a/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs b/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs index 02c210a..a321f89 100644 --- a/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs +++ b/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs @@ -1,6 +1,7 @@ using System.Security.Claims; using Asp.Versioning; using Asp.Versioning.ApiExplorer; +using Infrastructure.Domains; using Infrastructure.Filters; using Infrastructure.HttpUserContext; using Infrastructure.Options; @@ -318,7 +319,7 @@ public static class ServiceCollectionExtensions IConfiguration configuration, IWebHostEnvironment hostEnvironment, List? connectionConfigs = null, - Action? onDataChanging = null, + Action? onDataExecuting = null, Action? onDiffLogEvent = null ) { @@ -397,7 +398,24 @@ public static class ServiceCollectionExtensions }; } - client.Aop.DataExecuting = onDataChanging; + onDataExecuting ??= (o, entityInfo) => + { + if (entityInfo is + { + OperationType: DataFilterType.InsertByObject, PropertyName: nameof(IDateAbility.CreatedDate) + }) + { + entityInfo.SetValue(DateTime.Now); + } + + if (entityInfo is + { OperationType: DataFilterType.UpdateByObject, PropertyName: nameof(IDateAbility.UpdatedDate) }) + { + entityInfo.SetValue(DateTime.Now); + } + }; + + client.Aop.DataExecuting = onDataExecuting; client.Aop.OnDiffLogEvent = onDiffLogEvent; }); services.AddSingleton(sugarScope); diff --git a/src/Infrastructure/IDeleteable.cs b/src/Infrastructure/IDeleteable.cs deleted file mode 100644 index 970b548..0000000 --- a/src/Infrastructure/IDeleteable.cs +++ /dev/null @@ -1,40 +0,0 @@ -namespace Infrastructure; - -/// -/// 实体类软删除接口,用于filter -/// -public interface IDeletable -{ - bool IsDeleted { get; set; } -} - -public interface IPrimaryKey where T : IEquatable -{ - public T Id { get; set; } -} - -public interface IEntityBase : IPrimaryKey, IDeletable where T : IEquatable -{ - public DateTime CreatedDate { get; set; } - - public DateTime? UpdatedDate { get; set; } - - public string? Name { get; set; } - - public T CreatorId { get; set; } -} - -public abstract class EntityBase : IEntityBase where T : class, IEquatable -{ - public bool IsDeleted { get; set; } - - public T Id { get; set; } - - public DateTime CreatedDate { get; set; } - - public string? Name { get; set; } - - public DateTime? UpdatedDate { get; set; } - - public T CreatorId { get; set; } -} \ No newline at end of file