Autofac使用(1)

            1.Nuget引入程序包
            2.得到容器的建造者
            3.配置抽象和具体类之间的关系
            4.Build一下得到容器实例
            5.基于容器来获取对象的实例了

Autofac使用(1)_第1张图片

1、基础使用

ContainerBuilder containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType().As();
IContainer container = containerBuilder.Build();
IMicrophone microphone = container.Resolve();

2、多种注册方式

Autofac使用(1)_第2张图片

            关于Autofac容器的多种注册
            {

                注册抽象和具体普通类

                {
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterType().As();
                    IContainer container = containerBuilder.Build();
                    IMicrophone microphone = container.Resolve();
                }
                注册一个具体的实例
                {
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterInstance(new Microphone());
                    IContainer container = containerBuilder.Build();
                    IMicrophone microphone = container.Resolve();
                }

                {
                    注册一段业务逻辑
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterType().As();
                    containerBuilder.Register(context =>
                    {
                        //这里的业务逻辑负责创建出IPower 的实例---可以给一个入口,我们自己来创建对象的实例
                        IMicrophone microphone = context.Resolve();
                        IPower power = new Power(microphone);

                        return power;
                    });
                    IContainer container = containerBuilder.Build();
                    IPower power = container.Resolve();
                }

                注册泛型
                {
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterGeneric(typeof(List<>)).As(typeof(IList<>));
                    containerBuilder.RegisterType().As();
                    IContainer container = containerBuilder.Build();
                    IList microphonelist = container.Resolve>();
                }

                注册程序集
                {
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    Assembly interfaceAssembly = Assembly.LoadFrom("Advanced.NET6.Business.Interfaces.dll");
                    Assembly serviceAssembly = Assembly.LoadFrom("Advanced.NET6.Business.Services.dll");
                    containerBuilder.RegisterAssemblyTypes(interfaceAssembly, serviceAssembly).AsImplementedInterfaces();
                    IContainer container = containerBuilder.Build();
                    IEnumerable microphonelist = container.Resolve>();
                }

            }

3、构造函数注入

                {
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterType().As();
                    containerBuilder.RegisterType().As();
                    IContainer container = containerBuilder.Build();
                    IPower power = container.Resolve();
                }

                如果有多个构造函数,默认选择构造函数参数最多构造函数了
                如果希望选择其中只有一个构造函数参数的构造函数
                {
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterType()
                        .As();
                    containerBuilder.RegisterType()
                         .UsingConstructor(typeof(IMicrophone), typeof(IMicrophone)) //选择类型为IMicrophone 且参数的数量只有一个的构造函数
                        .As();
                    IContainer container = containerBuilder.Build();
                    IPower power = container.Resolve();
                }

4、属性注入

                //属性注入
                {
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterType().As();
                    containerBuilder.RegisterType().As();
                    containerBuilder.RegisterType().As();
                    containerBuilder.RegisterType().As()
                        .PropertiesAutowired(); //表示要支持属性注入: 在对象创建出来以后,自动给属性创建实例,赋值上去

                    IContainer container = containerBuilder.Build();
                    IPhone iPhone = container.Resolve();
                }

Autofac使用(1)_第3张图片

                //属性注入--支持PropertySelector
                {
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterType().As();
                    containerBuilder.RegisterType().As();
                    containerBuilder.RegisterType().As();
                    containerBuilder.RegisterType().As()
                        .PropertiesAutowired(new CusotmPropertySelector()); //表示要支持属性注入: 在对象创建出来以后,自动给属性创建实例,赋值上去

                    IContainer container = containerBuilder.Build();
                    IPhone iPhone = container.Resolve();
                }
 [AttributeUsage(AttributeTargets.Property)]
 public class CusotmSelectAttribute:Attribute
 {
 }

 public class CusotmPropertySelector : IPropertySelector
 {
     public bool InjectProperty(PropertyInfo propertyInfo, object instance)
     {
         //在这里就是判断哪些属性是需要做属性注入的
         return propertyInfo.CustomAttributes.Any(c => c.AttributeType == typeof(CusotmSelectAttribute));
     }
 }

 5、方法注入

Autofac使用(1)_第4张图片

 //方法注入
 {
     ContainerBuilder containerBuilder = new ContainerBuilder();
     containerBuilder.RegisterType().As();
     containerBuilder.RegisterType().As();
     containerBuilder.RegisterType().As();
     containerBuilder.RegisterType().As()
         .OnActivated(activa =>
     {
         IPower power = activa.Context.Resolve();
         activa.Instance.Init123456678890(power);
     });

     IContainer container = containerBuilder.Build();
     IPhone iPhone = container.Resolve();
 }

你可能感兴趣的:(java,开发语言)