change - 仓储层、多数据源配置

master
wenjy 1 year ago
parent 97f16b5377
commit 34abe6cb1f

@ -6,7 +6,7 @@ using System.Text;
namespace Durkee.Mes.Api.Model namespace Durkee.Mes.Api.Model
{ {
[SugarTable("BASE_BOMINFO","mes")] [SugarTable("BASE_BOMINFO"),TenantAttribute("mes")]
public class BaseBomInfo public class BaseBomInfo
{ {
/// <summary> /// <summary>

@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

@ -6,7 +6,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.112" /> <PackageReference Include="SqlSugarCore" Version="5.1.4.112" />
</ItemGroup> </ItemGroup>

@ -8,17 +8,14 @@ namespace Durkee.Mes.Api.Repository
public class Repository<T> : SimpleClient<T> where T : class, new() public class Repository<T> : SimpleClient<T> where T : class, new()
{ {
public ITenant itenant = null;//多租户事务、GetConnection、IsAnyConnection等功能 public ITenant itenant = null;//多租户事务、GetConnection、IsAnyConnection等功能
public Repository(ISqlSugarClient db) public Repository(ISqlSugarClient db)
{ {
itenant = db.AsTenant();//用来处理事务 itenant = db.AsTenant();//用来处理事务
base.Context = db.AsTenant().GetConnectionScopeWithAttr<T>();//获取子Db base.Context = db.AsTenant().GetConnectionScopeWithAttr<T>();//获取子Db
//如果不想通过注入多个仓储 //如果不想通过注入多个仓储
//用到ChangeRepository或者Db.GetMyRepository需要看标题4写法 //用到ChangeRepository或者Db.GetMyRepository需要看标题4写法
} }
} }

@ -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
{
/// <summary>
/// 注册SqlSugar
/// </summary>
/// <param name="services"></param>
public static void AddSqlSugarSetup(this IServiceCollection services)
{
services.AddSingleton<ISqlSugarClient>(x =>
{
var appConfig = x.GetService<AppConfig>();
SqlSugarScope Db = new SqlSugarScope(new List<ConnectionConfig>()
{
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;
});
}
/// <summary>
/// 注册Repository、Service服务
/// </summary>
/// <param name="services"></param>
public static void AddServices(this IServiceCollection services)
{
services.AddSingleton<Repository<BaseBomInfo>>();
services.AddSingleton<IBaseBomInfoService, BaseBomInfoServiceImpl>();
}
}
}

@ -1,5 +1,7 @@
using Durkee.Mes.Api.Model; using Durkee.Mes.Api.Model;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Logging; using Microsoft.IdentityModel.Logging;
using SqlSugar;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@ -8,9 +10,12 @@ namespace Durkee.Mes.Api.Repository.service.Impl
{ {
public class BaseBomInfoServiceImpl : IBaseBomInfoService public class BaseBomInfoServiceImpl : IBaseBomInfoService
{ {
private Repository<BaseBomInfo> _bomInfoRepository; private readonly ILogger<BaseBomInfoServiceImpl> _logger;
public BaseBomInfoServiceImpl(Repository<BaseBomInfo> bomInfoRepository)
private readonly Repository<BaseBomInfo> _bomInfoRepository;
public BaseBomInfoServiceImpl(ILogger<BaseBomInfoServiceImpl> logger,Repository<BaseBomInfo> bomInfoRepository)
{ {
_logger = logger;
_bomInfoRepository = bomInfoRepository; _bomInfoRepository = bomInfoRepository;
} }
@ -24,6 +29,7 @@ namespace Durkee.Mes.Api.Repository.service.Impl
catch (Exception ex) catch (Exception ex)
{ {
//logHelper.Error("获取BOM集合异常", ex); //logHelper.Error("获取BOM集合异常", ex);
_logger.LogError($"获取BOM集合异常:{ex.Message}");
return null; return null;
} }
} }

@ -24,7 +24,7 @@ namespace Durkee.Mes.Api.Controllers
private IBaseBomInfoService _service; private IBaseBomInfoService _service;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IBaseBomInfoService service) public WeatherForecastController(ILogger<WeatherForecastController> logger, AppConfig appConfig, IBaseBomInfoService service)
{ {
_logger = logger; _logger = logger;
_service = service; _service = service;

@ -1,10 +1,6 @@
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog; using Serilog;
using Serilog.Events;
using System;
using System.IO;
namespace Durkee.Mes.Api namespace Durkee.Mes.Api
{ {

@ -45,39 +45,12 @@ namespace Durkee.Mes.Api
var configuration = provider.GetService<IConfiguration>(); var configuration = provider.GetService<IConfiguration>();
return configuration.GetSection("AppConfig").Get<AppConfig>(); return configuration.GetSection("AppConfig").Get<AppConfig>();
}); });
//×¢²áSqlSugar //×¢²áSqlSugar
services.AddSingleton<ISqlSugarClient>(x => services.AddSqlSugarSetup();
{
var appConfig = x.GetService<AppConfig>(); //×¢²á·þÎñ
SqlSugarScope Db = new SqlSugarScope(new List<ConnectionConfig>() services.AddServices();
{
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<Repository<BaseBomInfo>>();
services.AddSingleton<IBaseBomInfoService, BaseBomInfoServiceImpl>();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

Loading…
Cancel
Save