RuoVea.Autowired 5.0.0

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

// Install RuoVea.Autowired as a Cake Tool
#tool nuget:?package=RuoVea.Autowired&version=5.0.0                

Autowired.Core

介绍

自动服务注入

一、基本使用说明
  1. 编写服务类,并添加[AppService]特性

    [AppService]
    public class MyService
    {
        //functions
    }
    
  2. 在Setup的ConfigureServices方法中注册应用服务

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        //注册应用服务
        services.AddAppServices();
    }
    
  3. 其他类中注入使用,比如Controller中

    public class HomeController : Controller
    {
        [Autowired]
        MyUserService myUserService;
    
        public HomeController(AutowiredService autowiredService)
        {
            autowiredService.Autowired(this);
        }
    }
    
  4. 如果服务的生命周期是Scoped,需要注入IServiceProvider

    public class HomeController : Controller
    {
        [Autowired]
        MyUserService myUserService;
    
        public HomeController(AutowiredService autowiredService, IServiceProvider provider )
        {
            autowiredService.Autowired(this, provider);
        }
    }
    
    
二、生命周期
    [AppService(ServiceLifetime.Transient)]
    public class MyService
    {
        //functions
    }
三、实现接口

服务代码:

    public interface IService{}

    [AppService]
    public class MyService:IService
    {
        //functions
    }

注入代码:

    [Autowired]
    IService myUserService; 
四、实现多接口
    public interface IService1{}
    public interface IService2{}

    [AppService]
    public class MyService:IService1,IService2
    {
        //functions
    }

这个时候可以默认注入类型可以是IService1,即第一个接口类型,如果要注入的IService2,可以把IService2放在第一位,也可以如下声明:

    [AppService(typeof(IService2))]  //对应IService2
    [AppService]   //对应IService1
    public class MyService:IService1,IService2{
        //functions
    }
五、一个接口多个实现
    public interface IService{}

    [AppService("ser1")]
    public class Service1:IService{
        //service1的实现
    }        
    [AppService("ser2")]
    public class Service2:IService{
        //service2的实现
    }

注入的时候,我们可以根据identifier指定具体哪个实现

    [Autowired("ser1")]
    IService service1; 

    [Autowired("ser2")]
    IService service2;
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

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
6.0.1 453 3/13/2022
6.0.0 435 2/9/2022
5.0.0 472 12/16/2021