OdinPlugs.OdinInject 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package OdinPlugs.OdinInject --version 1.0.1                
NuGet\Install-Package OdinPlugs.OdinInject -Version 1.0.1                
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="OdinPlugs.OdinInject" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OdinPlugs.OdinInject --version 1.0.1                
#r "nuget: OdinPlugs.OdinInject, 1.0.1"                
#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 OdinPlugs.OdinInject as a Cake Addin
#addin nuget:?package=OdinPlugs.OdinInject&version=1.0.1

// Install OdinPlugs.OdinInject as a Cake Tool
#tool nuget:?package=OdinPlugs.OdinInject&version=1.0.1                

OdinPlugs.OdinInject

简介:

自动注入类,可以通过提供的方法自动注入符合规则的类型.

注入类型:

  1. 注入规则:

    实现了接口 IAutoInject,IAutoInjectWithParamas,IAutoServiceInject的接口和对应的实现类都可以通过OdinInject提供的方法自动注入。注入方法的 Singleton、Transient、Scoped 与传统的.Net Core的 Singleton、Transient、Scoped 一致。

  2. 注入方式

    2.1 无参数注入示例:
    public interface IOdin:IAutoInject
    {
        void Show();
    }
    
    public class Odin : IOdin
    {
        public void Show() { Console.WriteLine("this is show method"); }
    }
    
    // 注入当前项目的所有类型
    services.AddOdinSingletonInject(this.GetType().Assembly);
    // 注入当前项目指定类型
    services.AddOdinSingletonInject<IOdin>(this.GetType().Assembly);
    // 注入某程序集的所有类型
    Assembly ass = Assembly.Load("OdinPlugs");
    services.AddOdinSingletonInject(ass);
    services.AddOdinSingletonInject<IOdin>(ass);
    
    2.2 带参数注入示例:
    public interface IOdinWithParams : IAutoInjectWithParams
    {
        void Show();
    }
    
    public class OdinWithParams : IOdinWithParams
    {
        private readonly string str;
        public Odin(string str) { this.str = str; }
        public void Show() { Console.WriteLine($"this is show method,str value:{ this.str }"); }
    }
    
    public class OdinOption
    {
        public string OptionName { get; set; }
        public string OptionValue { get; set; }
    }
    public class OdinWithOptionsParams : IOdinWithParams
    {
        private readonly OdinOption option;
        public Odin(OdinOption option) { this.option = option; }
        public void Show() 
        { 
            Console.WriteLine($"this is show method,option name:{ option.OptionName } option value:{ option.OptionValue }"); 
        }
    }
    
    // 如果使用 new object方式注入,参数顺序与构造函数参数顺序需要一直
    // 注入当前项目 IOdinWithParams 类型
    services.AddOdinSingletonWithParamsInject<IOdinWithParams>(
        this.GetType().Assembly,
        new object["InjectStr"]);
    // 注入某程序集指的定类型
    Assembly ass = Assembly.Load("OdinPlugs");
    services.AddOdinSingletonWithParamsInject<IOdinWithParams>(
        ass,
        new object["InjectStr"]);
    
    // 使用 options 注入
    // 注入当前项目 IOdinWithParams 类型
    services.AddOdinSingletonWithParamsInject<IOdinWithParams,OdinOption>(
        this.GetType().Assembly,
        opt=>
        { 
            opt.OptionName="InjectName", 
            opt.OptionValue="InjectValue" 
        });
    // 注入某程序集的所有类型
    Assembly ass = Assembly.Load("OdinPlugs");
    services.AddOdinSingletonWithParamsInject<IOdinWithParams,OdinOption>(
        ass,
        opt=>{ 
            opt.OptionName="InjectName", 
            opt.OptionValue="InjectValue" 
        });
    
    2.3 HttpClient注入:
    // 无ssl证书注册
    services.AddOdinHttpClient("OdinClient")
    
    // 有ssl证书注册
    services.AddOdinHttpClientByCer(opt =>
            opt = new List<SslCerOptions>
            {
                new SslCerOptions{ ClientName="",CerName="",CerPassword = "",CerPath="" }
            });
    

    具体使用:

    OdinInjectCore.GetService<IOdinHttpClientFactory>().GetRequestAsync(
        "OdinClient", 
        "url",
        customHeaders,
        "application/json");
    OdinInjectCore.GetService<IOdinHttpClientFactory>().PostRequestAsync(
        "OdinClient", 
        "url",
        postData,
        customHeaders,
        "application/json",
        Encoding.UTF8);
    
    2.4 Cap注入:

    项目使用Cap第三方框架,示例使用 mysql和rabbitmq,还可以使用其他方式,具体可以参见 OdinCapEventBusOptions

    services.AddOdinCapInject(opt=>
            opt = new OdinCapEventBusOptions
                {
                    MysqlConnectionString = _Options.DbEntity.ConnectionString,
                    RabbitmqOptions = _Options.RabbitMQ
                });
    

    具体使用:

    // 生产者
    var header = new Dictionary<string, string>()
            {
                ["RouteingKey"] = "cap.odinCore.Aop.RabbitMQ.TestAction",
            };
            OdinCapHelper.CapPublish("cap.odinCore.Aop.RabbitMQ.TestAction", DateTime.Now, () =>
            {
                System.Console.WriteLine("to do something");
            }, header);
    
    // 消费调用
    [CapSubscribe("cap.odinCore.Aop.RabbitMQ.#")]
    public async Task<Task> CheckReceivedMessage(DateTime time, [FromCap] CapHeader header)
    {
        Console.WriteLine($"============{header["RouteingKey"]}==========={time.ToString("yyyy-MM-dd hh:mm:ss")}================");
        return Task.CompletedTask;
    }
    

获取注入类型:

方法 说明 备注
GetService<T>() 在任何地方使用InjectCore获取注入的类型 T:要获取的类型^1
GetService<T>(this IServiceCollection services) services的扩展方法,可以方便的在Startup方法中使用services获取注入的类型 T:要获取的类型
SetServiceProvider(this IServiceCollection services) 获取ServiceProvider对象
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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 (4)

Showing the top 4 NuGet packages that depend on OdinPlugs.OdinInject:

Package Downloads
OdinPlugs.SnowFlake

Package Description

OdinPlugs.ApiLinkMonitor

Package Description

OdinPlugs.OdinEFCore

Package Description

OdinPlugs.OdinWebApi

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.7.2 1,204 8/4/2021
1.0.7 1,145 8/4/2021
1.0.6 1,067 7/20/2021
1.0.5 1,126 7/20/2021
1.0.1 437 7/9/2021