From 4ca7b2e023dc33a95f303542399052f5155a37a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Tue, 24 Jan 2023 13:46:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=A7=8D=E5=AD=90=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=A8=A1=E5=9D=97=EF=BC=8C=E5=AE=8C=E6=88=90=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=9B=BF=E6=8D=A2=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/SqlsugarRepository.cs | 4 +-- .../{UnitOfWork.cs => SqlsugarUnitOfWork.cs} | 2 +- ...anager.cs => SqlsugarUnitOfWorkManager.cs} | 8 ++--- .../YiFrameworkCoreSqlsugarModule.cs | 8 ++--- .../Attributes/AppServiceAttribute.cs | 11 +++++++ .../DataSeeds/AbstractDataSeed.cs | 10 ++++-- .../Yi.Framework.Data/DataSeeds/IDataSeed.cs | 4 +-- .../Extensions/DataSeedExtensions.cs | 26 +++++++++++++-- .../Yi.Framework.Data.csproj | 1 + .../YiFrameworkDataModule.cs | 6 +++- .../Yi.Framework.Uow/DefaultUnitOfWork.cs | 31 ++++++++++++++++++ .../DefaultUnitOfWorkManager.cs | 16 +++++++++ .../Yi.Framework.Uow/YiFrameworkUowModule.cs | 29 ++++++++++++++++ .../School/DataSeeds/StudentDataSeed.cs | 27 +++++++++++++++ .../src/project/Yi.Framework.Web/TimeTest.txt | 5 +++ .../project/Yi.Framework.Web/appsettings.json | 6 +++- .../Yi.Framework.Web/yi-sqlsugar-dev.db | Bin 12288 -> 12288 bytes 17 files changed, 173 insertions(+), 21 deletions(-) rename Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/{UnitOfWork.cs => SqlsugarUnitOfWork.cs} (96%) rename Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/{UnitOfWorkManager.cs => SqlsugarUnitOfWorkManager.cs} (73%) create mode 100644 Yi.Framework.Net6/src/framework/Yi.Framework.Uow/DefaultUnitOfWork.cs create mode 100644 Yi.Framework.Net6/src/framework/Yi.Framework.Uow/DefaultUnitOfWorkManager.cs create mode 100644 Yi.Framework.Net6/src/framework/Yi.Framework.Uow/YiFrameworkUowModule.cs create mode 100644 Yi.Framework.Net6/src/project/Yi.Framework.Domain/School/DataSeeds/StudentDataSeed.cs diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Repositories/SqlsugarRepository.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Repositories/SqlsugarRepository.cs index 2d04b21d..15416ceb 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Repositories/SqlsugarRepository.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Repositories/SqlsugarRepository.cs @@ -15,7 +15,7 @@ using Yi.Framework.Ddd.Repositories; namespace Yi.Framework.Core.Sqlsugar.Repositories { - [AppService(ServiceType = typeof(IRepository<>))] + [AppService(typeof(IRepository<>))] public class SqlsugarRepository : SimpleClient, IRepository where T : class, new() { public SqlsugarRepository(ISqlSugarClient context) : base(context) @@ -95,7 +95,7 @@ namespace Yi.Framework.Core.Sqlsugar.Repositories } else { - return await _Db.Deleteable().In(id).ExecuteCommand() > 0; + return await _Db.Deleteable().In(id).ExecuteCommandAsync() > 0; } } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWork.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/SqlsugarUnitOfWork.cs similarity index 96% rename from Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWork.cs rename to Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/SqlsugarUnitOfWork.cs index 91bad224..bda2bc4a 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWork.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/SqlsugarUnitOfWork.cs @@ -12,7 +12,7 @@ using Yi.Framework.Uow; namespace Yi.Framework.Core.Sqlsugar.Uow { - public class UnitOfWork : IUnitOfWork + public class SqlsugarUnitOfWork : IUnitOfWork { public ISqlSugarClient Db { get; set; } public ITenant Tenant { get; set; } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWorkManager.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/SqlsugarUnitOfWorkManager.cs similarity index 73% rename from Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWorkManager.cs rename to Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/SqlsugarUnitOfWorkManager.cs index b235050a..17e35ae2 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWorkManager.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/SqlsugarUnitOfWorkManager.cs @@ -11,19 +11,19 @@ namespace Yi.Framework.Core.Sqlsugar.Uow /// /// 此部分为sqlsugr的魔改版本 /// - internal class UnitOfWorkManager : IUnitOfWorkManager + internal class SqlsugarUnitOfWorkManager : IUnitOfWorkManager { - public UnitOfWorkManager(ISqlSugarClient db) + public SqlsugarUnitOfWorkManager(ISqlSugarClient db) { this.Db = db; } public ISqlSugarClient Db { get; set; } public IUnitOfWork CreateContext(bool isTran = true) { - UnitOfWork uow = new UnitOfWork(); + SqlsugarUnitOfWork uow = new SqlsugarUnitOfWork(); return CreateContext(isTran, uow); } - private IUnitOfWork CreateContext(bool isTran, UnitOfWork sugarUnitOf) + private IUnitOfWork CreateContext(bool isTran, SqlsugarUnitOfWork sugarUnitOf) { sugarUnitOf.Db = Db; sugarUnitOf.Tenant = Db.AsTenant(); diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/YiFrameworkCoreSqlsugarModule.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/YiFrameworkCoreSqlsugarModule.cs index 5349b2b2..de0e3ae5 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/YiFrameworkCoreSqlsugarModule.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/YiFrameworkCoreSqlsugarModule.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using StartupModules; using Yi.Framework.Core.Configuration; using Yi.Framework.Core.Sqlsugar.Extensions; @@ -24,11 +25,8 @@ namespace Yi.Framework.Core.Sqlsugar public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context) { services.AddTransient(typeof(IRepository<>), typeof(SqlsugarRepository<>)); - - services.AddSingleton(); - - //这里替换过滤器实现 - services.AddScoped(); + services.Replace(new ServiceDescriptor(typeof(IUnitOfWorkManager), typeof(SqlsugarUnitOfWorkManager), ServiceLifetime.Singleton)); + services.Replace(new ServiceDescriptor(typeof(IDataFilter), typeof(SqlsugarDataFilter), ServiceLifetime.Scoped)); services.Configure(Appsettings.appConfiguration("DbConnOptions")); services.AddSqlsugarServer(); diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Attributes/AppServiceAttribute.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Attributes/AppServiceAttribute.cs index e1c2cd13..a398b51a 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Attributes/AppServiceAttribute.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Attributes/AppServiceAttribute.cs @@ -12,6 +12,17 @@ namespace Yi.Framework.Core.Attributes [AttributeUsage(AttributeTargets.Class, Inherited = false)] public class AppServiceAttribute : Attribute { + public AppServiceAttribute() { } + public AppServiceAttribute(Type? serviceType) + { + ServiceType=serviceType; + } + + public AppServiceAttribute(Type? serviceType, LifeTime serviceLifetime) + { + ServiceType = serviceType; + ServiceLifetime= serviceLifetime; + } /// /// 服务声明周期 /// 不给默认值的话注册的是作用域 diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/AbstractDataSeed.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/AbstractDataSeed.cs index 71eb8efc..02fe3342 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/AbstractDataSeed.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/AbstractDataSeed.cs @@ -7,7 +7,7 @@ using Yi.Framework.Ddd.Repositories; namespace Yi.Framework.Data.DataSeeds { - public abstract class AbstractDataSeed : IDataSeed + public abstract class AbstractDataSeed : IDataSeed { private readonly IRepository _repository; public AbstractDataSeed(IRepository repository) @@ -35,8 +35,12 @@ namespace Yi.Framework.Data.DataSeeds /// 这个用来处理判断是否数据库还存在数据 /// /// - public virtual bool IsInvoker() + public virtual async Task IsInvoker() { + if (await _repository.CountAsync(u => true) > 0) + { + return false; + } return true; } @@ -47,7 +51,7 @@ namespace Yi.Framework.Data.DataSeeds public async virtual Task InvokerAsync() { bool res = true; - if (IsInvoker()) + if (await IsInvoker()) { return await DataHandlerAsync(); } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/IDataSeed.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/IDataSeed.cs index 316d9904..fdf12f75 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/IDataSeed.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/IDataSeed.cs @@ -6,8 +6,8 @@ using System.Threading.Tasks; namespace Yi.Framework.Data.DataSeeds { - public interface IDataSeed + public interface IDataSeed { - bool IsInvoker(); + Task InvokerAsync(); } } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/Extensions/DataSeedExtensions.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/Extensions/DataSeedExtensions.cs index 08155d49..b8180350 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/Extensions/DataSeedExtensions.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/Extensions/DataSeedExtensions.cs @@ -5,7 +5,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Yi.Framework.Core.Configuration; using Yi.Framework.Data.DataSeeds; +using Yi.Framework.Uow; namespace Yi.Framework.Data.Extensions { @@ -13,9 +15,29 @@ namespace Yi.Framework.Data.Extensions { public static IApplicationBuilder UseDataSeedServer(this IApplicationBuilder builder) { - var dataSeeds = builder.ApplicationServices.GetServices(typeof(IDataSeed<>)); - //这个种子数据是有问题的,先放个坑 + if (!Appsettings.appBool("EnabledDataSeed")) + { + return builder; + } + var dataSeeds = builder.ApplicationServices.GetServices(); + var iUnitOfWorkManager = builder.ApplicationServices.GetRequiredService(); + if (dataSeeds is not null) + { + using (var uow = iUnitOfWorkManager.CreateContext()) + { + foreach (var seed in dataSeeds) + { + seed.InvokerAsync(); + } + var res = uow.Commit(); + + if (!res) + { + throw new ApplicationException("种子数据初始化异常"); + } + } + } return builder.UseMiddleware(); } } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/Yi.Framework.Data.csproj b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/Yi.Framework.Data.csproj index d3779abd..a711a8d9 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/Yi.Framework.Data.csproj +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/Yi.Framework.Data.csproj @@ -9,6 +9,7 @@ + diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/YiFrameworkDataModule.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/YiFrameworkDataModule.cs index b46ce65d..25647bab 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/YiFrameworkDataModule.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/YiFrameworkDataModule.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using StartupModules; using System; using System.Collections.Generic; @@ -22,12 +23,15 @@ namespace Yi.Framework.Data { //使用了过滤器 app.UseDataFiterServer(); + + //添加种子数据 + app.UseDataSeedServer(); } public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context) { //添加默认没有真正实现的 - services.AddTransient(); + services.TryAddTransient(); } } } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/DefaultUnitOfWork.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/DefaultUnitOfWork.cs new file mode 100644 index 00000000..43e957c1 --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/DefaultUnitOfWork.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Ddd.Repositories; + +namespace Yi.Framework.Uow +{ + internal class DefaultUnitOfWork : IUnitOfWork + { + public DefaultUnitOfWork() { } + public bool IsTran { get; set; } + public bool IsCommit { get; set; } + public bool IsClose { get; set; } + + public bool Commit() + { + return true; + } + + public void Dispose() + { + } + + public IRepository GetRepository() + { + throw new NotImplementedException(); + } + } +} diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/DefaultUnitOfWorkManager.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/DefaultUnitOfWorkManager.cs new file mode 100644 index 00000000..b755c7f9 --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/DefaultUnitOfWorkManager.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Uow +{ + internal class DefaultUnitOfWorkManager : IUnitOfWorkManager + { + public IUnitOfWork CreateContext(bool isTran = true) + { + return new DefaultUnitOfWork(); + } + } +} diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/YiFrameworkUowModule.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/YiFrameworkUowModule.cs new file mode 100644 index 00000000..f4ced262 --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/YiFrameworkUowModule.cs @@ -0,0 +1,29 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using StartupModules; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Core.Attributes; +using Yi.Framework.Ddd; + +namespace Yi.Framework.Uow +{ + [DependsOn( + typeof(YiFrameworkDddModule))] + public class YiFrameworkUowModule : IStartupModule + { + public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context) + { + } + + public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context) + { + services.TryAddSingleton(); + } + } +} diff --git a/Yi.Framework.Net6/src/project/Yi.Framework.Domain/School/DataSeeds/StudentDataSeed.cs b/Yi.Framework.Net6/src/project/Yi.Framework.Domain/School/DataSeeds/StudentDataSeed.cs new file mode 100644 index 00000000..7d8f70c9 --- /dev/null +++ b/Yi.Framework.Net6/src/project/Yi.Framework.Domain/School/DataSeeds/StudentDataSeed.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Data.DataSeeds; +using Yi.Framework.Ddd.Repositories; +using Yi.Framework.Domain.School.Entities; + +namespace Yi.Framework.Domain.School.DataSeeds +{ + [AppService(typeof(IDataSeed))] + public class StudentDataSeed : AbstractDataSeed + { + public StudentDataSeed(IRepository repository) : base(repository) + { + } + + public override List GetSeedData() + { + return new List() { new StudentEntity { Id = SnowflakeHelper.NextId, Name = "你好", Phone = "123", Height = 188, IsDeleted = false } , + new StudentEntity { Id = SnowflakeHelper.NextId, Name = "你好1", Phone = "123", Height = 188, IsDeleted = false }, + new StudentEntity { Id = SnowflakeHelper.NextId, Name = "你好2", Phone = "123", Height = 188, IsDeleted = false } + }; + } + } +} diff --git a/Yi.Framework.Net6/src/project/Yi.Framework.Web/TimeTest.txt b/Yi.Framework.Net6/src/project/Yi.Framework.Web/TimeTest.txt index a0295bb9..f8ac9cbf 100644 --- a/Yi.Framework.Net6/src/project/Yi.Framework.Web/TimeTest.txt +++ b/Yi.Framework.Net6/src/project/Yi.Framework.Web/TimeTest.txt @@ -84,3 +84,8 @@ 2023:01:21-22:29:30本次运行启动时间为:9099毫秒 2023:01:21-22:30:38本次运行启动时间为:2136毫秒 2023:01:21-23:15:24本次运行启动时间为:1944毫秒 +2023:01:24-13:26:35本次运行启动时间为:2712毫秒 +2023:01:24-13:39:09本次运行启动时间为:2031毫秒 +2023:01:24-13:42:17本次运行启动时间为:2087毫秒 +2023:01:24-13:44:55本次运行启动时间为:1877毫秒 +2023:01:24-13:45:55本次运行启动时间为:19905毫秒 diff --git a/Yi.Framework.Net6/src/project/Yi.Framework.Web/appsettings.json b/Yi.Framework.Net6/src/project/Yi.Framework.Web/appsettings.json index 825f32ab..35a27b42 100644 --- a/Yi.Framework.Net6/src/project/Yi.Framework.Web/appsettings.json +++ b/Yi.Framework.Net6/src/project/Yi.Framework.Web/appsettings.json @@ -27,10 +27,14 @@ ] }, + //授权 "JwtTokenOptions": { "Audience": "yi", "Issuer": "localhost:19002", "Subject": "yiframwork", "ExpSecond": 3600 - } + }, + + //开启种子数据 + "EnabledDataSeed": true } diff --git a/Yi.Framework.Net6/src/project/Yi.Framework.Web/yi-sqlsugar-dev.db b/Yi.Framework.Net6/src/project/Yi.Framework.Web/yi-sqlsugar-dev.db index e84c360009782c75d6c2e870732b20dd58729646..a53c95870263438b535135165ebe0fd57796401a 100644 GIT binary patch delta 175 zcmZojXh@hK&B!uQ#+i|2W5N=CE@u9<4E#^|Px7zbEGRIaKT(RAjZKo^6N(%oM9vT_C&|dhCW+9;0FeVraWOD3F!SGH;Qzw^oc|Wk anlt=rJj`s2Fe{jmIE+l(%xs)c83q6lf-;c+ delta 98 zcmZojXh@hK&B!!S#+i|6W5N=CHb(w?4E*;t3o4xCpBTWcqrk|