Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Autofac;
  9.  
  10. namespace Example.Common
  11. {
  12. public static class IoC
  13. {
  14. private static IContainer _container { get; set; }
  15. private static ContainerBuilder builder = new ContainerBuilder();
  16. static List<Assembly> allAssemblies = new List<Assembly>();
  17.  
  18. public static IContainer Container
  19. {
  20. get
  21. {
  22. if (_container == null)
  23. {
  24. Init();
  25. }
  26. return _container;
  27. }
  28. }
  29.  
  30. public static void Init()
  31. {
  32. LoadEveryAssembly();
  33.  
  34. foreach (Assembly a in allAssemblies)
  35. {
  36. builder.RegisterAssemblyTypes(a)
  37. .Where(t => t.Name.EndsWith("Context")).AsSelf().SingleInstance();
  38.  
  39. builder.RegisterAssemblyTypes(a)
  40. .Where(t => t.Name.EndsWith("Repository"));
  41.  
  42. builder.RegisterAssemblyTypes(a)
  43. .Where(t => t.Name.EndsWith("Manager"))
  44. .AsImplementedInterfaces()
  45. .AsSelf();
  46.  
  47. builder.RegisterAssemblyTypes(a)
  48. .Where(t => t.Name.EndsWith("Query")).AsImplementedInterfaces()
  49. .AsSelf();
  50.  
  51. builder.RegisterAssemblyTypes(a)
  52. .Where(t => t.Name.EndsWith("Service"))
  53. .AsImplementedInterfaces()
  54. .AsSelf();
  55. }
  56. _container = builder.Build();
  57. }
  58.  
  59. private static void LoadEveryAssembly()
  60. {
  61. string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  62. foreach (string dll in Directory.GetFiles(path, "*.dll"))
  63. allAssemblies.Add(Assembly.LoadFile(dll));
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement