From 34abe6cb1fd6ac15e529619d0f191af10450e73e Mon Sep 17 00:00:00 2001 From: wenjy Date: Fri, 27 Oct 2023 10:40:35 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20=E4=BB=93=E5=82=A8=E5=B1=82?= =?UTF-8?q?=E3=80=81=E5=A4=9A=E6=95=B0=E6=8D=AE=E6=BA=90=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Durkee.Mes.Api.Model/BaseBomInfo.cs | 2 +- .../Durkee.Mes.Api.Model.csproj | 1 + .../Durkee.Mes.Api.Repository.csproj | 2 + Durkee.Mes.Api.Repository/Repository.cs | 5 +- Durkee.Mes.Api.Repository/SqlsugarSetup.cs | 64 +++++++++++++++++++ .../service/Impl/BaseBomInfoServiceImpl.cs | 10 ++- .../Controllers/WeatherForecastController.cs | 2 +- Durkee.Mes.Api/Program.cs | 4 -- Durkee.Mes.Api/Startup.cs | 37 ++--------- 9 files changed, 83 insertions(+), 44 deletions(-) create mode 100644 Durkee.Mes.Api.Repository/SqlsugarSetup.cs diff --git a/Durkee.Mes.Api.Model/BaseBomInfo.cs b/Durkee.Mes.Api.Model/BaseBomInfo.cs index 6665d6e..49b7f7c 100644 --- a/Durkee.Mes.Api.Model/BaseBomInfo.cs +++ b/Durkee.Mes.Api.Model/BaseBomInfo.cs @@ -6,7 +6,7 @@ using System.Text; namespace Durkee.Mes.Api.Model { - [SugarTable("BASE_BOMINFO","mes")] + [SugarTable("BASE_BOMINFO"),TenantAttribute("mes")] public class BaseBomInfo { /// diff --git a/Durkee.Mes.Api.Model/Durkee.Mes.Api.Model.csproj b/Durkee.Mes.Api.Model/Durkee.Mes.Api.Model.csproj index 82a7fbe..ab0a1c5 100644 --- a/Durkee.Mes.Api.Model/Durkee.Mes.Api.Model.csproj +++ b/Durkee.Mes.Api.Model/Durkee.Mes.Api.Model.csproj @@ -2,6 +2,7 @@ netcoreapp3.1 + true diff --git a/Durkee.Mes.Api.Repository/Durkee.Mes.Api.Repository.csproj b/Durkee.Mes.Api.Repository/Durkee.Mes.Api.Repository.csproj index 9d040d7..a6d9e85 100644 --- a/Durkee.Mes.Api.Repository/Durkee.Mes.Api.Repository.csproj +++ b/Durkee.Mes.Api.Repository/Durkee.Mes.Api.Repository.csproj @@ -6,7 +6,9 @@ + + diff --git a/Durkee.Mes.Api.Repository/Repository.cs b/Durkee.Mes.Api.Repository/Repository.cs index 5ef0bc1..dc21bde 100644 --- a/Durkee.Mes.Api.Repository/Repository.cs +++ b/Durkee.Mes.Api.Repository/Repository.cs @@ -8,17 +8,14 @@ namespace Durkee.Mes.Api.Repository public class Repository : SimpleClient where T : class, new() { public ITenant itenant = null;//多租户事务、GetConnection、IsAnyConnection等功能 + public Repository(ISqlSugarClient db) { - itenant = db.AsTenant();//用来处理事务 base.Context = db.AsTenant().GetConnectionScopeWithAttr();//获取子Db - //如果不想通过注入多个仓储 //用到ChangeRepository或者Db.GetMyRepository需要看标题4写法 - - } } diff --git a/Durkee.Mes.Api.Repository/SqlsugarSetup.cs b/Durkee.Mes.Api.Repository/SqlsugarSetup.cs new file mode 100644 index 0000000..f146636 --- /dev/null +++ b/Durkee.Mes.Api.Repository/SqlsugarSetup.cs @@ -0,0 +1,64 @@ +using Durkee.Mes.Api.Config; +using Durkee.Mes.Api.Model; +using Durkee.Mes.Api.Repository.service.Impl; +using Durkee.Mes.Api.Repository.service; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Text; + +namespace Durkee.Mes.Api.Repository +{ + public static class SqlsugarSetup + { + /// + /// 注册SqlSugar + /// + /// + public static void AddSqlSugarSetup(this IServiceCollection services) + { + services.AddSingleton(x => + { + var appConfig = x.GetService(); + SqlSugarScope Db = new SqlSugarScope(new List() + { + new ConnectionConfig() + { + ConfigId = "mes", + DbType = DbType.Oracle, + ConnectionString = appConfig.mesConnStr, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + }, + new ConnectionConfig() + { + ConfigId = "mcs", + DbType = DbType.Oracle, + ConnectionString = appConfig.mcsConnStr, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + } + }, + db => + { + db.Aop.OnLogExecuting = (sql, pars) => { }; + }); + + return Db; + }); + } + + /// + /// 注册Repository、Service服务 + /// + /// + public static void AddServices(this IServiceCollection services) + { + services.AddSingleton>(); + services.AddSingleton(); + } + } +} diff --git a/Durkee.Mes.Api.Repository/service/Impl/BaseBomInfoServiceImpl.cs b/Durkee.Mes.Api.Repository/service/Impl/BaseBomInfoServiceImpl.cs index 80409b9..dffb121 100644 --- a/Durkee.Mes.Api.Repository/service/Impl/BaseBomInfoServiceImpl.cs +++ b/Durkee.Mes.Api.Repository/service/Impl/BaseBomInfoServiceImpl.cs @@ -1,5 +1,7 @@ using Durkee.Mes.Api.Model; +using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Logging; +using SqlSugar; using System; using System.Collections.Generic; using System.Text; @@ -8,9 +10,12 @@ namespace Durkee.Mes.Api.Repository.service.Impl { public class BaseBomInfoServiceImpl : IBaseBomInfoService { - private Repository _bomInfoRepository; - public BaseBomInfoServiceImpl(Repository bomInfoRepository) + private readonly ILogger _logger; + + private readonly Repository _bomInfoRepository; + public BaseBomInfoServiceImpl(ILogger logger,Repository bomInfoRepository) { + _logger = logger; _bomInfoRepository = bomInfoRepository; } @@ -24,6 +29,7 @@ namespace Durkee.Mes.Api.Repository.service.Impl catch (Exception ex) { //logHelper.Error("获取BOM集合异常", ex); + _logger.LogError($"获取BOM集合异常:{ex.Message}"); return null; } } diff --git a/Durkee.Mes.Api/Controllers/WeatherForecastController.cs b/Durkee.Mes.Api/Controllers/WeatherForecastController.cs index 271e007..548a15c 100644 --- a/Durkee.Mes.Api/Controllers/WeatherForecastController.cs +++ b/Durkee.Mes.Api/Controllers/WeatherForecastController.cs @@ -24,7 +24,7 @@ namespace Durkee.Mes.Api.Controllers private IBaseBomInfoService _service; - public WeatherForecastController(ILogger logger, IBaseBomInfoService service) + public WeatherForecastController(ILogger logger, AppConfig appConfig, IBaseBomInfoService service) { _logger = logger; _service = service; diff --git a/Durkee.Mes.Api/Program.cs b/Durkee.Mes.Api/Program.cs index 2dc26ce..44078ce 100644 --- a/Durkee.Mes.Api/Program.cs +++ b/Durkee.Mes.Api/Program.cs @@ -1,10 +1,6 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; using Serilog; -using Serilog.Events; -using System; -using System.IO; namespace Durkee.Mes.Api { diff --git a/Durkee.Mes.Api/Startup.cs b/Durkee.Mes.Api/Startup.cs index c3c7da7..6312ef5 100644 --- a/Durkee.Mes.Api/Startup.cs +++ b/Durkee.Mes.Api/Startup.cs @@ -45,39 +45,12 @@ namespace Durkee.Mes.Api var configuration = provider.GetService(); return configuration.GetSection("AppConfig").Get(); }); - + //עSqlSugar - services.AddSingleton(x => - { - var appConfig = x.GetService(); - SqlSugarScope Db = new SqlSugarScope(new List() - { - new ConnectionConfig() - { - ConfigId = "mes", - DbType = DbType.Oracle, - ConnectionString = appConfig.mesConnStr, - InitKeyType = InitKeyType.Attribute, - IsAutoCloseConnection = true, - }, - new ConnectionConfig() - { - ConfigId = Guid.NewGuid(), - DbType = DbType.Oracle, - ConnectionString = appConfig.mcsConnStr, - InitKeyType = InitKeyType.Attribute, - IsAutoCloseConnection = true, - } - }, - db => - { - db.Aop.OnLogExecuting = (sql, pars) => { }; - }); - - return Db; - }); - services.AddSingleton>(); - services.AddSingleton(); + services.AddSqlSugarSetup(); + + //ע + services.AddServices(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.