From bfe7ecd81a0664ffbd2d009684768e320576d230 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Wed, 6 Apr 2022 11:21:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EDbSet=E5=92=8CSugarDbTables?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E3=80=82=E5=8F=AF=E4=BB=A5=E7=BB=A7=E6=89=BF?= =?UTF-8?q?SugarDbTables=E5=B9=B6=E4=BD=BF=E7=94=A8DbSet=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi/DatabaseDefinitions/DbSet.cs | 25 +++++++++++++ ...tionConfig.cs => SugarConnectionConfig.cs} | 4 ++- .../DatabaseDefinitions/SugarDbTables.cs | 35 +++++++++++++++++++ Falcon.SugarApi/IServiceCollectionExtend.cs | 10 ------ 4 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 Falcon.SugarApi/DatabaseDefinitions/DbSet.cs rename Falcon.SugarApi/DatabaseDefinitions/{MyConnectionConfig.cs => SugarConnectionConfig.cs} (94%) create mode 100644 Falcon.SugarApi/DatabaseDefinitions/SugarDbTables.cs diff --git a/Falcon.SugarApi/DatabaseDefinitions/DbSet.cs b/Falcon.SugarApi/DatabaseDefinitions/DbSet.cs new file mode 100644 index 0000000..90daac1 --- /dev/null +++ b/Falcon.SugarApi/DatabaseDefinitions/DbSet.cs @@ -0,0 +1,25 @@ +using SqlSugar; + +namespace Falcon.SugarApi.DatabaseDefinitions +{ + /// + /// 代表一张表 + /// + /// 表定义类型 + public class DbSet : SimpleClient where T : class, new() + { + /// + /// 通过数据上下文构造一张表 + /// + /// 数据上下文 + public DbSet(SqlSugarClient context) : base(context) { } + /// + /// 返回可迭代的 + /// + /// + public virtual ISugarQueryable Queryable() { + return Context.Queryable(); + } + } + +} diff --git a/Falcon.SugarApi/DatabaseDefinitions/MyConnectionConfig.cs b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs similarity index 94% rename from Falcon.SugarApi/DatabaseDefinitions/MyConnectionConfig.cs rename to Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs index 3f6a19d..f923d20 100644 --- a/Falcon.SugarApi/DatabaseDefinitions/MyConnectionConfig.cs +++ b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs @@ -15,7 +15,9 @@ namespace Falcon.SugarApi.DatabaseDefinitions /// 是否使用log /// public bool Log { get; set; } - + /// + /// 实例化SugarDb链接配置 + /// public SugarConnectionConfig() { this.ConfigureExternalServices ??= new ConfigureExternalServices { }; this.ConfigureExternalServices.EntityService += (p, c) => { diff --git a/Falcon.SugarApi/DatabaseDefinitions/SugarDbTables.cs b/Falcon.SugarApi/DatabaseDefinitions/SugarDbTables.cs new file mode 100644 index 0000000..fb4775d --- /dev/null +++ b/Falcon.SugarApi/DatabaseDefinitions/SugarDbTables.cs @@ -0,0 +1,35 @@ +using SqlSugar; +using System; +using System.Reflection; + +namespace Falcon.SugarApi.DatabaseDefinitions +{ + /// + /// 表示数据库表结构定义 + /// + public abstract class SugarDbTables + { + /// + /// 表使用的数据上下文 + /// + private SugarDbContext DbContext { get; set; } + + /// + /// 通过数据上下文构造表集合对象,并实例化所有DbSet实例 + /// + /// 使用的数据上下文 + /// 是否实例化所有DbSet表对象 + public SugarDbTables(SugarDbContext dbContext, bool createInstance = true) { + this.DbContext = dbContext; + if (createInstance) { + foreach (PropertyInfo property in this.GetType().GetProperties()) { + var ptype = property.PropertyType; + if (ptype.IsGenericType() && ptype.GetGenericTypeDefinition() == typeof(DbSet<>) && property.CanWrite) { + property.SetValue(this, Activator.CreateInstance(ptype, this.DbContext)); + } + } + } + } + } + +} diff --git a/Falcon.SugarApi/IServiceCollectionExtend.cs b/Falcon.SugarApi/IServiceCollectionExtend.cs index 881b003..08307e3 100644 --- a/Falcon.SugarApi/IServiceCollectionExtend.cs +++ b/Falcon.SugarApi/IServiceCollectionExtend.cs @@ -14,16 +14,6 @@ namespace Falcon.SugarApi /// public static class IServiceCollectionExtend { - /// - /// 注册Falcon.Sugar相关api方法和数据库上下文。 - /// - /// 服务集合 - /// 数据上下文配置节点 - /// 服务集合 - public static IServiceCollection AddSugarApiWithDbContext(this IServiceCollection services, SugarConnectionConfig config) { - return services.AddSugarApiDbContext(config).AddApiReturnModelProvider(); - } - /// /// 注册sugarDbcontext数据上下文 ///