IOC概念

Inversion of Control 控制反转(将新建对象的控制权交给容器)

实现方式

Dependency Injection 依赖注入(在运行时将对象注入到指定位置) -常见
Dependency Lookup 依赖查找

使用环境

对于所有新建对象照成的强耦合,使用IOC框架可以降低代码的耦合
(个人结合资料的理解)

以上概念引用:https://segmentfault.com/a/1190000020361476 作者:CoderV的进阶笔记
发布时间:2019-09-11

官方资料

官方网址:https://autofac.org/
快速使用文档:https://autofac.readthedocs.io/en/latest/getting-started/index.html

快速入门代码

参照:https://www.cnblogs.com/zcqiand/p/14257650.html
作者:南荣相如谈编程
对官方示例进行了简化
运行环境:.net core 3.1

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. //构造器
  6. var builder = new ContainerBuilder();
  7. //注册
  8. builder.RegisterType<MyModel1>();
  9. builder.RegisterType<MyModel2>();
  10. //容器
  11. IContainer container = builder.Build();
  12. //使用范围
  13. using (var scope = container.BeginLifetimeScope())
  14. {
  15. //解析实例
  16. var actor = scope.Resolve<MyModel1>();
  17. actor.Output();
  18. }
  19. Console.ReadKey();
  20. }
  21. }
  22. interface MyModel
  23. {
  24. void Output();
  25. }
  26. class MyModel1 : MyModel
  27. {
  28. public void Output()
  29. {
  30. Console.WriteLine("MyModel1");
  31. }
  32. }
  33. class MyModel2 : MyModel
  34. {
  35. public void Output()
  36. {
  37. Console.WriteLine("MyModel2");
  38. }
  39. }

添加接口使用

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. //构造器
  6. var builder = new ContainerBuilder();
  7. //注册类型并指定名称
  8. builder.RegisterType<MyModel1>().Named<MyModel>("my1");
  9. builder.RegisterType<MyModel2>().Named<MyModel>("my2");
  10. //接口只有一个实例时可以不用名称
  11. builder.RegisterType<MyTest>().As<Testfac>();
  12. //容器
  13. IContainer container = builder.Build();
  14. //使用范围
  15. using (var scope = container.BeginLifetimeScope())
  16. {
  17. //解析实例
  18. var actor = scope.ResolveNamed<MyModel>("my1");
  19. var actor2 = scope.Resolve<Testfac>();
  20. actor.Output();
  21. actor2.Ts();
  22. }
  23. Console.ReadKey();
  24. }
  25. }
  26. interface MyModel
  27. {
  28. void Output();
  29. }
  30. class MyModel1 : MyModel
  31. {
  32. public void Output()
  33. {
  34. Console.WriteLine("MyModel1");
  35. }
  36. }
  37. class MyModel2 : MyModel
  38. {
  39. public void Output()
  40. {
  41. Console.WriteLine("MyModel2");
  42. }
  43. }
  44. interface Testfac
  45. {
  46. public void Ts();
  47. }
  48. class MyTest : Testfac
  49. {
  50. public void Ts()
  51. {
  52. Console.WriteLine("MyTest");
  53. }
  54. }

输出
image.png

从程序集扫描并装载(注册)

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. //构造器
  6. var builder = new ContainerBuilder();
  7. //注册类型并指定名称
  8. //builder.RegisterType<MyModel1>().Named<MyModel>("my1");
  9. //builder.RegisterType<MyModel2>().Named<MyModel>("my2");
  10. //接口只有一个实例时可以不用名称
  11. //builder.RegisterType<MyTest>().As<Testfac>();
  12. //从程序集扫描并注册
  13. var dataAccess = System.Reflection.Assembly.GetExecutingAssembly();
  14. builder.RegisterAssemblyTypes(dataAccess).Where(t => t.Name.StartsWith("MyModel1")).Named<MyModel>("my1");
  15. builder.RegisterAssemblyTypes(dataAccess).Where(t => t.Name.StartsWith("MyModel2")).Named<MyModel>("my2");
  16. builder.RegisterAssemblyTypes(dataAccess)
  17. .Where(t => t.Name.StartsWith("MyTest"))
  18. .PublicOnly() /*只扫描公开类*/
  19. .As<Testfac>();
  20. //容器
  21. IContainer container = builder.Build();
  22. //使用范围
  23. using (var scope = container.BeginLifetimeScope())
  24. {
  25. //解析实例
  26. var actor = scope.ResolveNamed<MyModel>("my1");
  27. var actor2 = scope.Resolve<Testfac>();
  28. actor.Output();
  29. actor2.Ts();
  30. }
  31. Console.ReadKey();
  32. }
  33. }
  34. interface MyModel
  35. {
  36. void Output();
  37. }
  38. class MyModel1 : MyModel
  39. {
  40. public void Output()
  41. {
  42. Console.WriteLine("MyModel1");
  43. }
  44. }
  45. class MyModel2 : MyModel
  46. {
  47. public void Output()
  48. {
  49. Console.WriteLine("MyModel2");
  50. }
  51. }
  52. interface Testfac
  53. {
  54. public void Ts();
  55. }
  56. public class MyTest : Testfac
  57. {
  58. public void Ts()
  59. {
  60. Console.WriteLine("MyTest");
  61. }
  62. }