1、简介html
2、如何使用框架
2.一、基本使用ide
2.二、接口使用函数
2.三、 其余注入性能
2.四、 注入的生命周期ui
在上一篇文章中讲到替换默认服务容器,咱们选择了Autofacthis
Autofac---Autofac是一款IOC框架,比较于其余的IOC框架,如Spring.NET,Unity,Castle等等所包含的,它很轻量级性能上很是高。spa
咱们在.Net Core 中替换了自带的默认服务容器,选择采用Autofac,那么如何去使用它呢?code
2、如何使用htm
TestController控制器
public class TestController : Controller { private static Animals _animals; public IActionResult Index() { ViewBag.Animal = _animals.Cry(); return View(); } }
替换修改后的Startup.cs 中的ConfigureServices
public IServiceProvider ConfigureServices (IServiceCollection services) { services.AddMvc(); // Add other framework services // Add Autofac var containerBuilder = new ContainerBuilder(); containerBuilder.Populate(services); var container = containerBuilder.Build(); return new AutofacServiceProvider(container); }
1.一、 基本使用
建立 Animals 类
public class Animals { public string Cry() { return "小狗,汪汪汪"; } }
ConfigureServices 中添加注册
containerBuilder.RegisterType<Animals>();
TestController 控制器中添加构造函数
public TestController(Animals animals) { _animals = animals; }
运行起来看下
1.二、 接口使用
建立IAnimals.cs
public interface IAnimals { string Cry(); } public class DogCry : IAnimals { public string Cry() { return "小狗,汪汪汪"; } } public class CatCry : IAnimals { public string Cry() { return "小猫,喵喵喵"; } }
ConfigureServices 中添加注册
containerBuilder.RegisterType<DogCry>().As<IAnimals>();
TestController 控制器中添加构造函数并修改_animals为对应的类型
public TestController(IAnimals animals) { _animals = animals; }
运行起来
若是一个类型被屡次注册,以最后一个注册的为准
ConfigureServices 中添加注册
containerBuilder.RegisterType<DogCry>().As<IAnimals>();
containerBuilder.RegisterType<CatCry>().As<IAnimals>();
运行起来看下
1.三、 其余注入
一、 自动装配—从容器里面选择一个构造方法来建立对象
建立Cry类
public class Cry { public Cry() { voice= "小狗,汪汪汪"; } public Cry(string voices) { if (string.IsNullOrWhiteSpace(voices)) { voice = "旺旺旺"; } voice= $"小狗,{voices}"; } public Cry(string name, string voices):this(voices) { if (string.IsNullOrWhiteSpace(voices)) { voice = "旺旺旺"; } if (string.IsNullOrWhiteSpace(name)) { voice = "柴犬"; } voice= $"{name},{voices}"; } public static string voice { get; set; } }
ConfigureServices 中添加注册
containerBuilder.RegisterType<Cry>().UsingConstructor(typeof(string));
Autofac会默认从容器中选择参数最多的构造函数,若是想要指定选择的话能够指定UsingConstructor
二、 实例化注入
仍是上面的Cry类
ConfigureServices 中添加注册
var output = new Cry("叫声叫声"); containerBuilder.RegisterInstance(output).ExternallyOwned();
先对对象实例化而后注册,ExternallyOwned--配置组件,使容器永远不会处理实例。
修改Test控制器
public IActionResult Index() { ViewBag.Animal = Cry.voice; return View(); }
1.四、 注入的生命周期
1、 Transient(暂时生存期)--暂时生存期服务是每次从服务容器进行请求时建立的。 这种生存期适合轻量级、 无状态的服务。
2、 Scoped(范围生存期)--范围生存期服务是每一个客户端请求链接时建立的一次实例
3、 Singleton(单例生存期)--单例生存期会在程序第一次请求是建立一次实例,不会变化的
咱们来利用生成guid来看一下三个的生命周期有什么具体的不同
修改Test控制器
public class TestController : Controller { private static IGetTransient _getTransient; private static IGetScoped _getScoped; private static IGetSingleton _getSingleton; public TestController(IGetTransient getTransient, IGetScoped getScoped, IGetSingleton getSingleton) { _getTransient = getTransient; _getScoped = getScoped; _getSingleton = getSingleton; } public IActionResult Index() { ViewBag.getTransient = _getTransient.GuidItem(); ViewBag.getScoped = _getScoped.GuidItem(); ViewBag.getSingleton = _getSingleton.GuidItem(); return View(); } }
修改Index.cshtml
<div> <span>Transient:</span><span>@ViewBag.getTransient</span> </div> <div> <span>Scoped:</span><span>@ViewBag.getScoped</span> </div> <div> <span>Singleton:</span><span>@ViewBag.getSingleton</span> </div>
IGuid接口
public interface IGuid { Guid GuidItem(); } /// <summary> /// 暂存生存期 /// </summary> public interface IGetTransient : IGuid { } /// <summary> /// 范围生存期 /// </summary> public interface IGetScoped : IGuid { } /// <summary> /// 单例生存期 /// </summary> public interface IGetSingleton : IGuid { }
GuidServiceBase类
public class GuidServiceBase: IGuid { private readonly Guid _item; public GuidServiceBase() { _item = Guid.NewGuid(); } public Guid GuidItem() { return _item; } } /// <summary> /// 暂存生存期 /// </summary> public class GuidTransientService : GuidServiceBase, IGetTransient { } /// <summary> /// 范围生存期 /// </summary> public class GuidScopedService : GuidServiceBase, IGetScoped { } /// <summary> /// 单例生存期 /// </summary> public class GuidSingletonService : GuidServiceBase, IGetSingleton { }
ConfigureServices 中添加注册
containerBuilder.RegisterType<GuidTransientService>().As<IGetTransient>(); containerBuilder.RegisterType<GuidScopedService>().As<IGetScoped>().InstancePerLifetimeScope();
containerBuilder.RegisterType<GuidSingletonService>().As<IGetSingleton>().SingleInstance();
运行起来发现Singleton(单例生存期)没有变化,仅产生了一个实例,可是Scoped(范围生存期) 变化的不同,按照理论来讲应该刷新以后会变化,可是两边应该会是同样的值。--(由于两个页面依然是独立的,并非一次请求)。咱们换另外一种方式验证这个
修改Test控制器—新增Guid
public IActionResult Guid() { return View(); }
添加Guid.cshtml—经过inject注入依赖
@{ Layout = null; } @inject WebApplication3.IGetTransient TransientService @inject WebApplication3.IGetScoped GuidScopedService @inject WebApplication3.IGetSingleton GuidSingletonService
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Guid</title> </head> <body> <div class="row"> <div> <h2>GuidItem Shows</h2> <h3>TransientItem: @TransientService.GuidItem()</h3> <h3>ScopedItem: @GuidScopedService.GuidItem()</h3> <h3>SingletonItem: @GuidSingletonService.GuidItem()</h3> </div> </div> </body> </html>
修改Index.cshtml
@{ ViewData["Title"] = "Index"; } <h1>Index</h1> @Html.Partial("Guid") <h1>Guid</h1> @Html.Partial("Guid")
运行而后打开两个页面
咱们再次彻底吻合的,暂时生命周期在每次使用的时候的Guid(实例)都是变化的,范围生命周期在同一个请求范围内Guid是不变化的,不一样请求的Guid是会发生变化的。可是单例生命周期的Guid从程序开始就不会发生变化的。