IocContainer.cs 1.2 KB

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