Wjybxx.Commons.Inject 1.5.0-rc2

This is a prerelease version of Wjybxx.Commons.Inject.
dotnet add package Wjybxx.Commons.Inject --version 1.5.0-rc2
                    
NuGet\Install-Package Wjybxx.Commons.Inject -Version 1.5.0-rc2
                    
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="Wjybxx.Commons.Inject" Version="1.5.0-rc2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Wjybxx.Commons.Inject" Version="1.5.0-rc2" />
                    
Directory.Packages.props
<PackageReference Include="Wjybxx.Commons.Inject" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Wjybxx.Commons.Inject --version 1.5.0-rc2
                    
#r "nuget: Wjybxx.Commons.Inject, 1.5.0-rc2"
                    
#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.
#:package Wjybxx.Commons.Inject@1.5.0-rc2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Wjybxx.Commons.Inject&version=1.5.0-rc2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Wjybxx.Commons.Inject&version=1.5.0-rc2&prerelease
                    
Install as a Cake Tool

依赖注入工具

C# 也有一些依赖注入工具,比如Autofac,但Autofac设计得太复杂了,而且缺少我需要的子注入器。 所以,我还是决定实现一个我熟悉的依赖注入框架。

版本号同步

注意:Wjybxx.Commons.Core,Wjybxx.Commons.Inject,Wjybxx.Commons.Concurrent 三个程序集的版本号总是保持一致,任一程序集修改,其它程序集版本号也会修改。

注入方式

默认实现支持字段、属性、构造函数和普通方法注入,但构造函数不支持重载,即总是使用第一个带有注解(属性)的构造函数创建对象。

单例作用域问题(配置级别)

单例是锁定配置的,指向同一个配置的服务才将共享同一个实例。

    public void Configure(IInjectBinder binder) {
        // 这将为ServiceType1和ServiceType2分别创建一个实例
        binder.Bind<ImpType, ServiceTyp1>(InjectScope.Singleton);
        binder.Bind<ImpType, ServiceTyp2>(InjectScope.Singleton);
        
        // ServiceType1和ServiceType2将共享同一个实例
        binder.Bind<ImpType>(InjectScope.Singleton, ServiceTyp1, ServiceTyp2);
    }

PS:如果实现类是泛型的,那么不同运行时类型之间的单例也是独立的。

泛型问题

当实现类是泛型类时,如果不能自动从服务类继承泛型参数,则需要用户提供函数构建对应的实现类型。

命名服务

依赖注入框架支持为服务命名;当服务未同时绑定无name服务时--即全部以name进行绑定时,申请实例时也必须指定名字,否则将抛出异常或返回null。

    // injector配置
    public void Configure(IInjectBinder binder) {
        binder.Bind<ServiceImpl>();
         // 如果未同时绑定无name服务,将无法直接通过typeof(IService1)获得实例
        binder.Bind<IService1>("json");
        binder.Bind<IService2>();
    }
    //
   private class ServiceImpl
    {
        [Inject("json")] // 指定注入命名为json的Service1
        public IService1 service1;

        [Inject] // 注入 IService2,不存在时抛出异常
        public IService2 service2;

        [Inject(true)] // Service3不存在时保持null
        public IService3 service3;
    }

循环依赖

不支持构造器的循环依赖,循环依赖仅支持延迟注入 —— 不然复杂度太高。

注入容器

        // 该方式可以注入容器对象的引用
        [Inject] public IInjector injecor;

反射

默认实现是基于反射的,不适用于高频创建对象的场景,也不适用GameObject的管理,通常用于框架层的对象管理。

PS:单例对象的效率是不差的。

Release Notes

1.4.x

  1. 增加InjecMembers接口,允许为既有实例注入依赖 —— 在Unity项目很有用。
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 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.5.0-rc2 141 6/29/2025
1.5.0-rc1 156 6/14/2025
1.4.2 156 5/25/2025
1.4.1 72 5/24/2025
1.4.0 111 5/18/2025
1.3.1 228 5/14/2025
1.3.0 153 5/6/2025