当前位置:   article > 正文

Autofac学习之三种生命周期:InstancePerLifetimeScope、SingleInstance、InstancePerDependency

instanceperlifetimescope

InstancePerLifetimeScope:同一个Lifetime生成的对象是同一个实例


SingleInstance:单例模式,每次调用,都会使用同一个实例化的对象;每次都用同一个对象;


InstancePerDependency:默认模式,每次调用,都会重新实例化对象;每次请求都创建一个新的对象;

验证方法实现逻辑:在类的构造函数中,给属性赋值(GUID),通过判断属性值是否一致来判断 三种生命周期的效果。

先上图看结果:

1、InstancePerLifetimeScope 

 

2、SingleInstance

 

3、InstancePerDependency

整块代码实现如下:

  1. using Autofac;
  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. namespace SimpleAutofacConsole
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. //IAnimal animal = new Tiger();
  15. //animal.Show("普通new()对象");
  16. var builder = new ContainerBuilder();
  17. var dependencyRegistrar = new DependencyRegistrar();//(IDependencyRegistrar)Activator.CreateInstance(typeof(IDependencyRegistrar));
  18. dependencyRegistrar.Register(builder, null);
  19. var container = builder.Build();
  20. var animalIOC = container.Resolve<IAnimal>();
  21. //animalIOC.Show("Autofac方式实现new()对象");
  22. Console.WriteLine(animalIOC.Id);
  23. var animalIOC2 = container.Resolve<IAnimal>();
  24. //animalIOC2.Show("第二次从容器中实例化");
  25. Console.WriteLine(animalIOC2.Id);
  26. Console.WriteLine("开启新的生命周期");
  27. ILifetimeScope inner = container.BeginLifetimeScope();
  28. var myClass3 = inner.Resolve<IAnimal>();
  29. Console.WriteLine(myClass3.Id);
  30. var myClass4 = inner.Resolve<IAnimal>();
  31. Console.WriteLine(myClass4.Id);
  32. //var animalIOC = container.Resolve<Dog>();
  33. //animalIOC.Show("Autofac方式实现new()对象");
  34. Console.ReadLine();
  35. }
  36. }
  37. public interface IAnimal
  38. {
  39. void Show(string name);
  40. string Id { get; set; }
  41. }
  42. public class Tiger : IAnimal
  43. {
  44. private string _Id;
  45. public string Id { get { return this._Id; } set { Id = this._Id; } }
  46. public Tiger()
  47. {
  48. _Id = Guid.NewGuid().ToString();
  49. }
  50. public void Show(string name)
  51. {
  52. Console.WriteLine("老虎说:" + name);
  53. }
  54. }
  55. public class Dog : IAnimal
  56. {
  57. public string Id { get { return Guid.NewGuid().ToString(); } set { } }
  58. public void Show(string name)
  59. {
  60. Console.WriteLine("狗狗说:" + name);
  61. }
  62. }
  63. public class DependencyRegistrar : IDependencyRegistrar
  64. {
  65. public int Order { get { return 0; } }
  66. public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
  67. {
  68. // InstancePerLifetimeScope 同一个Lifetime生成的对象是同一个实例
  69. // SingleInstance 单例模式,每次调用,都会使用同一个实例化的对象;每次都用同一个对象
  70. // InstancePerDependency 默认模式,每次调用,都会重新实例化对象;每次请求都创建一个新的对象
  71. builder.RegisterType<Tiger>().As<IAnimal>().InstancePerLifetimeScope();
  72. //builder.RegisterType<Dog>().As<IAnimal>();//.PreserveExistingDefaults();
  73. }
  74. }
  75. public interface IDependencyRegistrar
  76. {
  77. void Register(ContainerBuilder builder,ITypeFinder typeFinder);
  78. int Order { get; }
  79. }
  80. public interface ITypeFinder
  81. {
  82. IList<Assembly> GetAssemblies();
  83. IEnumerable<Type> FindClassesOfType(Type assignTypeFrom, bool onlyConcreteClasses = true);
  84. IEnumerable<Type> FindClassesOfType(Type assignTypeFrom, IEnumerable<Assembly> assemblies, bool onlyConcreteClasses = true);
  85. IEnumerable<Type> FindClassesOfType<T>(bool onlyConcreteClasses = true);
  86. IEnumerable<Type> FindClassesOfType<T>(IEnumerable<Assembly> assemblies, bool onlyConcreteClasses = true);
  87. }
  88. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/程序代码艺术家2/article/detail/60702
推荐阅读
相关标签
  

闽ICP备14008679号