完成种子数据模块,完成服务替换模块

This commit is contained in:
橙子
2023-01-24 13:46:22 +08:00
parent aea8b55e82
commit 4ca7b2e023
17 changed files with 173 additions and 21 deletions

View File

@@ -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<T> : SimpleClient<T>, IRepository<T> 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<T>().In(id).ExecuteCommand() > 0;
return await _Db.Deleteable<T>().In(id).ExecuteCommandAsync() > 0;
}
}

View File

@@ -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; }

View File

@@ -11,19 +11,19 @@ namespace Yi.Framework.Core.Sqlsugar.Uow
/// <summary>
/// 此部分为sqlsugr的魔改版本
/// </summary>
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();

View File

@@ -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<IUnitOfWorkManager, UnitOfWorkManager>();
//这里替换过滤器实现
services.AddScoped<IDataFilter, SqlsugarDataFilter>();
services.Replace(new ServiceDescriptor(typeof(IUnitOfWorkManager), typeof(SqlsugarUnitOfWorkManager), ServiceLifetime.Singleton));
services.Replace(new ServiceDescriptor(typeof(IDataFilter), typeof(SqlsugarDataFilter), ServiceLifetime.Scoped));
services.Configure<DbConnOptions>(Appsettings.appConfiguration("DbConnOptions"));
services.AddSqlsugarServer();

View File

@@ -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;
}
/// <summary>
/// 服务声明周期
/// 不给默认值的话注册的是作用域

View File

@@ -7,7 +7,7 @@ using Yi.Framework.Ddd.Repositories;
namespace Yi.Framework.Data.DataSeeds
{
public abstract class AbstractDataSeed<TEntity> : IDataSeed<TEntity>
public abstract class AbstractDataSeed<TEntity> : IDataSeed
{
private readonly IRepository<TEntity> _repository;
public AbstractDataSeed(IRepository<TEntity> repository)
@@ -35,8 +35,12 @@ namespace Yi.Framework.Data.DataSeeds
/// 这个用来处理判断是否数据库还存在数据
/// </summary>
/// <returns></returns>
public virtual bool IsInvoker()
public virtual async Task<bool> 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<bool> InvokerAsync()
{
bool res = true;
if (IsInvoker())
if (await IsInvoker())
{
return await DataHandlerAsync();
}

View File

@@ -6,8 +6,8 @@ using System.Threading.Tasks;
namespace Yi.Framework.Data.DataSeeds
{
public interface IDataSeed<TEntity>
public interface IDataSeed
{
bool IsInvoker();
Task<bool> InvokerAsync();
}
}

View File

@@ -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<IDataSeed>();
var iUnitOfWorkManager = builder.ApplicationServices.GetRequiredService<IUnitOfWorkManager>();
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<DataFilterMiddleware>();
}
}

View File

@@ -9,6 +9,7 @@
<ItemGroup>
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
<ProjectReference Include="..\Yi.Framework.Ddd\Yi.Framework.Ddd.csproj" />
<ProjectReference Include="..\Yi.Framework.Uow\Yi.Framework.Uow.csproj" />
</ItemGroup>
</Project>

View File

@@ -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<IDataFilter, DefaultDataFilter>();
services.TryAddTransient<IDataFilter, DefaultDataFilter>();
}
}
}

View File

@@ -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<T> GetRepository<T>()
{
throw new NotImplementedException();
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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<IUnitOfWorkManager, DefaultUnitOfWorkManager>();
}
}
}

View File

@@ -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<StudentEntity>
{
public StudentDataSeed(IRepository<StudentEntity> repository) : base(repository)
{
}
public override List<StudentEntity> GetSeedData()
{
return new List<StudentEntity>() { 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 }
};
}
}
}

View File

@@ -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毫秒

View File

@@ -27,10 +27,14 @@
]
},
//授权
"JwtTokenOptions": {
"Audience": "yi",
"Issuer": "localhost:19002",
"Subject": "yiframwork",
"ExpSecond": 3600
}
},
//开启种子数据
"EnabledDataSeed": true
}