.Net Core Autofac

  • .Net Core Autofac已关闭评论
  • 169 次浏览
  • A+
所属分类:.NET技术
摘要

Autofac 是一款超赞的.NET IoC 容器 . 它管理类之间的依赖关系, 从而使 应用在规模及复杂性增长的情况下依然可以轻易地修改 . 它的实现方式是将常规的.net类当做 组件 处理.

Autofac

Autofac 是一款超赞的.NET IoC 容器 . 它管理类之间的依赖关系, 从而使 应用在规模及复杂性增长的情况下依然可以轻易地修改 . 它的实现方式是将常规的.net类当做 组件 处理.

控制反转背后的核心思想是, 我们不再将应用中的类捆绑在一起, 让类自己去 “new up” 他们的依赖, 而是反过来在类的构造方法中将依赖传递进去.

中文文档

引用

<ItemGroup>     <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />     <PackageReference Include="Castle.Core" Version="5.1.1" /> </ItemGroup> 

Program.cs

//添加 autofac builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); builder.Host.ConfigureContainer<ContainerBuilder>(builder => {     builder.RegisterModule<AutofacConfig>(); }); 

AutofacConfig.cs

using Autofac; using Autofac.Extras.DynamicProxy; using Cnpc.Com.Ioc.Bll; using Cnpc.Com.Ioc.Dal; using Cnpc.Com.Ioc.IBll; using Cnpc.Com.Ioc.IDal; using Cnpc.Com.Ioc.Tools;  namespace Cnpc.Com.AutofacTest.Autofac {     public class AutofacConfig : Module     {          protected override void Load(ContainerBuilder builder)         {             //ioc             builder.RegisterType<Pencil>().As<IWrite>(); //构造函数注入             builder.RegisterType<Student>().As<IStudent>().PropertiesAutowired(new PropertySelection()).OnActivated(it =>             {                 it.Instance.SetIWriten(it.Context.Resolve<IWrite>());             }).EnableInterfaceInterceptors(); //属性注入 + 指定属性注入 + 指定方法注入 + 开启Aop              //aop             builder.RegisterType<AutofacInterceptor>();             builder.RegisterType<TestAutofacInterceptor>();         }     } } 

Aop AutofacInterceptor.cs

using Castle.DynamicProxy; using Microsoft.Extensions.Logging; using NLog;  namespace Cnpc.Com.Ioc.Tools {     public class AutofacInterceptor : IInterceptor     {         //支持NOP             ILogger<AutofacInterceptor> logger;         public AutofacInterceptor(ILogger<AutofacInterceptor> logger)         {             this.logger = logger;         }         public void Intercept(IInvocation invocation)         {                         {                 logger.LogInformation("执行前");                 Console.WriteLine("执行前: " + DateTime.Now.ToString() + "此消息来自:" + this.GetType().FullName) ;             }             invocation.Proceed();              {                 Console.WriteLine("执行后: " + DateTime.Now.ToString() + "此消息来自:" + this.GetType().FullName);             }         }     } } 

测试接口+实现

IStudent.cs

using Autofac.Extras.DynamicProxy; using Cnpc.Com.Ioc.IDal; using Cnpc.Com.Ioc.Tools; using Microsoft.Extensions.Logging;  namespace Cnpc.Com.Ioc.IBll {     [Intercept(typeof(TestAutofacInterceptor))]     public interface IStudent     {         IWrite Write { get; set; }         IWrite Write1 { get; set; }         IWrite Write2 { get; set; }         IWrite Write3 { get; set; }         IWrite Write4 { get; set; }         IWrite Write5 { get; set; }          ILogger<IStudent> logger { get; set; }          IWrite Write6 { get; set; }         IWrite WriteContainer { get; set; }          void SetIWriten(IWrite write);          void DoHomeWork(IWrite writen);         void Calligraphy(IWrite writen);     } } 

Student.cs

using Autofac.Extras.DynamicProxy; using Cnpc.Com.Ioc.IBll; using Cnpc.Com.Ioc.IDal; using Cnpc.Com.Ioc.Tools; using Microsoft.Extensions.Logging; using NLog;  namespace Cnpc.Com.Ioc.Bll {     [Intercept(typeof(AutofacInterceptor))]     public class Student : IStudent     {         [PropertySelecction]         public IWrite Write { get; set ; }         public IWrite WriteContainer { get ; set ; }         public IWrite Write1 { get ; set ; }         public IWrite Write2 { get ; set ; }         public IWrite Write3 { get ; set ; }         public IWrite Write4 { get ; set ; }         public IWrite Write5 { get ; set ; }         public IWrite Write6 { get ; set ; }                   //支持Nlog 属性注入         [PropertySelecction]         public ILogger<IStudent> logger { get ; set ; }          public void Calligraphy(IWrite writen)         {             writen.Write();         }          public void DoHomeWork(IWrite writen)         {             logger.LogInformation("wow");             writen.Write();         }          public void SetIWriten(IWrite write)         {             this.WriteContainer = write;         }     } }