Autofac學習之三種生命周期:InstancePerLifetimeScope、SingleInstance、InstancePerDependency


InstancePerLifetimeScope:同一個Lifetime生成的對象是同一個實例


SingleInstance:單例模式,每次調用,都會使用同一個實例化的對象;每次都用同一個對象;


InstancePerDependency:默認模式,每次調用,都會重新實例化對象;每次請求都創建一個新的對象;

 

驗證方法實現邏輯:在類的構造函數中,給屬性賦值(GUID),通過判斷屬性值是否一致來判斷 三種生命周期的效果。

 

先上圖看結果:

1、InstancePerLifetimeScope 

 

 

2、SingleInstance

 

 

3、InstancePerDependency

 

整塊代碼實現如下:

using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SimpleAutofacConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            //IAnimal animal = new Tiger();
            //animal.Show("普通new()對象");

            var builder = new ContainerBuilder();
            var dependencyRegistrar = new DependencyRegistrar();//(IDependencyRegistrar)Activator.CreateInstance(typeof(IDependencyRegistrar));
            dependencyRegistrar.Register(builder, null);

            var container = builder.Build();

            var animalIOC = container.Resolve<IAnimal>();
            //animalIOC.Show("Autofac方式實現new()對象");
            Console.WriteLine(animalIOC.Id);

            var animalIOC2 = container.Resolve<IAnimal>();
            //animalIOC2.Show("第二次從容器中實例化");
            Console.WriteLine(animalIOC2.Id);

            Console.WriteLine("開啟新的生命周期");
            ILifetimeScope inner = container.BeginLifetimeScope();
            var myClass3 = inner.Resolve<IAnimal>();
            Console.WriteLine(myClass3.Id);
            var myClass4 = inner.Resolve<IAnimal>();
            Console.WriteLine(myClass4.Id);



            //var animalIOC = container.Resolve<Dog>();
            //animalIOC.Show("Autofac方式實現new()對象");



            Console.ReadLine();
        }
    }
    public interface IAnimal
    {
        void Show(string name);
        string Id { get; set; }
    }
    public class Tiger : IAnimal
    {
        private string _Id;
        public string Id { get { return this._Id; } set { Id = this._Id; } }
        public Tiger()
        {
            _Id = Guid.NewGuid().ToString();
        }

        public void Show(string name)
        {
            Console.WriteLine("老虎說:" + name);
        }
    }
    public class Dog : IAnimal
    {
        public string Id { get { return Guid.NewGuid().ToString(); } set { } }

        public void Show(string name)
        {
            Console.WriteLine("狗狗說:" + name);
        }
    }
    public class DependencyRegistrar : IDependencyRegistrar
    {
        public int Order { get { return 0; } }

        public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            // InstancePerLifetimeScope 同一個Lifetime生成的對象是同一個實例
            // SingleInstance 單例模式,每次調用,都會使用同一個實例化的對象;每次都用同一個對象
            // InstancePerDependency 默認模式,每次調用,都會重新實例化對象;每次請求都創建一個新的對象
            builder.RegisterType<Tiger>().As<IAnimal>().InstancePerLifetimeScope();
            //builder.RegisterType<Dog>().As<IAnimal>();//.PreserveExistingDefaults();
        }
    }

    public interface IDependencyRegistrar
    {
        void Register(ContainerBuilder builder,ITypeFinder typeFinder);

        int Order { get; }
    }


    public interface ITypeFinder
    {
        IList<Assembly> GetAssemblies();

        IEnumerable<Type> FindClassesOfType(Type assignTypeFrom, bool onlyConcreteClasses = true);

        IEnumerable<Type> FindClassesOfType(Type assignTypeFrom, IEnumerable<Assembly> assemblies, bool onlyConcreteClasses = true);

        IEnumerable<Type> FindClassesOfType<T>(bool onlyConcreteClasses = true);

        IEnumerable<Type> FindClassesOfType<T>(IEnumerable<Assembly> assemblies, bool onlyConcreteClasses = true);
    }
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM