refactor: 优化 PetaPoco 数据服务接口

This commit is contained in:
Argo-Lenovo 2022-01-07 01:14:06 +08:00
parent 913f4db051
commit 15007cd2fc
2 changed files with 14 additions and 10 deletions

View File

@ -26,14 +26,14 @@ public static class ServicesExtensions
/// </summary> /// </summary>
/// <param name="services"></param> /// <param name="services"></param>
/// <returns></returns> /// <returns></returns>
public static IServiceCollection AddPetaPocoDataAccessServices(this IServiceCollection services) public static IServiceCollection AddPetaPocoDataAccessServices(this IServiceCollection services, Action<IServiceProvider, IDatabaseBuildConfiguration> builder)
{ {
services.TryAddSingleton<IDatabase>(provider => services.TryAddSingleton<IDatabase>(provider =>
{ {
//TODO: 后期改造成自定适配 var option = DatabaseConfiguration.Build();
var configuration = provider.GetRequiredService<IConfiguration>(); builder(provider, option);
var connString = configuration.GetConnectionString("bb"); option.UsingDefaultMapper<BootstrapAdminConventionMapper>();
var db = new Database<SQLiteDatabaseProvider>(connString, new BootstrapAdminConventionMapper()); var db = new Database(option);
var logger = provider.GetRequiredService<ILogger<Database>>(); var logger = provider.GetRequiredService<ILogger<Database>>();
db.ExceptionThrown += (sender, e) => db.ExceptionThrown += (sender, e) =>

View File

@ -3,10 +3,8 @@ using BootstrapAdmin.Web.Services;
using BootstrapAdmin.Web.Services.SMS; using BootstrapAdmin.Web.Services.SMS;
using BootstrapAdmin.Web.Services.SMS.Tencent; using BootstrapAdmin.Web.Services.SMS.Tencent;
using BootstrapAdmin.Web.Utils; using BootstrapAdmin.Web.Utils;
//using Microsoft.EntityFrameworkCore; using PetaPoco;
using System.Text; using PetaPoco.Providers;
using System.Text.Encodings.Web;
using System.Text.Unicode;
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
{ {
@ -49,7 +47,13 @@ namespace Microsoft.Extensions.DependencyInjection
//}); //});
// 增加 PetaPoco 数据服务 // 增加 PetaPoco 数据服务
services.AddPetaPocoDataAccessServices(); services.AddPetaPocoDataAccessServices((provider, builder) =>
{
var configuration = provider.GetRequiredService<IConfiguration>();
var connString = configuration.GetConnectionString("bb");
builder.UsingProvider<SQLiteDatabaseProvider>()
.UsingConnectionString(connString);
});
// 增加后台任务 // 增加后台任务
services.AddTaskServices(); services.AddTaskServices();