第三节 Autofac的生命周期

Autofac生命周期

  1. InstancePerDependency
  2. SingleInstance
  3. InstancePerLifetimeScope
  4. InstancePerMatchingLifetimeScope

A)InstancePerDependency
在其他容器中也称为“瞬态”或“工厂”。使用per-dependency关系范围,将从服务的每个请求返回唯一的实例。如果没有指定其他选项,这是默认值。

using Autofac;
using Autofac.Core;
using System;

namespace ConsoleApp1
{
    public class ConsoleInput
    {
        public void Get()
        {
            Console.Write("Input");
        }
    }

    class Program
    {
        private static IContainer container { get; set; }
        static void Main(string[] args)
        {
            var containerBuilder = new ContainerBuilder();
            containerBuilder.RegisterType<ConsoleInput>().InstancePerDependency();
            container = containerBuilder.Build();

            using (var scope = container.BeginLifetimeScope())
            {
                for (int i = 0; i < 3; i++)
                {
                    var temp = scope.Resolve<ConsoleInput>();
                    Console.WriteLine(temp.GetHashCode().ToString());
                }  
            }
          
            Console.ReadKey();
        }
    }
}

控制台结果:
在这里插入图片描述
B)SingleInstance
使用单实例作用域,从根和所有嵌套作用域中的所有请求返回一个实例。当您解析单个实例组件时,无论您在何处请求它,您总是获得同一个实例。

 class Program
    {
        private static IContainer container { get; set; }
        static void Main(string[] args)
        {
            var containerBuilder = new ContainerBuilder();
            containerBuilder.RegisterType<ConsoleInput>().SingleInstance();
            container = containerBuilder.Build();

            using (var scope = container.BeginLifetimeScope())
            {
                for (int i = 0; i < 3; i++)
                {
                    var temp = scope.Resolve<ConsoleInput>();
                    Console.WriteLine(temp.GetHashCode().ToString());
                }  
            }
          
            Console.ReadKey();
        }
    }

控制台结果:
在这里插入图片描述
C) InstancePerLifetimeScope
每个生命周期范围将获得一个注册依赖的新实例。

 class Program
    {
        private static IContainer container { get; set; }
        static void Main(string[] args)
        {
            var containerBuilder = new ContainerBuilder();
            containerBuilder.RegisterType<ConsoleInput>().InstancePerLifetimeScope();
            container = containerBuilder.Build();

            using (var scope = container.BeginLifetimeScope())
            {
                for (int i = 0; i < 2; i++)
                {
                    var temp = scope.Resolve<ConsoleInput>();
                    Console.WriteLine(temp.GetHashCode().ToString());
                }  
            }

            Console.WriteLine();

            using (var scope = container.BeginLifetimeScope())
            {
                for (int i = 0; i < 2; i++)
                {
                    var temp = scope.Resolve<ConsoleInput>();
                    Console.WriteLine(temp.GetHashCode().ToString());
                }
            }

            Console.ReadKey();
        }
    }

控制台结果:
第三节 Autofac的生命周期_第1张图片
D) InstancePerMatchingLifetimeScope
这类似于上面的“每个生命周期范围的实例”概念,但允许对实例共享进行更精确的控制。
当创建嵌套生命期作用域时,您可以“标记”或“命名”作用域。具有每个per-matching-lifetime范围的组件最多将具有一个与给定名称匹配的每个嵌套生命周期范围的单个实例。

 class Program
    {
        private static IContainer container { get; set; }
        static void Main(string[] args)
        {
            var containerBuilder = new ContainerBuilder();
            containerBuilder.RegisterType<ConsoleInput>().InstancePerMatchingLifetimeScope("Test");
            container = containerBuilder.Build();

            using (var scope = container.BeginLifetimeScope("Test"))
            {
                for (int i = 0; i < 2; i++)
                {
                    var temp = scope.Resolve<ConsoleInput>();
                    Console.WriteLine(temp.GetHashCode().ToString());
                }  
            }

            Console.WriteLine();

            using (var scope = container.BeginLifetimeScope("Test"))
            {
                for (int i = 0; i < 2; i++)
                {
                    var temp = scope.Resolve<ConsoleInput>();
                    Console.WriteLine(temp.GetHashCode().ToString());
                }
            }

            Console.ReadKey();
        }
    }

控制台结果:
第三节 Autofac的生命周期_第2张图片

你可能感兴趣的:(autofac,c#,ioc)