feat: 增加 SqlSugar ORM 示例
This commit is contained in:
parent
81ce2b70e0
commit
cf164c57c7
|
@ -157,6 +157,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BootstrapClient.Web", "src\
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BootStarpAdmin.DataAccess.FreeSql", "src\blazor\admin\BootStarpAdmin.DataAccess.FreeSql\BootStarpAdmin.DataAccess.FreeSql.csproj", "{11122D97-B349-4A3E-B7DD-73B8B363C47C}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootStarpAdmin.DataAccess.SqlSugar", "src\blazor\admin\BootStarpAdmin.DataAccess.SqlSugar\BootStarpAdmin.DataAccess.SqlSugar.csproj", "{1D20E6CF-9825-4CDE-B732-AE586BD1AABA}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -227,6 +229,10 @@ Global
|
|||
{11122D97-B349-4A3E-B7DD-73B8B363C47C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{11122D97-B349-4A3E-B7DD-73B8B363C47C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{11122D97-B349-4A3E-B7DD-73B8B363C47C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{1D20E6CF-9825-4CDE-B732-AE586BD1AABA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{1D20E6CF-9825-4CDE-B732-AE586BD1AABA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1D20E6CF-9825-4CDE-B732-AE586BD1AABA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1D20E6CF-9825-4CDE-B732-AE586BD1AABA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -265,6 +271,7 @@ Global
|
|||
{93770088-3463-427B-9CD8-88B8D7945C83} = {55A2459A-6BDE-4493-B2C0-5BE1673E99EE}
|
||||
{6CD7A35B-93A8-4DB2-B078-EE5A81F40032} = {55A2459A-6BDE-4493-B2C0-5BE1673E99EE}
|
||||
{11122D97-B349-4A3E-B7DD-73B8B363C47C} = {45ADEF9B-C8BD-4224-9E12-F6716E85A22C}
|
||||
{1D20E6CF-9825-4CDE-B732-AE586BD1AABA} = {45ADEF9B-C8BD-4224-9E12-F6716E85A22C}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {221EAE38-5F75-4391-9A48-E462A9F3B8FC}
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BootstrapBlazor" Version="6.2.7-beta02" />
|
||||
<PackageReference Include="SqlSugar" Version="5.0.5.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BootstrapAdmin.Web.Core\BootstrapAdmin.Web.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,43 @@
|
|||
using BootStarpAdmin.DataAccess.SqlSugar.Service;
|
||||
using BootstrapBlazor.Components;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
/// <summary>
|
||||
/// FreeSql ORM 注入服务扩展类
|
||||
/// </summary>
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 注入 FreeSql 数据服务类
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="sqlSugarBuilder"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddSqlSugar(this IServiceCollection services, Action<IServiceProvider, ConnectionConfig> sqlSugarBuilder)
|
||||
{
|
||||
services.TryAddSingleton<ISqlSugarClient>(provider =>
|
||||
{
|
||||
var builder = new ConnectionConfig();
|
||||
builder.IsAutoCloseConnection = true;
|
||||
sqlSugarBuilder(provider, builder);
|
||||
return new SqlSugarClient(builder);
|
||||
});
|
||||
|
||||
// 增加数据服务
|
||||
services.AddSingleton(typeof(IDataService<>), typeof(DefaultDataService<>));
|
||||
|
||||
// 增加业务服务
|
||||
//services.AddSingleton<IApp, AppService>();
|
||||
//services.AddSingleton<IDict, DictService>();
|
||||
//services.AddSingleton<IException, ExceptionService>();
|
||||
//services.AddSingleton<IGroup, GroupService>();
|
||||
//services.AddSingleton<ILogin, LoginService>();
|
||||
//services.AddSingleton<INavigation, NavigationService>();
|
||||
//services.AddSingleton<IRole, RoleService>();
|
||||
//services.AddSingleton<IUser, UserService>();
|
||||
return services;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
namespace BootStarpAdmin.DataAccess.SqlSugar.Extensions;
|
||||
|
||||
static class SqlSugarExtensions
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
using BootstrapBlazor.Components;
|
||||
using SqlSugar;
|
||||
|
||||
namespace BootStarpAdmin.DataAccess.SqlSugar.Service;
|
||||
|
||||
class DefaultDataService<TModel> : DataServiceBase<TModel> where TModel : class, new()
|
||||
{
|
||||
private ISqlSugarClient Client { get; }
|
||||
|
||||
public DefaultDataService(ISqlSugarClient client) => Client = client;
|
||||
|
||||
/// <summary>
|
||||
/// 删除方法
|
||||
/// </summary>
|
||||
/// <param name="models"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<bool> DeleteAsync(IEnumerable<TModel> models)
|
||||
{
|
||||
await Client.Deleteable<TModel>(models).ExecuteCommandAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存方法
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="changedType"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<bool> SaveAsync(TModel model, ItemChangedType changedType)
|
||||
{
|
||||
if (changedType == ItemChangedType.Add)
|
||||
{
|
||||
await Client.Insertable<TModel>(model).ExecuteCommandAsync();
|
||||
}
|
||||
else if (changedType == ItemChangedType.Update)
|
||||
{
|
||||
await Client.Updateable<TModel>(model).ExecuteCommandAsync();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public override Task<QueryData<TModel>> QueryAsync(QueryPageOptions option)
|
||||
{
|
||||
var ret = new QueryData<TModel>()
|
||||
{
|
||||
IsSorted = true,
|
||||
IsFiltered = true,
|
||||
IsSearch = true,
|
||||
IsAdvanceSearch = option.AdvanceSearchs.Any() || option.CustomerSearchs.Any()
|
||||
};
|
||||
if (option.IsPage)
|
||||
{
|
||||
var count = 0;
|
||||
ret.Items = Client.Queryable<TModel>()
|
||||
.ToPageList(option.PageIndex, option.PageItems, ref count);
|
||||
ret.TotalCount = count;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret.Items = Client.Queryable<TModel>()
|
||||
.ToList();
|
||||
}
|
||||
return Task.FromResult(ret);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue