internal static AuthorizationRuleManager GetRulesForType(ApplicationContext applicationContext, Type type, string ruleSet)
        {
            if (ruleSet == ApplicationContext.DefaultRuleSet)
            {
                ruleSet = null;
            }

            var key = new RuleSetKey {
                Type = type, RuleSet = ruleSet
            };

#if NET5_0_OR_GREATER
            var rulesInfo = _perTypeRules.Value
                            .GetOrAdd(
                key,
                (t) => AssemblyLoadContextManager.CreateCacheInstance(type, new AuthorizationRuleManager(), OnAssemblyLoadContextUnload)
                );

            var result = rulesInfo.Item2;
#else
            var result = _perTypeRules.Value.GetOrAdd(key, (t) => { return(new AuthorizationRuleManager()); });
#endif

            InitializePerTypeRules(applicationContext, result, type);

            return(result);
        }
        private static void OnAssemblyLoadContextUnload(AssemblyLoadContext context)
        {
            var cache = PropertyInfoCache;

            lock (cache)
                AssemblyLoadContextManager.RemoveFromCache(cache, context);
        }
Exemple #3
0
        public static DataPortalMethodInfo GetMethodInfo(Type objectType, string methodName, params object[] parameters)
        {
            var key = new MethodCacheKey(objectType.FullName, methodName, MethodCaller.GetParameterTypes(parameters));

            DataPortalMethodInfo result = null;

            var found = false;

#if NET5_0_OR_GREATER
            try
            {
                found = _cache.TryGetValue(key, out var methodInfo);

                result = methodInfo?.Item2;
            }
            catch
            { /* failure will drop into !found block */ }

            if (!found)
            {
                lock (_cache)
                {
                    found = _cache.TryGetValue(key, out var methodInfo);

                    result = methodInfo?.Item2;

                    if (!found)
                    {
                        result = new DataPortalMethodInfo(MethodCaller.GetMethod(objectType, methodName, parameters));

                        var cacheInstance = AssemblyLoadContextManager.CreateCacheInstance(objectType, result, OnAssemblyLoadContextUnload);

                        _cache.Add(key, cacheInstance);
                    }
                }
            }
#else
            try
            {
                found = _cache.TryGetValue(key, out result);
            }
            catch
            { /* failure will drop into !found block */ }

            if (!found)
            {
                lock (_cache)
                {
                    if (!_cache.TryGetValue(key, out result))
                    {
                        result = new DataPortalMethodInfo(MethodCaller.GetMethod(objectType, methodName, parameters));

                        _cache.Add(key, result);
                    }
                }
            }
#endif

            return(result);
        }
        internal static PropertyInfoList GetPropertyListCache(Type objectType)
        {
            var cache = PropertyInfoCache;

#if NET5_0_OR_GREATER
            var found = cache.TryGetValue(objectType, out var listInfo);

            var list = listInfo?.Item2;

            if (!found)
            {
                lock (cache)
                {
                    found = cache.TryGetValue(objectType, out listInfo);

                    if (!found)
                    {
                        list = new PropertyInfoList();

                        var cacheInstance = AssemblyLoadContextManager.CreateCacheInstance(objectType, list, OnAssemblyLoadContextUnload);

                        cache.Add(objectType, cacheInstance);

                        FieldDataManager.ForceStaticFieldInit(objectType);
                    }
                }
            }
#else
            var found = cache.TryGetValue(objectType, out PropertyInfoList list);

            if (!found)
            {
                lock (cache)
                {
                    found = cache.TryGetValue(objectType, out list);

                    if (!found)
                    {
                        list = new PropertyInfoList();

                        cache.Add(objectType, list);

                        FieldDataManager.ForceStaticFieldInit(objectType);
                    }
                }
            }
#endif

            return(list);
        }
Exemple #5
0
        public static List <DynamicMemberHandle> GetCachedFieldHandlers(Type type)
        {
            List <DynamicMemberHandle> handlers;

#if NET5_0_OR_GREATER
            var found = _undoableFieldCache.TryGetValue(type, out var handlersInfo);

            handlers = handlersInfo?.Item2;

            if (!found)
            {
                lock (_undoableFieldCache)
                {
                    found = _undoableFieldCache.TryGetValue(type, out handlersInfo);

                    handlers = handlersInfo?.Item2;

                    if (!found)
                    {
                        var newHandlers = BuildHandlers(type);

                        var cacheInstance = AssemblyLoadContextManager.CreateCacheInstance(type, newHandlers, OnAssemblyLoadContextUnload);

                        _undoableFieldCache.Add(type, cacheInstance);

                        handlers = newHandlers;
                    }
                }
            }
#else
            if (!_undoableFieldCache.TryGetValue(type, out handlers))
            {
                var newHandlers = BuildHandlers(type);

                lock (_undoableFieldCache)  //ready to add, lock
                {
                    if (!_undoableFieldCache.TryGetValue(type, out handlers))
                    {
                        _undoableFieldCache.Add(type, newHandlers);

                        handlers = newHandlers;
                    }
                }
            }
#endif

            return(handlers);
        }
        internal static BusinessRuleManager GetRulesForType(Type type, string ruleSet)
        {
            if (ruleSet == ApplicationContext.DefaultRuleSet)
            {
                ruleSet = null;
            }

            var key = new RuleSetKey {
                Type = type, RuleSet = ruleSet
            };

#if NET5_0_OR_GREATER
            var rulesInfo = _perTypeRules.Value
                            .GetOrAdd(
                key,
                (t) => AssemblyLoadContextManager.CreateCacheInstance(type, new BusinessRuleManager(), OnAssemblyLoadContextUnload)
                );

            return(rulesInfo.Item2);
#else
            return(_perTypeRules.Value.GetOrAdd(key, (t) => { return new BusinessRuleManager(); }));
#endif
        }
 private static void OnAssemblyLoadContextUnload(AssemblyLoadContext context)
 {
     lock (_perTypeRules)
         AssemblyLoadContextManager.RemoveFromCache(_perTypeRules.Value, context, true);
 }
Exemple #8
0
 private static void OnAssemblyLoadContextUnload(AssemblyLoadContext context)
 {
     lock (_cache)
         AssemblyLoadContextManager.RemoveFromCache(_cache, context);
 }