Skip to content
.NET 开发者指北.NET 开发者指北
CMS
.NET指北
FreeKit
Docker
关于
博客
github icon
    • 开发起步
      • Newtonsoft.Json基础问题
        • 依赖注入scrutor
          • Serivce后缀服务注入DI
            • ITransientDependency
              • 接口
                • 模拟实现
                  • 如何使用
                    • 统一注入
                    • 认证与授权
                      • 获取控制器及方法特性标签
                        • 认证鉴权状态
                          • 全局敏感词处理
                            • GitHub第三方授权登录
                              • QQ第三方授权登录
                                • CAP实现EventBus
                                  • Scriban 模板语言

                                  依赖注入scrutor

                                  calendar icon2020年5月8日timer icon大约 2 分钟word icon约 590 字

                                  此页内容
                                  • Serivce后缀服务注入DI
                                  • ITransientDependency
                                  • 接口
                                  • 模拟实现
                                  • 如何使用
                                  • 统一注入

                                  # 依赖注入scrutor

                                  官网介绍

                                  https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2open in new window

                                  • 开源地址https://github.com/khellang/Scrutoropen in new window
                                  • 参考文档 https://www.cnblogs.com/catcher1994/p/10316928.htmlopen in new window 手动管理依赖注入过于麻烦,当有多个仓储,服务,无法统一注入,Scrutor能帮助我们简化ASP.NET Core的DI注册。

                                  在ConfigServices中,我们原本需要这样子依次注入仓储,服务和其他接口及实现,当有多个仓储时,这样就过于繁琐。

                                  services.AddTransient<IUserRepository, UserRepository>();
                                  services.AddTransient<IUserService, UserService>();
                                  services.AddTransient<ICurrentUser, CurrentUser>();
                                  
                                  1
                                  2
                                  3

                                  # Serivce后缀服务注入DI

                                  当我们有多个Service后缀的服务时,使用以下方法,可将服务扫描只留下以Serivce结尾的类,将其类型注册为提供所有公共接口生成服务,其生命周期为Transient,

                                  services.Scan(scan => scan
                                          //加载Startup这个类所在的程序集
                                          .FromAssemblyOf<Startup>()
                                          // 表示要注册那些类,上面的代码还做了过滤,只留下了以 Service 结尾的类
                                          .AddClasses(classes => classes.Where(t => t.Name.EndsWith("Service", StringComparison.OrdinalIgnoreCase)))
                                          //表示将类型注册为提供其所有公共接口作为服务
                                          .AsImplementedInterfaces()
                                          //表示注册的生命周期为 Transient
                                          .WithTransientLifetime()
                                           );
                                  
                                  
                                  1
                                  2
                                  3
                                  4
                                  5
                                  6
                                  7
                                  8
                                  9
                                  10
                                  11

                                  # ITransientDependency

                                  新建一个空接口,当其他类继承此接口后,统一注入到DI中,以Transient的生命周期。

                                  namespace LinCms.Zero.Dependency
                                  {
                                      public interface ITransientDependency
                                      {
                                      }
                                  }
                                  
                                  1
                                  2
                                  3
                                  4
                                  5
                                  6

                                  # 接口

                                  public interface ICurrentUser
                                  {
                                      int? Id { get; }
                                  
                                      int? GroupId { get; }
                                  
                                      bool? IsAdmin { get; }
                                  }
                                  
                                  1
                                  2
                                  3
                                  4
                                  5
                                  6
                                  7
                                  8

                                  # 模拟实现

                                      public class CurrentUser : ICurrentUser, ITransientDependency
                                      {
                                       
                                          public int? Id => 1;
                                          public int? GroupId => 2;
                                          public bool? IsAdmin => true;
                                      }
                                  
                                  1
                                  2
                                  3
                                  4
                                  5
                                  6
                                  7

                                  扫描所有继承ITransientDependency的实现。

                                     services.Scan(scan => scan
                                         // We start out with all types in the assembly of ITransientService
                                          .FromAssemblyOf<ITransientDependency>()
                                          // AddClasses starts out with all public, non-abstract types in this assembly.
                                          // These types are then filtered by the delegate passed to the method.
                                          // In this case, we filter out only the classes that are assignable to ITransientService.
                                          .AddClasses(classes => classes.AssignableTo<ITransientDependency>())
                                          // We then specify what type we want to register these classes as.
                                          // In this case, we want to register the types as all of its implemented interfaces.
                                          // So if a type implements 3 interfaces; A, B, C, we'd end up with three separate registrations.
                                          .AsImplementedInterfaces()
                                          // And lastly, we specify the lifetime of these registrations.
                                          .WithTransientLifetime()
                                           );
                                  
                                  
                                  1
                                  2
                                  3
                                  4
                                  5
                                  6
                                  7
                                  8
                                  9
                                  10
                                  11
                                  12
                                  13
                                  14
                                  15

                                  # 如何使用

                                  在其他类中使用此接口

                                  [ApiController]
                                  [Route("cms/user")]
                                  public class UserController : ControllerBase
                                  {
                                      private readonly ICurrentUser _currentUser;
                                  
                                      public UserController(ICurrentUser currentUser)
                                      {
                                          _currentUser = currentUser;
                                      }
                                  
                                      [HttpGet]
                                      public int GetUser()
                                      {
                                          return _currentUser.Id;
                                      }
                                  }
                                  
                                  1
                                  2
                                  3
                                  4
                                  5
                                  6
                                  7
                                  8
                                  9
                                  10
                                  11
                                  12
                                  13
                                  14
                                  15
                                  16
                                  17

                                  # 统一注入

                                  当然,我们可以统一注入,而非写二次servics.Scan

                                  services.Scan(scan => scan
                                              .FromAssemblyOf<Startup>()
                                              .AddClasses(classes => classes.Where(t => t.Name.EndsWith("Service",StringComparison.OrdinalIgnoreCase)))
                                              .AsImplementedInterfaces()
                                              .WithTransientLifetime()
                                              .FromAssemblyOf<ITransientDependency>()
                                              .AddClasses(classes => classes.AssignableTo<ITransientDependency>())
                                              .AsImplementedInterfaces()
                                              .WithTransientLifetime()
                                        );
                                  
                                  1
                                  2
                                  3
                                  4
                                  5
                                  6
                                  7
                                  8
                                  9
                                  10
                                  edit icon在 GitHub 上编辑此页open in new window
                                  上次编辑于: 2022/9/13 11:34:29
                                  贡献者: igeekfan,luoyunchong
                                  上一页
                                  Newtonsoft.Json基础问题
                                  下一页
                                  认证与授权
                                  MIT Licensed | Copyright © 2021-present luoyunchong
                                  苏ICP备16046457号-1

                                  该应用可以安装在你的 PC 或移动设备上。这将使该 Web 应用程序外观和行为与其他应用程序相同。它将在出现在应用程序列表中,并可以固定到主屏幕,开始菜单或任务栏。此 Web 应用程序还将能够与其他应用程序和你的操作系统安全地进行交互。

                                  详情