Autofac支持属性注入
一、支持属性注入
1、创建控制台程序
2、Nuget引入AutoFace
3、创建接口层Business.IServices,包含以下几个接口
IMicrophone.cs
1 namespace Business.IServices 2 { 3 public interface IMicrophone 4 { 5 6 } 7 }
IPower.cs
1 namespace Business.IServices 2 { 3 public interface IPower 4 { 5 } 6 }
IHeadphone.cs
1 namespace Business.IServices 2 { 3 public interface IHeadphone 4 { 5 } 6 }
4、创建具体类层,分别继承上面接口
Microphone.cs
1 using Business.IServices; 2 3 namespace Business.Services 4 { 5 public class Microphone : IMicrophone 6 { 7 public Microphone() 8 { 9 Console.WriteLine($"{this.GetType().Name}被构造.."); 10 } 11 } 12 }
Power.cs
1 using Business.IServices; 2 3 namespace Business.Services 4 { 5 public class Power :IPower 6 { 7 8 public Power(IMicrophone microphone) 9 { 10 Console.WriteLine($"{this.GetType().Name}被构造了"); 11 } 12 13 public Power(IMicrophone microphone, IMicrophone microphone2) 14 { 15 Console.WriteLine($"{this.GetType().Name}被构造了"); 16 } 17 } 18 }
Headphone.cs
1 using Business.IServices; 2 3 namespace Business.Services 4 { 5 public class Headphone : IHeadphone 6 { 7 public IMicrophone microphone { get; set; } 8 9 public Headphone(IPower power) 10 { 11 Console.WriteLine($"{this.GetType().Name}被构造..."); 12 } 13 } 14 }
5、修改Program.cs
1 { 2 //属性注入 3 ContainerBuilder containerBuilder = new ContainerBuilder(); 4 containerBuilder.RegisterType<Microphone>().As<IMicrophone>(); 5 containerBuilder.RegisterType<Power>().As<IPower>(); 6 containerBuilder.RegisterType<Headphone>().As<IHeadphone>(); 7 containerBuilder.RegisterType<Headphone>().As<IHeadphone>().PropertiesAutowired(); //支持属性注入 8 IContainer container = containerBuilder.Build(); 9 IHeadphone headphone = container.Resolve<IHeadphone>(); 10 }
6、设置断点,执行程序
可以发现属性public IMicrophone microphone { get; set; }被创建
二、指定属性才能注入
以上方法是所有属性都可以注入,我们可以指定有标识的属性才注入
1、创建CustomSelectAttribute
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Business.FrameWork.AutofacExt 8 { 9 [AttributeUsage(AttributeTargets.Property)] //只能用在属性上 10 public class CustomSelectAttribute : Attribute 11 { 12 13 } 14 }
2、创建CustomPropertySelector
1 using Autofac.Core; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Reflection; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace Business.FrameWork.AutofacExt 10 { 11 public class CustomPropertySelector : IPropertySelector 12 { 13 14 public bool InjectProperty(PropertyInfo propertyInfo, object instance) 15 { 16 //判断哪里属性需要属性注入 17 return propertyInfo.CustomAttributes.Any(c => c.AttributeType == typeof(CustomSelectAttribute)); 18 } 19 } 20 }
3、修改Headphone
1 using Business.FrameWork.AutofacExt; 2 using Business.IServices; 3 4 namespace Business.Services 5 { 6 public class Headphone : IHeadphone 7 { 8 [CustomSelectAttribute] 9 public IMicrophone microphone { get; set; } 10 11 public IPower power1 { get; set; } 12 13 public Headphone(IPower power) 14 { 15 Console.WriteLine($"{this.GetType().Name}被构造..."); 16 } 17 } 18 }
4、修改Program.cs
1 //指定属性才注入 2 ContainerBuilder containerBuilder = new ContainerBuilder(); 3 containerBuilder.RegisterType<Microphone>().As<IMicrophone>(); 4 containerBuilder.RegisterType<Power>().As<IPower>(); 5 containerBuilder.RegisterType<Headphone>().As<IHeadphone>(); 6 containerBuilder.RegisterType<Headphone>().As<IHeadphone>().PropertiesAutowired(new CustomPropertySelector()); //支持属性注入,指定选择器 7 IContainer container = containerBuilder.Build(); 8 IHeadphone headphone = container.Resolve<IHeadphone>();
5、设置断点,运行程序
可以发现
[CustomSelectAttribute]
public IMicrophone microphone { get; set; }
指定了[CustomSelectAttribute]才被创建实例,
public IPower power1 { get; set; }没有被创建。
三、方法注入
1、headphone类添加方法Init
1 using Business.FrameWork.AutofacExt; 2 using Business.IServices; 3 4 namespace Business.Services 5 { 6 public class Headphone : IHeadphone 7 { 8 //[CustomSelectAttribute] 9 //public IMicrophone microphone { get; set; } 10 11 //public IPower power1 { get; set; } 12 13 public Headphone(IPower power) 14 { 15 Console.WriteLine($"{this.GetType().Name}被构造..."); 16 } 17 18 public void Init(IPower power2) 19 { 20 Console.WriteLine("方法注入"); 21 } 22 } 23 }
2、修改Program.cs
1 { 2 //方法注入 3 ContainerBuilder containerBuilder = new ContainerBuilder(); 4 containerBuilder.RegisterType<Microphone>().As<IMicrophone>(); 5 containerBuilder.RegisterType<Power>().As<IPower>(); 6 containerBuilder.RegisterType<Headphone>().As<IHeadphone>(); 7 containerBuilder.RegisterType<Headphone>().As<IHeadphone>().OnActivated( 8 activate => 9 { 10 IPower power = activate.Context.Resolve<IPower>(); 11 activate.Instance.Init(power); //方法注入 12 }); 13 IContainer container = containerBuilder.Build(); 14 IHeadphone headphone = container.Resolve<IHeadphone>(); 15 }