Blame view

netcore/src/Infrastructure/NCC.Data.SqlSugar/Extensions/SqlSugarServiceCollectionExtensions.cs 1.92 KB
96009bc9   hexiaodong   hxd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  using SqlSugar;
  using System;
  using System.Linq;
  
  namespace Microsoft.Extensions.DependencyInjection
  {
      /// <summary>
      /// SqlSugar 拓展类
      /// </summary>
      public static class SqlSugarServiceCollectionExtensions
      {
          /// <summary>
          /// 添加 SqlSugar 拓展
          /// </summary>
          /// <param name="services"></param>
          /// <param name="config"></param>
          /// <param name="buildAction"></param>
          /// <returns></returns>
          public static IServiceCollection AddSqlSugar(this IServiceCollection services, ConnectionConfig config, Action<ISqlSugarClient> buildAction = default)
          {
              return services.AddSqlSugar(new ConnectionConfig[] { config }, buildAction);
          }
  
          /// <summary>
          /// 添加 SqlSugar 拓展
          /// </summary>
          /// <param name="services"></param>
          /// <param name="configs"></param>
          /// <param name="buildAction"></param>
          /// <returns></returns>
          public static IServiceCollection AddSqlSugar(this IServiceCollection services, ConnectionConfig[] configs, Action<ISqlSugarClient> buildAction = default)
          {
              // 注册 SqlSugar 客户端
              services.AddScoped<ISqlSugarClient>(u =>
              {
                  var sqlSugarClient = new SqlSugarScope(configs.ToList());
                  buildAction?.Invoke(sqlSugarClient);
                  return sqlSugarClient;
              });
              services.AddScoped<ITenant>(u =>
              {
                  var tenant = new SqlSugarScope(configs.ToList());
                  buildAction?.Invoke(tenant);
                  return tenant;
              });
  
              // 注册非泛型仓储
              services.AddScoped<ISqlSugarRepository, SqlSugarRepository>();
  
              // 注册 SqlSugar 仓储
              services.AddScoped(typeof(ISqlSugarRepository<>), typeof(SqlSugarRepository<>));
  
              return services;
          }
      }
  }