diff --git a/Readme.md b/Readme.md index b0df53f..2832504 100644 --- a/Readme.md +++ b/Readme.md @@ -1,60 +1,65 @@ -**使用说明** - -1. 安装:可以通过下载源码安装,也可以通过nuget包安装。 -1. 引入名字空间 -~~~ -using Microsoft.Extensions.DependencyInjection; -using Falcon.DI -~~~ -1. 初始化容器:使用UseFalconDI方法注册所有服务。 -~~~ -IServiceCollection ser = new ServiceCollection(); -ser.UseFalconDI(); -~~~ -1. 添加注册特性,服务可以注册到基础接口或自身。 -~~~ -public interface IMyInterface -{ - string Getname(); -} - -[FalconDIRegister] -public class MyClassInterfaces:IMyInterface -{ - public string Getname() { - return this.GetType().Name; - } -} -~~~ -1. 注入可以使用ServiceCollection获取注册的服务。 - ~~~ - using(var pd = ser.BuildServiceProvider()) { - var service = pd.GetServices(); - // Do something - } - - ~~~ - 或者使用构造注入 - ~~~ - public interface INfi1 { } - public interface INfi2 { } - - [FalconDIRegister] - public class NotFull:INfi1 - { - } - - [FalconDIRegister(typeof(NotFullObj))] - public class NotFullObj - { - public INfi1 F1 { get; set; } - public INfi2 F2 { get; set; } - - public NotFullObj(INfi1 f1,INfi2 f2=null) { - this.F1 = f1; - this.F2 = f2; - } - } - ~~~ -以上例子中首先注入了NotFull类型实现INfi1服务,然后又注册了NotFullObj类型,并通过构造注入方式消费了INfi1服务。INfi2因为没有注册所以使用默认值跳过注入。 - +**使用说明** + +1. 安装:通过nuget包安装。 + `https://www.nuget.org/packages/Falcon.DI/` +1. 引入名字空间 + `using Falcon.DI` +1. 初始化容器:使用UseFalconDI方法注册所有服务。 + ``` + IServiceCollection ser = new ServiceCollection(); + ser.UseFalconDI(); + ``` +1. 添加注册特性,服务可以通过`FalconDIRegisterAttribute`特性进行注册。 + 注册规则如下: + > 1. 如果提供了服务,例如`FalconDIRegister(Typeof(IMyInterface))`则注册到提供的服务。 + > 2. 如果未提供任何服务类,则注册到类型实现的所有接口、非object的基类和类型本身。 + > 3. 如果类型本身不可以通过反射创建,则不会注册。 + + ``` + public interface IMyInterface + { + string Getname(); + } + + [FalconDIRegister] + public class MyClassInterfaces:IMyInterface + { + public string Getname() { + return this.GetType().Name; + } + } + ``` +1. 注入可以使用ServiceCollection获取注册的服务。 + ~~~ + using(var pd = ser.BuildServiceProvider()) { + var service = pd.GetServices(); + // Do something + } + + ~~~ + 或者使用构造注入 + ~~~ + public interface INfi1 { } + public interface INfi2 { } + + [FalconDIRegister] + public class NotFull:INfi1 + { + } + + [FalconDIRegister(typeof(NotFullObj))] + public class NotFullObj + { + public INfi1 F1 { get; set; } + public INfi2 F2 { get; set; } + + public NotFullObj(INfi1 f1,INfi2 f2=null) { + this.F1 = f1; + this.F2 = f2; + } + } + ~~~ +以上例子中首先注入了NotFull类型,该类型提供INfi1服务,然后又注册了NotFullObj类型,并通过构造注入方式注入INfi1服务。INfi2因为没有注册所以使用默认值。 + +1. 关于生命周期。 + 提供了三种[实例生命周期](http://39.105.71.191/Falcon/Falcon.Di/src/branch/master/Falcon.DI/ServiceLifetime.cs),默认为`Scoped`。