Newcats.DependencyInjection 1.0.3

dotnet add package Newcats.DependencyInjection --version 1.0.3                
NuGet\Install-Package Newcats.DependencyInjection -Version 1.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Newcats.DependencyInjection" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Newcats.DependencyInjection --version 1.0.3                
#r "nuget: Newcats.DependencyInjection, 1.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Newcats.DependencyInjection as a Cake Addin
#addin nuget:?package=Newcats.DependencyInjection&version=1.0.3

// Install Newcats.DependencyInjection as a Cake Tool
#tool nuget:?package=Newcats.DependencyInjection&version=1.0.3                

Newcats.DependencyInjection 使用说明

Net Core Nuget GitHub License

1.标记接口

ISingletonDependency: 整个应用程序生命周期以内只创建一个实例

IScopedDependency:在同一个Scope内只初始化一个实例 ,可以理解为( 每一个request级别只创建一个实例,同一个http request会在一个 scope内)

ITransientDependency:每一次GetService都会创建一个新的实例

  • 注1:具体需要哪个标记接口,看需求,一般Web项目用IScopedDependency
  • 注2:程序启动时会扫描bin目录下的所有自己项目的dll文件,查找上述3个标记接口,然后注册依赖关系
  • 注3:对于不会自动生成在bin下的项目dll,请直接引用
  • 注4:推荐使用使用构造函数注入
  • 注5:对于不能使用构造函数注入的,可以使用HttpContext.RequestServices.GetService<IService>();

2.注册服务

Startup.cs
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddHttpContextAccessor();
        services.AddDependencyInjection();//注册依赖
    }

   public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
   {
        app.UseStaticHttpContext();//可选,如需使用HttpContext.RequestServices.GetService<IService>();则必须添加此行代码
   }
}

3.添加依赖注册器

  • 注:不想使用标记接口,不想在Startup.cs文件里写太多的注册依赖,需要手动管理依赖的,可以使用依赖注册器
  • 添加类,继承Newcats.DependencyInjection.IDependencyRegistrar即可
/// <summary>
/// 自定义依赖注册器
/// </summary>
public class ServiceRegister : IDependencyRegistrar
{
   public void Register(IServiceCollection services)
      {
         const string mySqlStr = "server=localhost;port=3306;database=NewcatsDB20211019;uid=root;pwd=1232@mysql;CharSet=utf8;AllowLoadLocalInfile=true";

         services.AddScoped<IFileStore, FileStore>();
            
         //services.AddScoped(typeof(DataAccess.SqlServer.IRepository<>), typeof(DataAccess.SqlServer.Repository<>));//注册泛型仓储
            
         //services.AddScoped(typeof(DataAccess.MySql.IRepository<>), typeof(DataAccess.MySql.Repository<>));//注册泛型仓储

         services.AddMySqlDataAccess<MySqlDbContext>(opt =>
         {
               opt.ConnectionString = mySqlStr;
         });
        }
    }

4.使用构造函数注入

   //接口
   public interface IUserInfoService : IScopedDependency
   {
   }

   //实现
   public class UserInfoService : IUserInfoService
   {

   }
UserInfoController.cs
   //控制器
   public class UserInfoController : ControllerBase
   {
      private readonly IUserInfoService _userService;

      public UserInfoController(IUserInfoService userService)
      {
         _userService = userService;//此处得到的即为IUserInfoService的实现类UserInfoService
      }
   }

贡献与反馈

如果你在阅读或使用任意一个代码片断时发现Bug,或有更佳实现方式,欢迎提Issue。

对于你提交的代码,如果我们决定采纳,可能会进行相应重构,以统一代码风格。

对于热心的同学,将会把你的名字放到贡献者名单中。


免责声明

  • 虽然代码已经进行了高度审查,并用于自己的项目中,但依然可能存在某些未知的BUG,如果你的生产系统蒙受损失,本人不会对此负责。
  • 出于成本的考虑,将不会对已发布的API保持兼容,每当更新代码时,请注意该问题。

协议

MIT © Newcats


作者: newcats-2021/12/01

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Newcats.DependencyInjection:

Package Downloads
Newcats.AspNetCore

Some common modules to make AspNetCore easy.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 1,283 12/1/2021
1.0.2 359 11/18/2021
1.0.1 351 10/20/2021
1.0.0 319 9/2/2021

2021-12-01
1.增加详细的使用说明文档,详见github