feat: 增加 IDBManager 接口负责维护 IDatabase
This commit is contained in:
parent
f7b2c059ef
commit
2481a4a577
|
@ -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
|
|||
///
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="builder"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddPetaPocoDataAccessServices(this IServiceCollection services, Action<IServiceProvider, IDatabaseBuildConfiguration> builder)
|
||||
public static IServiceCollection AddPetaPocoDataAccessServices(this IServiceCollection services)
|
||||
{
|
||||
services.TryAddSingleton<IDatabase>(provider =>
|
||||
{
|
||||
var option = DatabaseConfiguration.Build();
|
||||
builder(provider, option);
|
||||
option.UsingDefaultMapper<BootstrapAdminConventionMapper>();
|
||||
var db = new Database(option);
|
||||
|
||||
var logger = provider.GetRequiredService<ILogger<Database>>();
|
||||
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<IWebHostEnvironment>();
|
||||
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<IDBManager, DBManagerService>();
|
||||
|
||||
// 增加数据服务
|
||||
services.AddSingleton(typeof(IDataService<>), typeof(DefaultDataService<>));
|
||||
|
|
|
@ -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<DBManagerService> Logger { get; set; }
|
||||
|
||||
private IWebHostEnvironment WebHost { get; set; }
|
||||
|
||||
public DBManagerService(IConfiguration configuration, ILogger<DBManagerService> logger, IWebHostEnvironment host)
|
||||
{
|
||||
Configuration = configuration;
|
||||
Logger = logger;
|
||||
WebHost = host;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建 IDatabase 实例方法
|
||||
/// </summary>
|
||||
/// <param name="connectionName">连接字符串键值</param>
|
||||
/// <param name="keepAlive"></param>
|
||||
/// <returns></returns>
|
||||
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<BootstrapAdminConventionMapper>();
|
||||
|
||||
// connectionstring
|
||||
option.UsingConnectionString(conn);
|
||||
|
||||
// provider
|
||||
option.UsingProvider<SqlServerDatabaseProvider>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public interface IDBManager
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IDatabase Create(string? connectionName = "ba", bool keepAlive = false);
|
||||
}
|
|
@ -73,13 +73,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
// });
|
||||
|
||||
// 增加 PetaPoco 数据服务
|
||||
services.AddPetaPocoDataAccessServices((provider, builder) =>
|
||||
{
|
||||
var configuration = provider.GetRequiredService<IConfiguration>();
|
||||
var connString = configuration.GetConnectionString("ba");
|
||||
builder.UsingProvider<SQLiteDatabaseProvider>()
|
||||
.UsingConnectionString(connString);
|
||||
});
|
||||
services.AddPetaPocoDataAccessServices();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue