public XmlDataTypeStoreDataScope GetDataScopeForType(Type type)
        {
            var currentDataScope = DataScopeManager.MapByType(type);
            var culture          = LocalizationScopeManager.MapByType(type);

            return(GetDataScope(currentDataScope, culture, type));
        }
Beispiel #2
0
        /// <exclude />
        internal static IQueryable <T> GetDataFromCache <T>(Func <IQueryable <T> > getQueryFunc)
            where T : class, IData
        {
            Verify.That(_isEnabled, "The cache is disabled.");

            DataScopeIdentifier dataScopeIdentifier = DataScopeManager.MapByType(typeof(T));
            CultureInfo         localizationScope   = LocalizationScopeManager.MapByType(typeof(T));

            var typeData = _cachedData.GetOrAdd(typeof(T), t => new TypeData());

            var dataScopeData = typeData.GetOrAdd(dataScopeIdentifier, scope => new ConcurrentDictionary <CultureInfo, CachedTable>());


            CachedTable cachedTable;

            if (!dataScopeData.TryGetValue(localizationScope, out cachedTable))
            {
                IQueryable <T> wholeTable = getQueryFunc();

                if (!DataProvidersSupportDataWrapping(typeof(T)))
                {
                    DisableCachingForType(typeof(T));

                    return(Verify.ResultNotNull(wholeTable));
                }

                if (_maximumSize != -1)
                {
                    List <IData> cuttedTable = TakeElements(wholeTable, _maximumSize + 1);
                    if (cuttedTable.Count > _maximumSize)
                    {
                        DisableCachingForType(typeof(T));

                        return(Verify.ResultNotNull(wholeTable));
                    }
                    cachedTable = new CachedTable(cuttedTable.Cast <T>().AsQueryable());
                }
                else
                {
                    cachedTable = new CachedTable(wholeTable.Evaluate().AsQueryable());
                }

                dataScopeData[localizationScope] = cachedTable;
            }

            var typedData = cachedTable.Queryable as IQueryable <T>;

            Verify.IsNotNull(typedData, "Cached value is invalid.");

            // Leaving a possibility to extract original query
            Func <IQueryable> originalQueryGetter = () =>
            {
                using (new DataScope(dataScopeIdentifier, localizationScope))
                {
                    return(getQueryFunc());
                }
            };

            return(new CachingQueryable <T>(cachedTable, originalQueryGetter));
        }
        private SqlDataTypeStoreTableKey GetTableKey()
        {
            string dataScope   = DataScopeManager.MapByType(InterfaceType).Name;
            string cultureInfo = LocalizationScopeManager.MapByType(InterfaceType).Name;

            return(new SqlDataTypeStoreTableKey(dataScope, cultureInfo));
        }
Beispiel #4
0
        /// <summary>
        /// Flush cached data for a data type in the specified data scope.
        /// </summary>
        /// <param name="interfaceType">The type of data to flush from the cache</param>
        /// <param name="dataScopeIdentifier">The data scope to flush</param>
        public static void ClearCache(Type interfaceType, DataScopeIdentifier dataScopeIdentifier)
        {
            using (_resourceLocker.Locker)
            {
                var cachedData = _resourceLocker.Resources.CachedData;

                if (!cachedData.ContainsKey(interfaceType))
                {
                    return;
                }

                if (dataScopeIdentifier == null)
                {
                    dataScopeIdentifier = DataScopeManager.MapByType(interfaceType);
                }

                if (cachedData[interfaceType].ContainsKey(dataScopeIdentifier))
                {
                    cachedData[interfaceType].Remove(dataScopeIdentifier);

                    if (cachedData[interfaceType].Count == 0)
                    {
                        cachedData.Remove(interfaceType);
                    }
                }
            }
        }
        public IQueryable <T> GetData <T>()
            where T : class, IData
        {
            CheckTransactionNotInAbortedState();

            XmlDataTypeStore dataTypeStore = _xmlDataTypeStoresContainer.GetDataTypeStore(typeof(T));

            var currentDataScope = DataScopeManager.MapByType(typeof(T));

            if (!dataTypeStore.HasDataScopeName(currentDataScope))
            {
                return(Enumerable.Empty <T>().AsQueryable());
            }

            var dataTypeStoreScope = dataTypeStore.GetDataScopeForType(typeof(T));

            var fileRecord = XmlDataProviderDocumentCache.GetFileRecord(
                dataTypeStoreScope.Filename,
                dataTypeStoreScope.ElementName,
                dataTypeStore.Helper.CreateDataId);

            if (fileRecord.CachedTable == null)
            {
                ICollection <XElement> elements = fileRecord.ReadOnlyElementsList;

                Func <XElement, T> fun = dataTypeStore.Helper.CreateSelectFunction <T>(_dataProviderContext.ProviderName);

                var list = new List <T>(elements.Count);
                list.AddRange(elements.Select(fun));

                fileRecord.CachedTable = new CachedTable <T>(list);
            }

            return(new CachingQueryable <T>(fileRecord.CachedTable, () => fileRecord.CachedTable.Queryable));
        }
        /// <exclude />
        public DataSourceId CreateDataSourceId(IDataId dataId, Type interfaceType)
        {
            Verify.ArgumentNotNull(dataId, "dataId");
            Verify.ArgumentNotNull(interfaceType, "interfaceType");
            Verify.ArgumentCondition(typeof(IData).IsAssignableFrom(interfaceType), "interfaceType", "The interface type '{0}' does not inherit the interface '{1}'".FormatWith(interfaceType, typeof(IData)));

            return(new DataSourceId(dataId, _providerName, interfaceType, DataScopeManager.MapByType(interfaceType), LocalizationScopeManager.MapByType(interfaceType)));
        }
        /// <exclude />
        public Func <XElement, T> CreateSelectFunction <T>(string providerName) where T : IData
        {
            Type type = typeof(T);

            var cache = _selectFunctionCache[typeof(T)];

            if (cache == null)
            {
                lock (_selectFunctionCache)
                {
                    cache = _selectFunctionCache[type];

                    if (cache == null)
                    {
                        cache = new Hashtable <string, Delegate>();
                        _selectFunctionCache.Add(type, cache);
                    }
                }
            }

            CultureInfo         cultureInfo         = LocalizationScopeManager.MapByType(type);
            DataScopeIdentifier dataScopeIdentifier = DataScopeManager.MapByType(type);

            string cacheKey = cultureInfo + "|" + dataScopeIdentifier.Name;

            Delegate result = cache[cacheKey];

            if (result == null)
            {
                lock (this)
                {
                    result = cache[cacheKey];
                    if (result == null)
                    {
                        // element => new [WrapperClass](element, new DataSourceId(new [DataIdClass](element), providerName, type, dataScope, localizationScope)), element
                        ParameterExpression parameterExpression = Expression.Parameter(typeof(XElement), "element");
                        result = Expression.Lambda <Func <XElement, T> >(
                            Expression.New(_wrapperClassConstructor,
                                           new Expression[]
                        {
                            parameterExpression,
                            Expression.New(GeneretedClassesMethodCache.DataSourceIdConstructor2,
                                           new Expression[]
                            {
                                Expression.New(_idClassConstructor, new Expression[] { parameterExpression }),
                                Expression.Constant(providerName, typeof(string)),
                                Expression.Constant(type, typeof(Type)),
                                Expression.Constant(dataScopeIdentifier, typeof(DataScopeIdentifier)),
                                Expression.Constant(cultureInfo, typeof(CultureInfo))
                            })
                        }), new[] { parameterExpression }).Compile();
                        cache.Add(cacheKey, result);
                    }
                }
            }
            return((Func <XElement, T>)result);
        }
        private string GetCacheKey(TPropertyType key)
        {
            string dataScope = DataScopeManager.MapByType(typeof(TDataType)).ToString();
            string result    = key + dataScope;

            if (_typeIsLocalizable)
            {
                result += LocalizationScopeManager.MapByType(typeof(TDataType)).ToString();
            }
            return(result);
        }
        internal static void ClearCache(Type interfaceType, DataScopeIdentifier dataScopeIdentifier, CultureInfo localizationScope)
        {
            if (!_cachedData.TryGetValue(interfaceType, out TypeData typeData))
            {
                return;
            }

            dataScopeIdentifier = dataScopeIdentifier ?? DataScopeManager.MapByType(interfaceType);
            localizationScope   = !DataLocalizationFacade.IsLocalized(interfaceType)
                ? CultureInfo.InvariantCulture
                : (localizationScope ?? LocalizationScopeManager.CurrentLocalizationScope);

            var key = new Tuple <DataScopeIdentifier, CultureInfo>(dataScopeIdentifier, localizationScope);

            typeData.TryRemove(key, out _);
        }
Beispiel #10
0
        /// <summary>
        /// Flush cached data for a data type in the specified data scope.
        /// </summary>
        /// <param name="interfaceType">The type of data to flush from the cache</param>
        /// <param name="dataScopeIdentifier">The data scope to flush</param>
        public static void ClearCache(Type interfaceType, DataScopeIdentifier dataScopeIdentifier)
        {
            TypeData typeData;

            if (!_cachedData.TryGetValue(interfaceType, out typeData))
            {
                return;
            }

            if (dataScopeIdentifier == null)
            {
                dataScopeIdentifier = DataScopeManager.MapByType(interfaceType);
            }

            ConcurrentDictionary <CultureInfo, CachedTable> data;

            typeData.TryRemove(dataScopeIdentifier, out data);
        }
Beispiel #11
0
        internal static void ClearCache(Type interfaceType, DataScopeIdentifier dataScopeIdentifier, CultureInfo localizationScope)
        {
            TypeData typeData;

            if (!_cachedData.TryGetValue(interfaceType, out typeData))
            {
                return;
            }

            dataScopeIdentifier = dataScopeIdentifier ?? DataScopeManager.MapByType(interfaceType);
            localizationScope   = localizationScope ?? LocalizationScopeManager.MapByType(interfaceType);

            var key = new Tuple <DataScopeIdentifier, CultureInfo>(dataScopeIdentifier, localizationScope);

            CachedTable value;

            typeData.TryRemove(key, out value);
        }
        /// <summary>
        /// Flush cached data for a data type in the specified data scope.
        /// </summary>
        /// <param name="interfaceType">The type of data to flush from the cache</param>
        /// <param name="dataScopeIdentifier">The data scope to flush</param>
        public static void ClearCache(Type interfaceType, DataScopeIdentifier dataScopeIdentifier)
        {
            TypeData typeData;

            if (!_cachedData.TryGetValue(interfaceType, out typeData))
            {
                return;
            }

            dataScopeIdentifier = dataScopeIdentifier ?? DataScopeManager.MapByType(interfaceType);

            var toRemove = typeData.Keys.Where(key => key.Item1 == dataScopeIdentifier).ToList();

            foreach (var key in toRemove)
            {
                CachedTable value;
                typeData.TryRemove(key, out value);
            }
        }
Beispiel #13
0
            public void WrapperAction(TSource source)
            {
                var originalHttpContext = HttpContext.Current;

                bool dataScopePushed     = false;
                bool languageScopePushed = false;

                var currentThread = System.Threading.Thread.CurrentThread;

                CultureInfo originalCulture   = currentThread.CurrentCulture;
                CultureInfo originalUiCulture = currentThread.CurrentUICulture;

                using (ThreadDataManager.Initialize(_parentData))
                {
                    try
                    {
                        DataScopeManager.EnterThreadLocal();

                        if (DataScopeManager.CurrentDataScope != _parentThreadDataScope)
                        {
                            DataScopeManager.PushDataScope(_parentThreadDataScope);
                            dataScopePushed = true;
                        }

                        LocalizationScopeManager.EnterThreadLocal();

                        if (LocalizationScopeManager.CurrentLocalizationScope != _parentThreadLocale)
                        {
                            LocalizationScopeManager.PushLocalizationScope(_parentThreadLocale);
                            languageScopePushed = true;
                        }

                        HttpContext.Current = _parentThreadHttpContext;

                        currentThread.CurrentCulture   = _parentThreadCulture;
                        currentThread.CurrentUICulture = _parentThreadUiCulture;

                        try
                        {
                            _body(source);
                        }
                        catch (ThreadAbortException threadAbort)
                        {
                            object state = threadAbort.ExceptionState;

                            if (state != null)
                            {
                                Thread.ResetAbort();

                                // Throwing another exception because Thread.ResetAbort clears ThreadAbortException.ExceptionState
                                throw new RethrowableThreadAbortException(state);
                            }
                        }
                    }
                    finally
                    {
                        currentThread.CurrentCulture   = originalCulture;
                        currentThread.CurrentUICulture = originalUiCulture;

                        HttpContext.Current = originalHttpContext;

                        if (dataScopePushed)
                        {
                            DataScopeManager.PopDataScope();
                        }
                        if (languageScopePushed)
                        {
                            LocalizationScopeManager.PopLocalizationScope();
                        }

                        DataScopeManager.ExitThreadLocal();
                        LocalizationScopeManager.ExitThreadLocal();
                    }
                }
            }
Beispiel #14
0
        /// <exclude />
        internal static IQueryable <T> GetDataFromCache <T>(Func <IQueryable <T> > getQueryFunc)
            where T : class, IData
        {
            Verify.That(_isEnabled, "The cache is disabled.");

            DataScopeIdentifier dataScopeIdentifier = DataScopeManager.MapByType(typeof(T));
            CultureInfo         localizationScope   = LocalizationScopeManager.MapByType(typeof(T));

            var cachedDataset = _resourceLocker.Resources.CachedData;

            Hashtable <DataScopeIdentifier, Hashtable <CultureInfo, CachedTable> > dataScopeData;

            if (!cachedDataset.TryGetValue(typeof(T), out dataScopeData))
            {
                using (_resourceLocker.Locker)
                {
                    if (!cachedDataset.TryGetValue(typeof(T), out dataScopeData))
                    {
                        dataScopeData = new Hashtable <DataScopeIdentifier, Hashtable <CultureInfo, CachedTable> >();

                        cachedDataset.Add(typeof(T), dataScopeData);
                    }
                }
            }

            Hashtable <CultureInfo, CachedTable> localizationScopeData;

            if (!dataScopeData.TryGetValue(dataScopeIdentifier, out localizationScopeData))
            {
                using (_resourceLocker.Locker)
                {
                    if (!dataScopeData.TryGetValue(dataScopeIdentifier, out localizationScopeData))
                    {
                        localizationScopeData = new Hashtable <CultureInfo, CachedTable>();
                        dataScopeData.Add(dataScopeIdentifier, localizationScopeData);
                    }
                }
            }

            CachedTable cachedTable;

            if (!localizationScopeData.TryGetValue(localizationScope, out cachedTable))
            {
                IQueryable <T> wholeTable = getQueryFunc();

                if (!DataProvidersSupportDataWrapping(typeof(T)))
                {
                    DisableCachingForType(typeof(T));

                    return(Verify.ResultNotNull(wholeTable));
                }

                if (_maximumSize != -1)
                {
                    List <IData> cuttedTable = TakeElements(wholeTable, _maximumSize + 1);
                    if (cuttedTable.Count > _maximumSize)
                    {
                        DisableCachingForType(typeof(T));

                        return(Verify.ResultNotNull(wholeTable));
                    }
                    cachedTable = new CachedTable(cuttedTable.Cast <T>().AsQueryable());
                }
                else
                {
                    cachedTable = new CachedTable(wholeTable.Evaluate().AsQueryable());
                }

                using (_resourceLocker.Locker)
                {
                    if (!localizationScopeData.ContainsKey(localizationScope))
                    {
                        localizationScopeData.Add(localizationScope, cachedTable);
                    }
                }
            }

            var typedData = cachedTable.Queryable as IQueryable <T>;

            Verify.IsNotNull(typedData, "Cached value is invalid.");

            // Leaving a possibility to extract original query
            Func <IQueryable> originalQueryGetter = () =>
            {
                using (new DataScope(dataScopeIdentifier, localizationScope))
                {
                    return(getQueryFunc());
                }
            };

            return(new CachingQueryable <T>(cachedTable, originalQueryGetter));
        }
 private static string GetKey(Type type)
 {
     return(GetKey(type, DataScopeManager.MapByType(type), LocalizationScopeManager.MapByType(type)));
 }
        /// <exclude />
        public void UpdateDataSourceIdDataScope(IData data)
        {
            Verify.ArgumentNotNull(data, "data");

            data.DataSourceId.DataScopeIdentifier = DataScopeManager.MapByType(data);
        }