1 Star 0 Fork 18

甲乙 / Autowired.Core

forked from lucky7 / Autowired.Core 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Autowired.Core

介绍

Make the default DI container inject fields and properties easily . (based on .netstandard2.0)

安装教程

PM> Install-Package 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);
        }
    }

二、生命周期

    [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;

空文件

简介

基于.netstandard2.0实现的一个支持属性和字段注入的手脚架 展开 收起
C#
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/jiayiay/Autowired.Core.git
git@gitee.com:jiayiay/Autowired.Core.git
jiayiay
Autowired.Core
Autowired.Core
master

搜索帮助