PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Signum.Engine/GlobalLazy.cs

https://github.com/mutharasank/framework
C# | 88 lines | 72 code | 16 blank | 0 comment | 6 complexity | 47a9c71420198d9e71fe758279749db2 MD5 | raw file
Possible License(s): GPL-3.0
  1. using Signum.Engine.Maps;
  2. using Signum.Entities;
  3. using Signum.Utilities;
  4. using Signum.Utilities.DataStructures;
  5. using Signum.Utilities.ExpressionTrees;
  6. using Signum.Utilities.Reflection;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Data.SqlClient;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using Signum.Engine;
  15. using Signum.Entities.Reflection;
  16. namespace Signum.Engine
  17. {
  18. public struct InvalidateWith
  19. {
  20. static readonly Type[] Empty = new Type[0];
  21. readonly Type[] types;
  22. public Type[] Types
  23. {
  24. get { return types ?? Empty; }
  25. }
  26. public InvalidateWith(params Type[] types)
  27. {
  28. if(types != null)
  29. foreach (var type in types)
  30. {
  31. if (type.IsAbstract)
  32. throw new InvalidOperationException("Impossible to invalidate using {0} because is abstract".Formato(type));
  33. if (!Reflector.IsIdentifiableEntity(type))
  34. throw new InvalidOperationException("Impossible to invalidate using {0} because is not and IdentifiableEntity".Formato(type));
  35. }
  36. this.types = types;
  37. }
  38. }
  39. public static class GlobalLazy
  40. {
  41. static HashSet<IResetLazy> registeredLazyList = new HashSet<IResetLazy>();
  42. public static ResetLazy<T> WithoutInvalidations<T>(Func<T> func) where T : class
  43. {
  44. ResetLazy<T> result = new ResetLazy<T>(() =>
  45. {
  46. using (ExecutionMode.Global())
  47. using (HeavyProfiler.Log("ResetLazy", () => typeof(T).TypeName()))
  48. using (Transaction tr = Transaction.InTestTransaction ? null : Transaction.ForceNew())
  49. using (new EntityCache(EntityCacheType.ForceNewSealed))
  50. {
  51. var value = func();
  52. if (tr != null)
  53. tr.Commit();
  54. return value;
  55. }
  56. }, mode: LazyThreadSafetyMode.ExecutionAndPublication,
  57. declaringType: func.Method.DeclaringType);
  58. registeredLazyList.Add(result);
  59. return result;
  60. }
  61. public static void ResetAll()
  62. {
  63. foreach (var lp in registeredLazyList)
  64. lp.Reset();
  65. }
  66. public static void LoadAll()
  67. {
  68. foreach (var lp in registeredLazyList)
  69. lp.Load();
  70. }
  71. }
  72. }