using SqlSugar;
namespace Microsoft.Extensions.DependencyInjection;
///
/// SqlSugar 拓展类
///
public static class SqlSugarServiceCollectionExtensions
{
///
/// 添加 SqlSugar 拓展
///
///
///
///
///
public static IServiceCollection AddSqlSugar(this IServiceCollection services, ConnectionConfig config, Action buildAction = default)
{
var list = new List();
list.Add(config);
return services.AddSqlSugar(list, buildAction);
}
///
/// 添加 SqlSugar 拓展
///
///
///
///
///
public static IServiceCollection AddSqlSugar(this IServiceCollection services, List configs, Action buildAction = default)
{
// SqlSugarScope是线程安全,可使用单例注入
// 注册 SqlSugar 客户端
services.AddSingleton(u =>
{
var sqlSugarClient = new SqlSugarScope(configs);
buildAction?.Invoke(sqlSugarClient);
return sqlSugarClient;
});
// 注册 SqlSugar 仓储
services.AddScoped(typeof(ISqlSugarRepository<>), typeof(SqlSugarRepository<>));
return services;
}
}