framework /Signum.Engine/GlobalLazy.cs

Language C# Lines 89
MD5 Hash 47a9c71420198d9e71fe758279749db2
Repository https://github.com/mutharasank/framework.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using Signum.Engine.Maps;
using Signum.Entities;
using Signum.Utilities;
using Signum.Utilities.DataStructures;
using Signum.Utilities.ExpressionTrees;
using Signum.Utilities.Reflection;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using Signum.Engine;
using Signum.Entities.Reflection;

namespace Signum.Engine
{
    public struct InvalidateWith
    {
        static readonly Type[] Empty = new Type[0];

        readonly Type[] types;
        public Type[] Types 
        {
            get { return types ?? Empty; } 
        }

        public InvalidateWith(params Type[] types)
        {
            if(types != null)
                foreach (var type in types)
                {
                    if (type.IsAbstract)
                        throw new InvalidOperationException("Impossible to invalidate using {0} because is abstract".Formato(type));

                    if (!Reflector.IsIdentifiableEntity(type))
                        throw new InvalidOperationException("Impossible to invalidate using {0} because is not and IdentifiableEntity".Formato(type));
                }


            this.types = types;
        }
    }

    public static class GlobalLazy
    {
        static HashSet<IResetLazy> registeredLazyList = new HashSet<IResetLazy>();
        public static ResetLazy<T> WithoutInvalidations<T>(Func<T> func) where T : class
        {
            ResetLazy<T> result = new ResetLazy<T>(() =>
            {
                using (ExecutionMode.Global())
                using (HeavyProfiler.Log("ResetLazy", () => typeof(T).TypeName()))
                using (Transaction tr = Transaction.InTestTransaction ? null : Transaction.ForceNew())
                using (new EntityCache(EntityCacheType.ForceNewSealed))
                {
                    var value = func();

                    if (tr != null)
                        tr.Commit();

                    return value;
                }
            }, mode: LazyThreadSafetyMode.ExecutionAndPublication, 
            declaringType: func.Method.DeclaringType);

            registeredLazyList.Add(result);

            return result;
        }

        public static void ResetAll()
        {
            foreach (var lp in registeredLazyList)
                lp.Reset();
        }

        public static void LoadAll()
        {
            foreach (var lp in registeredLazyList)
                lp.Load();
        }
    }

  
}
Back to Top