示例#1
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));
        }
        public XmlDataTypeStoreDataScope GetDataScopeForType(Type type)
        {
            var currentDataScope = DataScopeManager.MapByType(type);
            var culture          = LocalizationScopeManager.MapByType(type);

            return(GetDataScope(currentDataScope, culture, type));
        }
        private static XElement AddData(DataType dataType, CultureInfo cultureInfo)
        {
            XElement datasElement = new XElement("Datas");

            if (cultureInfo != null)
            {
                datasElement.Add(new XAttribute("locale", cultureInfo.Name));
            }

            foreach (XElement addElement in dataType.Dataset)
            {
                IData data = DataFacade.BuildNew(dataType.InterfaceType);

                if (!dataType.InterfaceType.IsInstanceOfType(data))
                {
                    dataType.InterfaceType = GetInstalledVersionOfPendingType(dataType.InterfaceType, data);
                }

                var properties = GetDataTypeProperties(dataType.InterfaceType);

                foreach (XAttribute attribute in addElement.Attributes())
                {
                    string fieldName = attribute.Name.LocalName;
                    if (IsObsoleteField(dataType, fieldName))
                    {
                        continue;
                    }

                    PropertyInfo propertyInfo = properties[fieldName];

                    propertyInfo.SetValue(data, ValueTypeConverter.Convert(attribute.Value, propertyInfo.PropertyType), null);
                }

                ILocalizedControlled localizedControlled = data as ILocalizedControlled;
                if (localizedControlled != null)
                {
                    localizedControlled.SourceCultureName = LocalizationScopeManager.MapByType(dataType.InterfaceType).Name;
                }

                DataFacade.AddNew(data, false, true, false); // Ignore validation, this should have been done in the validation face

                XElement keysElement = new XElement("Keys");

                foreach (PropertyInfo propertyInfo in data.GetKeyProperties())
                {
                    string keyName  = propertyInfo.Name;
                    object keyValue = propertyInfo.GetValue(data, null);

                    XElement keyElement = new XElement("Key",
                                                       new XAttribute("name", keyName),
                                                       new XAttribute("value", keyValue));

                    keysElement.Add(keyElement);
                }

                datasElement.Add(keysElement);
            }

            return(datasElement);
        }
        private SqlDataTypeStoreTableKey GetTableKey()
        {
            string dataScope   = DataScopeManager.MapByType(InterfaceType).Name;
            string cultureInfo = LocalizationScopeManager.MapByType(InterfaceType).Name;

            return(new SqlDataTypeStoreTableKey(dataScope, cultureInfo));
        }
        /// <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);
        }
示例#7
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);
        }
示例#8
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();
                    }
                }
            }
示例#9
0
        private XElement AddData(DataType dataType, CultureInfo cultureInfo)
        {
            XElement datasElement = new XElement("Datas");

            if (cultureInfo != null)
            {
                datasElement.Add(new XAttribute("locale", cultureInfo.Name));
            }

            foreach (XElement addElement in dataType.Dataset)
            {
                var interfaceType = dataType.InterfaceType;

                IData data = DataFacade.BuildNew(interfaceType);

                if (!dataType.InterfaceType.IsInstanceOfType(data))
                {
                    dataType.InterfaceType = GetInstalledVersionOfPendingType(interfaceType, data);
                }


                var dataKey = CopyFieldValues(dataType, data, addElement);

                if (dataType.AllowOverwrite || dataType.OnlyUpdate)
                {
                    IData existingData = DataFacade.TryGetDataByUniqueKey(interfaceType, dataKey);

                    if (existingData != null)
                    {
                        CopyFieldValues(dataType, existingData, addElement);
                        DataFacade.Update(existingData, false, true, false);

                        continue;
                    }

                    if (dataType.OnlyUpdate)
                    {
                        continue;
                    }
                }

                ILocalizedControlled localizedControlled = data as ILocalizedControlled;
                if (localizedControlled != null)
                {
                    localizedControlled.SourceCultureName = LocalizationScopeManager.MapByType(interfaceType).Name;
                }

                if (data is IVersioned)
                {
                    UpdateVersionId((IVersioned)data);
                }

                DataFacade.AddNew(data, false, true, false); // Ignore validation, this should have been done in the validation face

                XElement keysElement = new XElement("Keys");

                foreach (PropertyInfo propertyInfo in data.GetKeyProperties())
                {
                    string keyName  = propertyInfo.Name;
                    object keyValue = propertyInfo.GetValue(data, null);

                    XElement keyElement = new XElement("Key",
                                                       new XAttribute("name", keyName),
                                                       new XAttribute("value", keyValue));

                    keysElement.Add(keyElement);
                }

                datasElement.Add(keysElement);
            }

            return(datasElement);
        }
示例#10
0
        private XElement AddData(DataType dataType, CultureInfo cultureInfo)
        {
            XElement datasElement = new XElement("Datas");

            if (cultureInfo != null)
            {
                datasElement.Add(new XAttribute("locale", cultureInfo.Name));
            }

            foreach (XElement addElement in dataType.Dataset)
            {
                var interfaceType = dataType.InterfaceType;

                IData data = DataFacade.BuildNew(interfaceType);

                if (!dataType.InterfaceType.IsInstanceOfType(data))
                {
                    dataType.InterfaceType = GetInstalledVersionOfPendingType(interfaceType, data);
                }


                var dataKey = PopulateAndReturnKeyPropertyValues(dataType, data, addElement);

                if (dataType.AllowOverwrite || dataType.OnlyUpdate)
                {
                    List <IData> existingDataList = DataFacade.TryGetDataByLookupKeys(interfaceType, dataKey).ToList();
                    if (existingDataList.Count > 1)
                    {
                        throw new InvalidOperationException("Got more than 1 existing data element when querying on key properties for " + addElement.ToString());
                    }

                    IData existingData = existingDataList.FirstOrDefault();

                    if (existingData != null)
                    {
                        PopulateAndReturnKeyPropertyValues(dataType, existingData, addElement);
                        DataFacade.Update(existingData, false, true, false);

                        continue;
                    }

                    if (dataType.OnlyUpdate)
                    {
                        continue;
                    }
                }

                if (data is ILocalizedControlled localizedControlled)
                {
                    localizedControlled.SourceCultureName = LocalizationScopeManager.MapByType(interfaceType).Name;
                }

                if (data is IVersioned versionedData)
                {
                    UpdateVersionId(versionedData);
                }

                DataFacade.AddNew(data, false, true, false); // Ignore validation, this should have been done in the validation face

                XElement keysElement = new XElement("Keys");

                foreach (PropertyInfo propertyInfo in data.GetKeyProperties())
                {
                    string keyName  = propertyInfo.Name;
                    object keyValue = propertyInfo.GetValue(data, null);

                    XElement keyElement = new XElement("Key",
                                                       new XAttribute("name", keyName),
                                                       new XAttribute("value", keyValue));

                    keysElement.Add(keyElement);
                }

                datasElement.Add(keysElement);
            }

            return(datasElement);
        }
示例#11
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 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)));
        }