IocContainer.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Autofac;
  2. using Microsoft.AspNetCore.Http;
  3. using Serilog;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace DW5S.Repostory
  10. {
  11. public static class IocContainer
  12. {
  13. public static void Init(ILifetimeScope container)
  14. {
  15. Container = container;
  16. Logger=GetService<ILogger>();
  17. }
  18. private static ILifetimeScope Container;
  19. public static ILogger Logger { get; private set; }
  20. public static IUnitOfWork UnitOfWork
  21. {
  22. get
  23. {
  24. return GetService<IUnitOfWork>();
  25. }
  26. }
  27. public static T GetService<T>() where T : class
  28. {
  29. var scop = Container.BeginLifetimeScope();
  30. return scop.Resolve<T>();
  31. }
  32. public static T GetService<T>(object parameter) where T : class
  33. {
  34. var scop = Container.BeginLifetimeScope();
  35. return scop.Resolve<T>( new TypedParameter(parameter.GetType(), parameter));
  36. }
  37. }
  38. }