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

master
wenjy 1 year ago
parent 97f16b5377
commit 34abe6cb1f

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

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

@ -6,7 +6,9 @@
</PropertyGroup>
<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.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.112" />
</ItemGroup>

@ -8,17 +8,14 @@ namespace Durkee.Mes.Api.Repository
public class Repository<T> : SimpleClient<T> where T : class, new()
{
public ITenant itenant = null;//多租户事务、GetConnection、IsAnyConnection等功能
public Repository(ISqlSugarClient db)
{
itenant = db.AsTenant();//用来处理事务
base.Context = db.AsTenant().GetConnectionScopeWithAttr<T>();//获取子Db
//如果不想通过注入多个仓储
//用到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 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<BaseBomInfo> _bomInfoRepository;
public BaseBomInfoServiceImpl(Repository<BaseBomInfo> bomInfoRepository)
private readonly ILogger<BaseBomInfoServiceImpl> _logger;
private readonly Repository<BaseBomInfo> _bomInfoRepository;
public BaseBomInfoServiceImpl(ILogger<BaseBomInfoServiceImpl> logger,Repository<BaseBomInfo> 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;
}
}

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

@ -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
{

@ -47,37 +47,10 @@ namespace Durkee.Mes.Api
});
//×¢²áSqlSugar
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 = Guid.NewGuid(),
DbType = DbType.Oracle,
ConnectionString = appConfig.mcsConnStr,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
}
},
db =>
{
db.Aop.OnLogExecuting = (sql, pars) => { };
});
services.AddSqlSugarSetup();
return Db;
});
services.AddSingleton<Repository<BaseBomInfo>>();
services.AddSingleton<IBaseBomInfoService, BaseBomInfoServiceImpl>();
//×¢²á·þÎñ
services.AddServices();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

Loading…
Cancel
Save