feat(DataService): add SqlSugar support (#3333)

* chore: 更新文档注释

* chore: bump version 8.0.0

Co-authored-by:  wen lin <qdingwen@live.com>
This commit is contained in:
Argo Zhang 2024-04-23 17:09:42 +09:00 committed by GitHub
parent 366d5f86e3
commit fe4b8bfa27
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 140 additions and 14 deletions

View File

@ -132,7 +132,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BootstrapBlazor.Maui", "src
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "maui", "maui", "{81615751-490B-4B50-8029-C79846F592BB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapBlazor.TagHelper", "src\Extensions\Components\BootstrapBlazor.TagHelper\BootstrapBlazor.TagHelper.csproj", "{65CEB170-D561-4878-80E4-625538590D29}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BootstrapBlazor.TagHelper", "src\Extensions\Components\BootstrapBlazor.TagHelper\BootstrapBlazor.TagHelper.csproj", "{65CEB170-D561-4878-80E4-625538590D29}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BootstrapBlazor.DataAccess.SqlSugar", "src\Extensions\DataServices\BootstrapBlazor.DataAccess.SqlSugar\BootstrapBlazor.DataAccess.SqlSugar.csproj", "{FF6149D2-82B8-40B4-9BB4-83D7AE9B3DA9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -298,6 +300,10 @@ Global
{65CEB170-D561-4878-80E4-625538590D29}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65CEB170-D561-4878-80E4-625538590D29}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65CEB170-D561-4878-80E4-625538590D29}.Release|Any CPU.Build.0 = Release|Any CPU
{FF6149D2-82B8-40B4-9BB4-83D7AE9B3DA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF6149D2-82B8-40B4-9BB4-83D7AE9B3DA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF6149D2-82B8-40B4-9BB4-83D7AE9B3DA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF6149D2-82B8-40B4-9BB4-83D7AE9B3DA9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -350,6 +356,7 @@ Global
{B94D32F4-2E7C-400B-BC34-D6BA31A234CB} = {81615751-490B-4B50-8029-C79846F592BB}
{81615751-490B-4B50-8029-C79846F592BB} = {A2182155-43ED-44C1-BF6F-1B70EBD2DFFE}
{65CEB170-D561-4878-80E4-625538590D29} = {CD062AB6-244D-402A-8F33-C37DAC5856CC}
{FF6149D2-82B8-40B4-9BB4-83D7AE9B3DA9} = {56FEFF09-85FC-408D-A397-6E264E68414E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0DCB0756-34FA-4FD0-AE1D-D3F08B5B3A6B}

View File

@ -124,9 +124,9 @@ class DefaultDataService<TModel> : DataServiceBase<TModel>, IEntityFrameworkCore
TotalCount = count,
Items = items,
IsSorted = option.SortOrder != SortOrder.Unset,
IsFiltered = option.Filters.Any(),
IsAdvanceSearch = option.AdvanceSearches.Any(),
IsSearch = option.Searches.Any() || option.CustomerSearches.Any()
IsFiltered = option.Filters.Count > 0,
IsAdvanceSearch = option.AdvanceSearches.Count > 0,
IsSearch = option.Searches.Count > 0 || option.CustomerSearches.Count > 0
};
return Task.FromResult(ret);
}

View File

@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// BootstrapBlazor 服务扩展类
/// BootstrapBlazor EFCore 服务扩展类
/// </summary>
public static class EFCoreServiceCollectionExtensions
{

View File

@ -9,9 +9,6 @@ namespace BootstrapBlazor.DataAccess.FreeSql;
/// <summary>
/// FreeSql ORM 的 IDataService 接口实现
/// </summary>
/// <remarks>
/// 构造函数
/// </remarks>
class DefaultDataService<TModel>(IFreeSql db) : DataServiceBase<TModel> where TModel : class, new()
{
/// <summary>
@ -64,9 +61,9 @@ class DefaultDataService<TModel>(IFreeSql db) : DataServiceBase<TModel> where TM
TotalCount = (int)count,
Items = items.ToList<TModel>(),
IsSorted = option.SortOrder != SortOrder.Unset,
IsFiltered = option.Filters.Any(),
IsAdvanceSearch = option.AdvanceSearches.Any(),
IsSearch = option.Searches.Any() || option.CustomerSearches.Any()
IsFiltered = option.Filters.Count > 0,
IsAdvanceSearch = option.AdvanceSearches.Count > 0,
IsSearch = option.Searches.Count > 0 || option.CustomerSearches.Count > 0
};
return Task.FromResult(ret);
}

View File

@ -9,7 +9,7 @@ using FreeSql;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// BootstrapBlazor 服务扩展类
/// BootstrapBlazor FreeSql 服务扩展类
/// </summary>
public static class FreeSqlServiceCollectionExtensions
{
@ -22,7 +22,7 @@ public static class FreeSqlServiceCollectionExtensions
/// <returns></returns>
public static IServiceCollection AddFreeSql(this IServiceCollection services, Action<FreeSqlBuilder> optionsAction, Action<IFreeSql>? configureAction = null)
{
services.AddSingleton<IFreeSql>(sp =>
services.AddSingleton(sp =>
{
var builder = new FreeSqlBuilder();
optionsAction(builder);

View File

@ -9,7 +9,7 @@ using PetaPoco;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// BootstrapBlazor 服务扩展类
/// BootstrapBlazor PetaPoco 服务扩展类
/// </summary>
public static class PetaPocoServiceCollectionExtensions
{

View File

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>8.0.0</Version>
</PropertyGroup>
<PropertyGroup>
<PackageTags>Bootstrap Blazor WebAssembly wasm UI Components SqlSugar</PackageTags>
<Description>Bootstrap UI components extensions of SqlSugar</Description>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BootstrapBlazor" Version="8.0.0" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.152" />
</ItemGroup>
<ItemGroup>
<Using Include="SqlSugar"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,67 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using BootstrapBlazor.Components;
namespace BootstrapBlazor.DataAccess.SqlSugar;
/// <summary>
/// SqlSugar ORM 的 IDataService 接口实现
/// </summary>
class DefaultDataService<TModel>(ISqlSugarClient db) : DataServiceBase<TModel> where TModel : class, new()
{
/// <summary>
/// 删除方法
/// </summary>
/// <param name="models"></param>
/// <returns></returns>
public override async Task<bool> DeleteAsync(IEnumerable<TModel> models)
{
// 通过模型获取主键列数据
// 支持批量删除
await db.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)
{
await db.Storageable(model).ExecuteCommandAsync();
return true;
}
/// <summary>
/// 查询方法
/// </summary>
/// <param name="option"></param>
/// <returns></returns>
public override Task<QueryData<TModel>> QueryAsync(QueryPageOptions option)
{
int count = 0;
var filter = option.ToFilter();
var items = db.Queryable<TModel>()
.WhereIF(filter.HasFilters(), filter.GetFilterLambda<TModel>())
.OrderByIF(option.SortOrder != SortOrder.Unset, $"{option.SortName} {option.SortOrder}")
.ToPageList(option.PageIndex, option.PageItems, ref count);
var ret = new QueryData<TModel>()
{
TotalCount = count,
Items = items,
IsSorted = option.SortOrder != SortOrder.Unset,
IsFiltered = option.Filters.Count > 0,
IsAdvanceSearch = option.AdvanceSearches.Count > 0,
IsSearch = option.Searches.Count > 0 || option.CustomerSearches.Count > 0
};
return Task.FromResult(ret);
}
}

View File

@ -0,0 +1,35 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using BootstrapBlazor.Components;
using BootstrapBlazor.DataAccess.SqlSugar;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// BootstrapBlazor SqlSugar 服务扩展类
/// </summary>
public static class SqlSugarServiceCollectionExtensions
{
/// <summary>
/// 增加 SqlSugar 数据库操作服务
/// </summary>
/// <param name="services"></param>
/// <param name="optionsAction"></param>
/// <param name="configureAction"></param>
/// <returns></returns>
public static IServiceCollection AddSqlSugar(this IServiceCollection services,
Action<ConnectionConfig> optionsAction,
Action<SqlSugarClient>? configureAction = null)
{
services.AddSingleton<ISqlSugarClient>(sp =>
{
var config = new ConnectionConfig();
optionsAction(config);
return new SqlSugarScope(config, configureAction);
});
services.AddScoped(typeof(IDataService<>), typeof(DefaultDataService<>));
return services;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB