完成种子数据模块,完成服务替换模块
This commit is contained in:
@@ -15,7 +15,7 @@ using Yi.Framework.Ddd.Repositories;
|
|||||||
|
|
||||||
namespace Yi.Framework.Core.Sqlsugar.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 class SqlsugarRepository<T> : SimpleClient<T>, IRepository<T> where T : class, new()
|
||||||
{
|
{
|
||||||
public SqlsugarRepository(ISqlSugarClient context) : base(context)
|
public SqlsugarRepository(ISqlSugarClient context) : base(context)
|
||||||
@@ -95,7 +95,7 @@ namespace Yi.Framework.Core.Sqlsugar.Repositories
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return await _Db.Deleteable<T>().In(id).ExecuteCommand() > 0;
|
return await _Db.Deleteable<T>().In(id).ExecuteCommandAsync() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ using Yi.Framework.Uow;
|
|||||||
|
|
||||||
namespace Yi.Framework.Core.Sqlsugar.Uow
|
namespace Yi.Framework.Core.Sqlsugar.Uow
|
||||||
{
|
{
|
||||||
public class UnitOfWork : IUnitOfWork
|
public class SqlsugarUnitOfWork : IUnitOfWork
|
||||||
{
|
{
|
||||||
public ISqlSugarClient Db { get; set; }
|
public ISqlSugarClient Db { get; set; }
|
||||||
public ITenant Tenant { get; set; }
|
public ITenant Tenant { get; set; }
|
||||||
@@ -11,19 +11,19 @@ namespace Yi.Framework.Core.Sqlsugar.Uow
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 此部分为sqlsugr的魔改版本
|
/// 此部分为sqlsugr的魔改版本
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class UnitOfWorkManager : IUnitOfWorkManager
|
internal class SqlsugarUnitOfWorkManager : IUnitOfWorkManager
|
||||||
{
|
{
|
||||||
public UnitOfWorkManager(ISqlSugarClient db)
|
public SqlsugarUnitOfWorkManager(ISqlSugarClient db)
|
||||||
{
|
{
|
||||||
this.Db = db;
|
this.Db = db;
|
||||||
}
|
}
|
||||||
public ISqlSugarClient Db { get; set; }
|
public ISqlSugarClient Db { get; set; }
|
||||||
public IUnitOfWork CreateContext(bool isTran = true)
|
public IUnitOfWork CreateContext(bool isTran = true)
|
||||||
{
|
{
|
||||||
UnitOfWork uow = new UnitOfWork();
|
SqlsugarUnitOfWork uow = new SqlsugarUnitOfWork();
|
||||||
return CreateContext(isTran, uow);
|
return CreateContext(isTran, uow);
|
||||||
}
|
}
|
||||||
private IUnitOfWork CreateContext(bool isTran, UnitOfWork sugarUnitOf)
|
private IUnitOfWork CreateContext(bool isTran, SqlsugarUnitOfWork sugarUnitOf)
|
||||||
{
|
{
|
||||||
sugarUnitOf.Db = Db;
|
sugarUnitOf.Db = Db;
|
||||||
sugarUnitOf.Tenant = Db.AsTenant();
|
sugarUnitOf.Tenant = Db.AsTenant();
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
using StartupModules;
|
using StartupModules;
|
||||||
using Yi.Framework.Core.Configuration;
|
using Yi.Framework.Core.Configuration;
|
||||||
using Yi.Framework.Core.Sqlsugar.Extensions;
|
using Yi.Framework.Core.Sqlsugar.Extensions;
|
||||||
@@ -24,11 +25,8 @@ namespace Yi.Framework.Core.Sqlsugar
|
|||||||
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
||||||
{
|
{
|
||||||
services.AddTransient(typeof(IRepository<>), typeof(SqlsugarRepository<>));
|
services.AddTransient(typeof(IRepository<>), typeof(SqlsugarRepository<>));
|
||||||
|
services.Replace(new ServiceDescriptor(typeof(IUnitOfWorkManager), typeof(SqlsugarUnitOfWorkManager), ServiceLifetime.Singleton));
|
||||||
services.AddSingleton<IUnitOfWorkManager, UnitOfWorkManager>();
|
services.Replace(new ServiceDescriptor(typeof(IDataFilter), typeof(SqlsugarDataFilter), ServiceLifetime.Scoped));
|
||||||
|
|
||||||
//这里替换过滤器实现
|
|
||||||
services.AddScoped<IDataFilter, SqlsugarDataFilter>();
|
|
||||||
services.Configure<DbConnOptions>(Appsettings.appConfiguration("DbConnOptions"));
|
services.Configure<DbConnOptions>(Appsettings.appConfiguration("DbConnOptions"));
|
||||||
services.AddSqlsugarServer();
|
services.AddSqlsugarServer();
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,17 @@ namespace Yi.Framework.Core.Attributes
|
|||||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
||||||
public class AppServiceAttribute : Attribute
|
public class AppServiceAttribute : Attribute
|
||||||
{
|
{
|
||||||
|
public AppServiceAttribute() { }
|
||||||
|
public AppServiceAttribute(Type? serviceType)
|
||||||
|
{
|
||||||
|
ServiceType=serviceType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AppServiceAttribute(Type? serviceType, LifeTime serviceLifetime)
|
||||||
|
{
|
||||||
|
ServiceType = serviceType;
|
||||||
|
ServiceLifetime= serviceLifetime;
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 服务声明周期
|
/// 服务声明周期
|
||||||
/// 不给默认值的话注册的是作用域
|
/// 不给默认值的话注册的是作用域
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ using Yi.Framework.Ddd.Repositories;
|
|||||||
|
|
||||||
namespace Yi.Framework.Data.DataSeeds
|
namespace Yi.Framework.Data.DataSeeds
|
||||||
{
|
{
|
||||||
public abstract class AbstractDataSeed<TEntity> : IDataSeed<TEntity>
|
public abstract class AbstractDataSeed<TEntity> : IDataSeed
|
||||||
{
|
{
|
||||||
private readonly IRepository<TEntity> _repository;
|
private readonly IRepository<TEntity> _repository;
|
||||||
public AbstractDataSeed(IRepository<TEntity> repository)
|
public AbstractDataSeed(IRepository<TEntity> repository)
|
||||||
@@ -35,8 +35,12 @@ namespace Yi.Framework.Data.DataSeeds
|
|||||||
/// 这个用来处理判断是否数据库还存在数据
|
/// 这个用来处理判断是否数据库还存在数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public virtual bool IsInvoker()
|
public virtual async Task<bool> IsInvoker()
|
||||||
{
|
{
|
||||||
|
if (await _repository.CountAsync(u => true) > 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +51,7 @@ namespace Yi.Framework.Data.DataSeeds
|
|||||||
public async virtual Task<bool> InvokerAsync()
|
public async virtual Task<bool> InvokerAsync()
|
||||||
{
|
{
|
||||||
bool res = true;
|
bool res = true;
|
||||||
if (IsInvoker())
|
if (await IsInvoker())
|
||||||
{
|
{
|
||||||
return await DataHandlerAsync();
|
return await DataHandlerAsync();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace Yi.Framework.Data.DataSeeds
|
namespace Yi.Framework.Data.DataSeeds
|
||||||
{
|
{
|
||||||
public interface IDataSeed<TEntity>
|
public interface IDataSeed
|
||||||
{
|
{
|
||||||
bool IsInvoker();
|
Task<bool> InvokerAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Yi.Framework.Core.Configuration;
|
||||||
using Yi.Framework.Data.DataSeeds;
|
using Yi.Framework.Data.DataSeeds;
|
||||||
|
using Yi.Framework.Uow;
|
||||||
|
|
||||||
namespace Yi.Framework.Data.Extensions
|
namespace Yi.Framework.Data.Extensions
|
||||||
{
|
{
|
||||||
@@ -13,9 +15,29 @@ namespace Yi.Framework.Data.Extensions
|
|||||||
{
|
{
|
||||||
public static IApplicationBuilder UseDataSeedServer(this IApplicationBuilder builder)
|
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>();
|
return builder.UseMiddleware<DataFilterMiddleware>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
|
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
|
||||||
<ProjectReference Include="..\Yi.Framework.Ddd\Yi.Framework.Ddd.csproj" />
|
<ProjectReference Include="..\Yi.Framework.Ddd\Yi.Framework.Ddd.csproj" />
|
||||||
|
<ProjectReference Include="..\Yi.Framework.Uow\Yi.Framework.Uow.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
using StartupModules;
|
using StartupModules;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -22,12 +23,15 @@ namespace Yi.Framework.Data
|
|||||||
{
|
{
|
||||||
//使用了过滤器
|
//使用了过滤器
|
||||||
app.UseDataFiterServer();
|
app.UseDataFiterServer();
|
||||||
|
|
||||||
|
//添加种子数据
|
||||||
|
app.UseDataSeedServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
||||||
{
|
{
|
||||||
//添加默认没有真正实现的
|
//添加默认没有真正实现的
|
||||||
services.AddTransient<IDataFilter, DefaultDataFilter>();
|
services.TryAddTransient<IDataFilter, DefaultDataFilter>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -84,3 +84,8 @@
|
|||||||
2023:01:21-22:29:30本次运行启动时间为:9099毫秒
|
2023:01:21-22:29:30本次运行启动时间为:9099毫秒
|
||||||
2023:01:21-22:30:38本次运行启动时间为:2136毫秒
|
2023:01:21-22:30:38本次运行启动时间为:2136毫秒
|
||||||
2023:01:21-23:15:24本次运行启动时间为:1944毫秒
|
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毫秒
|
||||||
|
|||||||
@@ -27,10 +27,14 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
|
//授权
|
||||||
"JwtTokenOptions": {
|
"JwtTokenOptions": {
|
||||||
"Audience": "yi",
|
"Audience": "yi",
|
||||||
"Issuer": "localhost:19002",
|
"Issuer": "localhost:19002",
|
||||||
"Subject": "yiframwork",
|
"Subject": "yiframwork",
|
||||||
"ExpSecond": 3600
|
"ExpSecond": 3600
|
||||||
}
|
},
|
||||||
|
|
||||||
|
//开启种子数据
|
||||||
|
"EnabledDataSeed": true
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user