SingletonDependency.cs 797 B

1234567891011121314151617181920212223242526
  1. using System;
  2. namespace Abp.Dependency
  3. {
  4. /// <summary>
  5. /// Used to get a singleton of any class which can be resolved using <see cref="IocManager.Instance"/>.
  6. /// Important: Use classes by injecting wherever possible. This class is for cases that's not possible.
  7. /// </summary>
  8. /// <typeparam name="T"></typeparam>
  9. public static class SingletonDependency<T>
  10. {
  11. /// <summary>
  12. /// Gets the instance.
  13. /// </summary>
  14. /// <value>
  15. /// The instance.
  16. /// </value>
  17. public static T Instance => LazyInstance.Value;
  18. private static readonly Lazy<T> LazyInstance;
  19. static SingletonDependency()
  20. {
  21. LazyInstance = new Lazy<T>(() => IocManager.Instance.Resolve<T>(), true);
  22. }
  23. }
  24. }