diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServiceCollectionExtensions.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServiceCollectionExtensions.cs index 6b04f6d2..c21c108d 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServiceCollectionExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServiceCollectionExtensions.cs @@ -6,7 +6,6 @@ using BootstrapAdmin.DataAccess.PetaPoco; using BootstrapAdmin.DataAccess.PetaPoco.Services; using BootstrapAdmin.Web.Core; using BootstrapBlazor.Components; -using BootstrapBlazor.DataAcces.PetaPoco.Services; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; @@ -27,43 +26,10 @@ public static class ServiceCollectionExtensions /// /// /// - /// /// - public static IServiceCollection AddPetaPocoDataAccessServices(this IServiceCollection services, Action builder) + public static IServiceCollection AddPetaPocoDataAccessServices(this IServiceCollection services) { - services.TryAddSingleton(provider => - { - var option = DatabaseConfiguration.Build(); - builder(provider, option); - option.UsingDefaultMapper(); - var db = new Database(option); - - var logger = provider.GetRequiredService>(); - db.ExceptionThrown += (sender, e) => - { - var message = e.Exception.Format(new NameValueCollection() - { - [nameof(db.LastCommand)] = db.LastCommand, - [nameof(db.LastArgs)] = string.Join(",", db.LastArgs) - }); - logger.LogError(new EventId(1001, "GlobalException"), e.Exception, message); - }; - var env = provider.GetRequiredService(); - if (env.IsDevelopment()) - { - db.CommandExecuted += (sender, args) => - { - var parameters = new StringBuilder(); - foreach (DbParameter p in args.Command.Parameters) - { - parameters.AppendFormat("{0}: {1} ", p.ParameterName, p.Value); - } - logger.LogInformation(args.Command.CommandText); - logger.LogInformation(parameters.ToString()); - }; - }; - return db; - }); + services.TryAddSingleton(); // 增加数据服务 services.AddSingleton(typeof(IDataService<>), typeof(DefaultDataService<>)); diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DBManagerService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DBManagerService.cs new file mode 100644 index 00000000..5cc5f760 --- /dev/null +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DBManagerService.cs @@ -0,0 +1,78 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information. +// Website: https://admin.blazor.zone + +using BootstrapBlazor.Components; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using PetaPoco; +using PetaPoco.Providers; +using System.Collections.Specialized; +using System.Data.Common; +using System.Text; + +namespace BootstrapAdmin.DataAccess.PetaPoco.Services; + +internal class DBManagerService : IDBManager +{ + private IConfiguration Configuration { get; set; } + + private ILogger Logger { get; set; } + + private IWebHostEnvironment WebHost { get; set; } + + public DBManagerService(IConfiguration configuration, ILogger logger, IWebHostEnvironment host) + { + Configuration = configuration; + Logger = logger; + WebHost = host; + } + + /// + /// 创建 IDatabase 实例方法 + /// + /// 连接字符串键值 + /// + /// + public IDatabase Create(string? connectionName = "client", bool keepAlive = false) + { + var conn = Configuration.GetConnectionString(connectionName) ?? throw new ArgumentNullException(nameof(connectionName)); + + var option = DatabaseConfiguration.Build(); + option.UsingDefaultMapper(); + + // connectionstring + option.UsingConnectionString(conn); + + // provider + option.UsingProvider(); + + var db = new Database(option) { KeepConnectionAlive = keepAlive }; + + db.ExceptionThrown += (sender, e) => + { + var message = e.Exception.Format(new NameValueCollection() + { + [nameof(db.LastCommand)] = db.LastCommand, + [nameof(db.LastArgs)] = string.Join(",", db.LastArgs) + }); + Logger.LogError(new EventId(1001, "GlobalException"), e.Exception, message); + }; + if (WebHost.IsDevelopment()) + { + db.CommandExecuted += (sender, args) => + { + var parameters = new StringBuilder(); + foreach (DbParameter p in args.Command.Parameters) + { + parameters.AppendFormat("{0}: {1} ", p.ParameterName, p.Value); + } + Logger.LogInformation(args.Command.CommandText); + Logger.LogInformation(parameters.ToString()); + }; + }; + return db; + } +} diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/IDBManager.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/IDBManager.cs new file mode 100644 index 00000000..d89f8e60 --- /dev/null +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/IDBManager.cs @@ -0,0 +1,19 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information. +// Website: https://admin.blazor.zone + +using PetaPoco; + +namespace BootstrapAdmin.DataAccess.PetaPoco.Services; + +/// +/// +/// +public interface IDBManager +{ + /// + /// + /// + /// + IDatabase Create(string? connectionName = "ba", bool keepAlive = false); +} diff --git a/src/blazor/admin/BootstrapAdmin.Web/Extensions/ServiceCollectionExtensions.cs b/src/blazor/admin/BootstrapAdmin.Web/Extensions/ServiceCollectionExtensions.cs index fa92ef03..d6446760 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Extensions/ServiceCollectionExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Extensions/ServiceCollectionExtensions.cs @@ -73,13 +73,7 @@ namespace Microsoft.Extensions.DependencyInjection // }); // 增加 PetaPoco 数据服务 - services.AddPetaPocoDataAccessServices((provider, builder) => - { - var configuration = provider.GetRequiredService(); - var connString = configuration.GetConnectionString("ba"); - builder.UsingProvider() - .UsingConnectionString(connString); - }); + services.AddPetaPocoDataAccessServices(); return services; }