diff --git a/BootstrapAdmin.sln b/BootstrapAdmin.sln index 9102eabf..8a88581d 100644 --- a/BootstrapAdmin.sln +++ b/BootstrapAdmin.sln @@ -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} diff --git a/src/blazor/admin/BootStarpAdmin.DataAccess.SqlSugar/BootStarpAdmin.DataAccess.SqlSugar.csproj b/src/blazor/admin/BootStarpAdmin.DataAccess.SqlSugar/BootStarpAdmin.DataAccess.SqlSugar.csproj new file mode 100644 index 00000000..d0f27664 --- /dev/null +++ b/src/blazor/admin/BootStarpAdmin.DataAccess.SqlSugar/BootStarpAdmin.DataAccess.SqlSugar.csproj @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/src/blazor/admin/BootStarpAdmin.DataAccess.SqlSugar/Extensions/ServiceCollectionExtensions.cs b/src/blazor/admin/BootStarpAdmin.DataAccess.SqlSugar/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 00000000..4e3fda0a --- /dev/null +++ b/src/blazor/admin/BootStarpAdmin.DataAccess.SqlSugar/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,43 @@ +using BootStarpAdmin.DataAccess.SqlSugar.Service; +using BootstrapBlazor.Components; +using Microsoft.Extensions.DependencyInjection.Extensions; +using SqlSugar; + +namespace Microsoft.Extensions.DependencyInjection; + +/// +/// FreeSql ORM 注入服务扩展类 +/// +public static class ServiceCollectionExtensions +{ + /// + /// 注入 FreeSql 数据服务类 + /// + /// + /// + /// + public static IServiceCollection AddSqlSugar(this IServiceCollection services, Action sqlSugarBuilder) + { + services.TryAddSingleton(provider => + { + var builder = new ConnectionConfig(); + builder.IsAutoCloseConnection = true; + sqlSugarBuilder(provider, builder); + return new SqlSugarClient(builder); + }); + + // 增加数据服务 + services.AddSingleton(typeof(IDataService<>), typeof(DefaultDataService<>)); + + // 增加业务服务 + //services.AddSingleton(); + //services.AddSingleton(); + //services.AddSingleton(); + //services.AddSingleton(); + //services.AddSingleton(); + //services.AddSingleton(); + //services.AddSingleton(); + //services.AddSingleton(); + return services; + } +} diff --git a/src/blazor/admin/BootStarpAdmin.DataAccess.SqlSugar/Extensions/SqlSugarExtensions.cs b/src/blazor/admin/BootStarpAdmin.DataAccess.SqlSugar/Extensions/SqlSugarExtensions.cs new file mode 100644 index 00000000..e96004c5 --- /dev/null +++ b/src/blazor/admin/BootStarpAdmin.DataAccess.SqlSugar/Extensions/SqlSugarExtensions.cs @@ -0,0 +1,6 @@ +namespace BootStarpAdmin.DataAccess.SqlSugar.Extensions; + +static class SqlSugarExtensions +{ + +} diff --git a/src/blazor/admin/BootStarpAdmin.DataAccess.SqlSugar/Service/DefaultDataService.cs b/src/blazor/admin/BootStarpAdmin.DataAccess.SqlSugar/Service/DefaultDataService.cs new file mode 100644 index 00000000..e8702649 --- /dev/null +++ b/src/blazor/admin/BootStarpAdmin.DataAccess.SqlSugar/Service/DefaultDataService.cs @@ -0,0 +1,65 @@ +using BootstrapBlazor.Components; +using SqlSugar; + +namespace BootStarpAdmin.DataAccess.SqlSugar.Service; + +class DefaultDataService : DataServiceBase where TModel : class, new() +{ + private ISqlSugarClient Client { get; } + + public DefaultDataService(ISqlSugarClient client) => Client = client; + + /// + /// 删除方法 + /// + /// + /// + public override async Task DeleteAsync(IEnumerable models) + { + await Client.Deleteable(models).ExecuteCommandAsync(); + return true; + } + + /// + /// 保存方法 + /// + /// + /// + /// + public override async Task SaveAsync(TModel model, ItemChangedType changedType) + { + if (changedType == ItemChangedType.Add) + { + await Client.Insertable(model).ExecuteCommandAsync(); + } + else if (changedType == ItemChangedType.Update) + { + await Client.Updateable(model).ExecuteCommandAsync(); + } + return true; + } + + public override Task> QueryAsync(QueryPageOptions option) + { + var ret = new QueryData() + { + IsSorted = true, + IsFiltered = true, + IsSearch = true, + IsAdvanceSearch = option.AdvanceSearchs.Any() || option.CustomerSearchs.Any() + }; + if (option.IsPage) + { + var count = 0; + ret.Items = Client.Queryable() + .ToPageList(option.PageIndex, option.PageItems, ref count); + ret.TotalCount = count; + } + else + { + ret.Items = Client.Queryable() + .ToList(); + } + return Task.FromResult(ret); + } +}