feat: Client 工程增加 DBManagerService
This commit is contained in:
parent
3f6834de5f
commit
f030cb4be9
|
@ -47,7 +47,7 @@ internal class DBManagerService : IDBManager
|
|||
option.UsingConnectionString(conn);
|
||||
|
||||
// provider
|
||||
option.UsingProvider<SqlServerDatabaseProvider>();
|
||||
option.UsingProvider<SQLiteDatabaseProvider>();
|
||||
|
||||
var db = new Database(option) { KeepConnectionAlive = keepAlive };
|
||||
|
||||
|
|
|
@ -26,46 +26,11 @@ 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.AddSingleton<DBManagerService>();
|
||||
services.TryAddSingleton<IDBManager, DBManagerService>();
|
||||
|
||||
// 增加业务服务
|
||||
services.AddSingleton<IDict, DictService>();
|
||||
|
|
|
@ -2,24 +2,32 @@
|
|||
// 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 BootstrapClient.DataAccess.PetaPoco.Services;
|
||||
|
||||
internal class DBManagerService
|
||||
internal class DBManagerService : IDBManager
|
||||
{
|
||||
private IConfiguration Configuration { get; set; }
|
||||
|
||||
private ILogger<DBManagerService> Logger { get; set; }
|
||||
|
||||
public DBManagerService(IConfiguration configuration, ILogger<DBManagerService> logger)
|
||||
private IWebHostEnvironment WebHost { get; set; }
|
||||
|
||||
public DBManagerService(IConfiguration configuration, ILogger<DBManagerService> logger, IWebHostEnvironment host)
|
||||
{
|
||||
Configuration = configuration;
|
||||
Logger = logger;
|
||||
WebHost = host;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -28,21 +36,42 @@ internal class DBManagerService
|
|||
/// <param name="connectionName">连接字符串键值</param>
|
||||
/// <param name="keepAlive"></param>
|
||||
/// <returns></returns>
|
||||
public IDatabase Create(string? connectionName = "client", bool keepAlive = false)
|
||||
public IDatabase Create(string? connectionName = "ba", bool keepAlive = false)
|
||||
{
|
||||
var conn = Configuration.GetConnectionString(connectionName) ?? throw new ArgumentNullException(nameof(connectionName));
|
||||
|
||||
var source = DatabaseConfiguration.Build();
|
||||
var option = DatabaseConfiguration.Build();
|
||||
option.UsingDefaultMapper<BootstrapAdminConventionMapper>();
|
||||
|
||||
// connectionstring
|
||||
source.UsingConnectionString(conn);
|
||||
option.UsingConnectionString(conn);
|
||||
|
||||
// provider
|
||||
source.UsingProvider<SqlServerDatabaseProvider>();
|
||||
var db = new Database(source) { KeepConnectionAlive = keepAlive };
|
||||
db.ExceptionThrown += (sender, args) =>
|
||||
option.UsingProvider<SQLiteDatabaseProvider>();
|
||||
|
||||
var db = new Database(option) { KeepConnectionAlive = keepAlive };
|
||||
|
||||
db.ExceptionThrown += (sender, e) =>
|
||||
{
|
||||
Logger.LogError(args.Exception, $"Last-Cmd: {db.LastCommand}");
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -10,15 +10,19 @@ namespace BootstrapClient.DataAccess.PetaPoco.Services;
|
|||
|
||||
class DictService : IDict
|
||||
{
|
||||
private IDatabase Database { get; }
|
||||
private IDBManager DBManager { get; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
public DictService(IDatabase db) => Database = db;
|
||||
public DictService(IDBManager db) => DBManager = db;
|
||||
|
||||
public List<Dict> GetAll() => Database.Fetch<Dict>();
|
||||
public List<Dict> GetAll()
|
||||
{
|
||||
using var db = DBManager.Create();
|
||||
return db.Fetch<Dict>();
|
||||
}
|
||||
|
||||
public bool IsDemo()
|
||||
{
|
||||
|
|
|
@ -10,15 +10,15 @@ namespace BootstrapClient.DataAccess.PetaPoco.Services;
|
|||
|
||||
internal class DummyService : IDummy
|
||||
{
|
||||
private IDatabase Database { get; }
|
||||
private IDBManager DBManager { get; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
public DummyService(DBManagerService db)
|
||||
public DummyService(IDBManager db)
|
||||
{
|
||||
Database = db.Create();
|
||||
DBManager = db;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -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 BootstrapClient.DataAccess.PetaPoco.Services;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public interface IDBManager
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IDatabase Create(string? connectionName = "ba", bool keepAlive = false);
|
||||
}
|
|
@ -13,13 +13,13 @@ namespace BootstrapClient.DataAccess.PetaPoco.Services;
|
|||
/// </summary>
|
||||
class NavigationService : INavigation
|
||||
{
|
||||
private IDatabase Database { get; }
|
||||
private IDBManager DBManager { get; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
public NavigationService(IDatabase db) => Database = db;
|
||||
public NavigationService(IDBManager db) => DBManager = db;
|
||||
|
||||
/// <summary>
|
||||
/// 获得指定用户名可访问的所有菜单集合
|
||||
|
@ -28,7 +28,8 @@ class NavigationService : INavigation
|
|||
/// <returns>未层次化的菜单集合</returns>
|
||||
public List<Navigation> GetMenus(string userName)
|
||||
{
|
||||
var order = Database.Provider.EscapeSqlIdentifier("Order");
|
||||
return Database.Fetch<Navigation>($"select n.ID, n.ParentId, n.Name, n.{order}, n.Icon, n.Url, n.Category, n.Target, n.IsResource, n.Application from Navigations n inner join (select nr.NavigationID from Users u inner join UserRole ur on ur.UserID = u.ID inner join NavigationRole nr on nr.RoleID = ur.RoleID where u.UserName = @UserName union select nr.NavigationID from Users u inner join UserGroup ug on u.ID = ug.UserID inner join RoleGroup rg on rg.GroupID = ug.GroupID inner join NavigationRole nr on nr.RoleID = rg.RoleID where u.UserName = @UserName union select n.ID from Navigations n where EXISTS (select UserName from Users u inner join UserRole ur on u.ID = ur.UserID inner join Roles r on ur.RoleID = r.ID where u.UserName = @UserName and r.RoleName = 'Administrators')) nav on n.ID = nav.NavigationID Where n.Category = '1' ORDER BY n.Application, n.{order}", new { UserName = userName });
|
||||
using var db = DBManager.Create ();
|
||||
var order = db.Provider.EscapeSqlIdentifier("Order");
|
||||
return db.Fetch<Navigation>($"select n.ID, n.ParentId, n.Name, n.{order}, n.Icon, n.Url, n.Category, n.Target, n.IsResource, n.Application from Navigations n inner join (select nr.NavigationID from Users u inner join UserRole ur on ur.UserID = u.ID inner join NavigationRole nr on nr.RoleID = ur.RoleID where u.UserName = @UserName union select nr.NavigationID from Users u inner join UserGroup ug on u.ID = ug.UserID inner join RoleGroup rg on rg.GroupID = ug.GroupID inner join NavigationRole nr on nr.RoleID = rg.RoleID where u.UserName = @UserName union select n.ID from Navigations n where EXISTS (select UserName from Users u inner join UserRole ur on u.ID = ur.UserID inner join Roles r on ur.RoleID = r.ID where u.UserName = @UserName and r.RoleName = 'Administrators')) nav on n.ID = nav.NavigationID Where n.Category = '1' ORDER BY n.Application, n.{order}", new { UserName = userName });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,32 +10,49 @@ namespace BootstrapClient.DataAccess.PetaPoco.Services;
|
|||
|
||||
class UserService : IUser
|
||||
{
|
||||
private IDatabase Database { get; }
|
||||
private IDBManager DBManager { get; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
public UserService(IDatabase db) => Database = db;
|
||||
public UserService(IDBManager db) => DBManager = db;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="userName"></param>
|
||||
/// <returns></returns>
|
||||
public User? GetUserByUserName(string? userName) => string.IsNullOrEmpty(userName) ? null : Database.FirstOrDefault<User>("Where UserName = @0", userName);
|
||||
public User? GetUserByUserName(string? userName)
|
||||
{
|
||||
User? user = null;
|
||||
if (!string.IsNullOrEmpty(userName))
|
||||
{
|
||||
using var db = DBManager.Create();
|
||||
user = db.FirstOrDefault<User>("Where UserName = @0", userName);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="userName"></param>
|
||||
/// <returns></returns>
|
||||
public List<string> GetApps(string userName) => Database.Fetch<string>($"select d.Code from Dicts d inner join RoleApp ra on d.Code = ra.AppId inner join (select r.Id from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 union select r.Id from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0) r on ra.RoleId = r.ID union select Code from Dicts where Category = @1 and exists(select r.ID from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 and r.RoleName = @2 union select r.ID from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0 and r.RoleName = @2)", userName, "应用程序", "Administrators");
|
||||
public List<string> GetApps(string userName)
|
||||
{
|
||||
using var db = DBManager.Create();
|
||||
return db.Fetch<string>($"select d.Code from Dicts d inner join RoleApp ra on d.Code = ra.AppId inner join (select r.Id from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 union select r.Id from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {db.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0) r on ra.RoleId = r.ID union select Code from Dicts where Category = @1 and exists(select r.ID from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 and r.RoleName = @2 union select r.ID from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {db.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0 and r.RoleName = @2)", userName, "应用程序", "Administrators");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="userName"></param>
|
||||
/// <returns></returns>
|
||||
public List<string> GetRoles(string userName) => Database.Fetch<string>($"select r.RoleName from Roles r inner join UserRole ur on r.ID=ur.RoleID inner join Users u on ur.UserID = u.ID and u.UserName = @0 union select r.RoleName from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID and u.UserName=@0", userName);
|
||||
public List<string> GetRoles(string userName)
|
||||
{
|
||||
using var db = DBManager.Create();
|
||||
return db.Fetch<string>($"select r.RoleName from Roles r inner join UserRole ur on r.ID=ur.RoleID inner join Users u on ur.UserID = u.ID and u.UserName = @0 union select r.RoleName from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {db.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID and u.UserName=@0", userName);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,13 +33,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
services.AddScoped<BootstrapAppContext>();
|
||||
|
||||
// 增加 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