Ejemplo n.º 1
0
        public T GetDataFromDataSourceId <T>(DataSourceId dataSourceId, bool useCaching)
            where T : class, IData
        {
            if (null == dataSourceId)
            {
                throw new ArgumentNullException("dataSourceId");
            }

            useCaching = useCaching && DataCachingFacade.IsTypeCacheable(typeof(T));

            using (new DataScope(dataSourceId.DataScopeIdentifier, dataSourceId.LocaleScope))
            {
                T resultData = null;

                string cacheKey = string.Empty;
                if (useCaching)
                {
                    cacheKey   = dataSourceId.ToString();
                    resultData = (T)_dataBySourceIdCache.Get(cacheKey);
                }

                if (resultData == null)
                {
                    resultData = DataProviderPluginFacade.GetData <T>(dataSourceId.ProviderName, dataSourceId.DataId);

                    if (useCaching && resultData != null && _dataBySourceIdCache.Enabled)
                    {
                        _dataBySourceIdCache.Add(cacheKey, resultData);
                    }
                }

                if (useCaching && resultData != null)
                {
                    resultData = DataWrappingFacade.Wrap(resultData);
                }

                DataInterceptor dataInterceptor;
                this.DataInterceptors.TryGetValue(typeof(T), out dataInterceptor);
                if (dataInterceptor != null)
                {
                    try
                    {
                        resultData = dataInterceptor.InterceptGetDataFromDataSourceId <T>(resultData);
                    }
                    catch (Exception ex)
                    {
                        Log.LogError(LogTitle, "Calling data interceptor failed with the following exception");
                        Log.LogError(LogTitle, ex);
                    }
                }

                return(resultData);
            }
        }
Ejemplo n.º 2
0
        public T GetDataFromDataSourceId <T>(DataSourceId dataSourceId, bool useCaching)
            where T : class, IData
        {
            Verify.ArgumentNotNull(dataSourceId, nameof(dataSourceId));

            useCaching = useCaching && DataCachingFacade.IsTypeCacheable(typeof(T));

            using (new DataScope(dataSourceId.DataScopeIdentifier, dataSourceId.LocaleScope))
            {
                T resultData = null;

                string cacheKey = string.Empty;
                if (useCaching)
                {
                    cacheKey   = dataSourceId.ToString();
                    resultData = (T)_dataBySourceIdCache.Get(cacheKey);
                }

                if (resultData == null)
                {
                    resultData = DataProviderPluginFacade.GetData <T>(dataSourceId.ProviderName, dataSourceId.DataId);

                    if (useCaching && resultData != null && _dataBySourceIdCache.Enabled)
                    {
                        _dataBySourceIdCache.Add(cacheKey, resultData);
                    }
                }

                if (useCaching && resultData != null)
                {
                    resultData = DataWrappingFacade.Wrap(resultData);
                }

                foreach (var dataInterceptor in GetDataInterceptors(typeof(T)))
                {
                    try
                    {
                        resultData = dataInterceptor.InterceptGetDataFromDataSourceId <T>(resultData);
                    }
                    catch (Exception ex)
                    {
                        Log.LogError(LogTitle, ex);
                    }
                }

                return(resultData);
            }
        }
Ejemplo n.º 3
0
        public IQueryable <T> GetData <T>(bool useCaching, IEnumerable <string> providerNames)
            where T : class, IData
        {
            IQueryable <T> resultQueryable;

            if (DataProviderRegistry.AllInterfaces.Contains(typeof(T)))
            {
                if (useCaching &&
                    providerNames == null &&
                    DataCachingFacade.IsDataAccessCacheEnabled(typeof(T)))
                {
                    resultQueryable = DataCachingFacade.GetDataFromCache <T>(
                        () => BuildQueryFromProviders <T>(null));
                }
                else
                {
                    resultQueryable = BuildQueryFromProviders <T>(providerNames);
                }
            }
            else
            {
                DataProviderRegistry.CheckInitializationErrors(typeof(T));

                if (!typeof(T).GetCustomInterfaceAttributes <AutoUpdatebleAttribute>().Any())
                {
                    throw new ArgumentException($"The given interface type ({typeof (T)}) is not supported by any data providers");
                }

                resultQueryable = new List <T>().AsQueryable();
            }

            foreach (var dataInterceptor in GetDataInterceptors(typeof(T)))
            {
                try
                {
                    resultQueryable = dataInterceptor.InterceptGetData <T>(resultQueryable);
                }
                catch (Exception ex)
                {
                    Log.LogError(LogTitle, ex);
                }
            }

            return(resultQueryable);
        }
Ejemplo n.º 4
0
        private void Delete <T>(IEnumerable <T> dataset, bool suppressEventing, CascadeDeleteType cascadeDeleteType, bool referencesFromAllScopes, HashSet <DataSourceId> dataPendingDeletion)
            where T : class, IData
        {
            Verify.ArgumentNotNull(dataset, nameof(dataset));

            dataset = dataset.Evaluate();

            foreach (var data in dataset)
            {
                var dataSourceId = data.DataSourceId;
                if (!dataPendingDeletion.Contains(dataSourceId))
                {
                    dataPendingDeletion.Add(dataSourceId);
                }
            }

            if (cascadeDeleteType != CascadeDeleteType.Disable)
            {
                foreach (IData data in dataset)
                {
                    Verify.ArgumentCondition(data != null, nameof(dataset), "dataset may not contain nulls");

                    Type interfaceType = data.DataSourceId.InterfaceType;

                    // Not deleting references if the data is versioned and not all of the
                    // versions of the element are to be deleted
                    if (data is IVersioned && interfaceType.GetKeyProperties().Count == 1)
                    {
                        var key      = data.GetUniqueKey();
                        var versions = DataFacade.TryGetDataVersionsByUniqueKey(interfaceType, key).ToList();

                        if (versions.Count > 1 &&
                            (dataset.Count() < versions.Count ||
                             !versions.All(v => dataPendingDeletion.Contains(v.DataSourceId))))
                        {
                            continue;
                        }
                    }

                    using (new DataScope(data.DataSourceId.DataScopeIdentifier))
                    {
                        var allReferences = DataReferenceFacade.GetRefereesInt(data, referencesFromAllScopes, (a, b) => true);

                        if (allReferences.Count == 0)
                        {
                            continue;
                        }

                        Verify.IsTrue(cascadeDeleteType != CascadeDeleteType.Disallow, "One of the given datas is referenced by one or more datas");

                        var optionalReferences    = allReferences.Where(kvp => kvp.Item2.IsOptionalReference);
                        var notOptionalReferences = allReferences.Where(kvp => !kvp.Item2.IsOptionalReference &&
                                                                        !dataPendingDeletion.Contains(kvp.Item1.DataSourceId)).Evaluate();

                        foreach (var reference in optionalReferences)
                        {
                            var referee = reference.Item1;
                            reference.Item2.SourcePropertyInfo.SetValue(referee, null, null);
                            DataFacade.Update(referee, false, true, false);
                        }

                        foreach (var refereeInfo in notOptionalReferences)
                        {
                            if (!refereeInfo.Item2.AllowCascadeDeletes)
                            {
                                throw new InvalidOperationException("One of the given data items is referenced by one or more data items that do not allow cascade delete.");
                            }
                        }

                        var toDelete = notOptionalReferences.Select(_ => _.Item1);
                        Delete <IData>(toDelete, suppressEventing, cascadeDeleteType, referencesFromAllScopes);
                    }
                }
            }


            Dictionary <string, Dictionary <Type, List <IData> > > sortedDatas = dataset.ToDataProviderAndInterfaceTypeSortedDictionary();

            foreach (KeyValuePair <string, Dictionary <Type, List <IData> > > providerPair in sortedDatas)
            {
                foreach (KeyValuePair <Type, List <IData> > interfaceTypePair in providerPair.Value)
                {
                    DataProviderPluginFacade.Delete(providerPair.Key, interfaceTypePair.Value.Select(d => d.DataSourceId));

                    if (DataCachingFacade.IsTypeCacheable(interfaceTypePair.Key))
                    {
                        DataCachingFacade.RemoveFromCache(interfaceTypePair.Value);
                    }
                }
            }


            if (!suppressEventing)
            {
                foreach (IData element in dataset)
                {
                    DataEventSystemFacade.FireDataDeletedEvent(element.DataSourceId.InterfaceType, element);
                }
            }
        }
Ejemplo n.º 5
0
        private static List <T> AddNew_AddingMethod <T>(string providerName, IEnumerable <T> dataset, bool suppressEventing, bool performForeignKeyIntegrityCheck, bool performValidation)
            where T : class, IData
        {
            Verify.ArgumentNotNullOrEmpty(providerName, "providerName");
            Verify.ArgumentNotNull(dataset, "dataset");
            Verify.ArgumentCondition(!dataset.Contains(null), "dataset", "The enumeration may not contain null values");


            List <string> writeableProviders = DataProviderRegistry.GetWriteableDataProviderNamesByInterfaceType(typeof(T));

            if (!writeableProviders.Contains(providerName))
            {
                Log.LogVerbose(LogTitle, $"Type data interface '{typeof(T)}' is marked auto updateable and is not supported by the provider '{providerName}', adding it");

                DynamicTypeManager.EnsureCreateStore(typeof(T), providerName);
            }

            writeableProviders = DataProviderRegistry.GetWriteableDataProviderNamesByInterfaceType(typeof(T));
            if (!writeableProviders.Contains(providerName))
            {
                throw new InvalidOperationException($"The writeable data providers '{providerName}' does not support the interface '{typeof(T)}'.");
            }


            foreach (T data in dataset)
            {
                if (performValidation)
                {
                    CheckValidationResult(ValidationFacade.Validate <T>(data), typeof(T));
                }

                if (performForeignKeyIntegrityCheck)
                {
                    data.ValidateForeignKeyIntegrity();
                }
            }


            if (!suppressEventing)
            {
                foreach (T data in dataset)
                {
                    DataEventSystemFacade.FireDataBeforeAddEvent <T>(data);
                }
            }

            List <T> addedDataset = DataProviderPluginFacade.AddNew <T>(providerName, dataset);

            DataCachingFacade.ClearCache(typeof(T), DataScopeManager.MapByType(typeof(T)), LocalizationScopeManager.MapByType(typeof(T)));

            if (!suppressEventing)
            {
                foreach (T data in addedDataset)
                {
                    DataEventSystemFacade.FireDataAfterAddEvent <T>(data);
                }
            }


            return(addedDataset);
        }
Ejemplo n.º 6
0
        public void Update(IEnumerable <IData> dataset, bool suppressEventing, bool performForeignKeyIntegrityCheck, bool performValidation)
        {
            Verify.ArgumentNotNull(dataset, "dataset");

            var sortedDataset = dataset.ToDataProviderAndInterfaceTypeSortedDictionary();

            if (!suppressEventing)
            {
                foreach (IData data in dataset)
                {
                    DataEventSystemFacade.FireDataBeforeUpdateEvent(data.DataSourceId.InterfaceType, data);
                }
            }


            foreach (IData data in dataset)
            {
                if (performValidation)
                {
                    CheckValidationResult(ValidationFacade.Validate(data), data.DataSourceId.InterfaceType);
                }

                if (performForeignKeyIntegrityCheck)
                {
                    data.ValidateForeignKeyIntegrity();
                }
            }


            foreach (KeyValuePair <string, Dictionary <Type, List <IData> > > providerPair in sortedDataset)
            {
                foreach (KeyValuePair <Type, List <IData> > interfaceTypePair in providerPair.Value)
                {
                    List <IData> dataToUpdate = interfaceTypePair.Value;

                    if (DataCachingFacade.IsTypeCacheable(interfaceTypePair.Key))
                    {
                        List <IData> newDataToUpdate = new List <IData>();

                        foreach (IData d in interfaceTypePair.Value)
                        {
                            newDataToUpdate.Add(DataWrappingFacade.UnWrap(d));
                        }

                        dataToUpdate = newDataToUpdate;
                    }

                    DataProviderPluginFacade.Update(providerPair.Key, dataToUpdate);

                    if (DataCachingFacade.IsTypeCacheable(interfaceTypePair.Key))
                    {
                        DataCachingFacade.ClearCache(interfaceTypePair.Key);
                    }
                }
            }


            if (!suppressEventing)
            {
                foreach (IData data in dataset)
                {
                    DataEventSystemFacade.FireDataAfterUpdateEvent(data.DataSourceId.InterfaceType, data);
                }
            }
        }
Ejemplo n.º 7
0
        private void Delete <T>(IEnumerable <T> dataset, bool suppressEventing, CascadeDeleteType cascadeDeleteType, bool referencesFromAllScopes, HashSet <DataSourceId> dataPendingDeletion)
            where T : class, IData
        {
            Verify.ArgumentNotNull(dataset, nameof(dataset));

            dataset = dataset.Evaluate();

            foreach (var data in dataset)
            {
                var dataSourceId = data.DataSourceId;
                if (!dataPendingDeletion.Contains(dataSourceId))
                {
                    dataPendingDeletion.Add(dataSourceId);
                }
            }

            if (cascadeDeleteType != CascadeDeleteType.Disable)
            {
                foreach (IData element in dataset)
                {
                    Verify.ArgumentCondition(element != null, nameof(dataset), "dataset may not contain nulls");

                    if (!element.IsDataReferred())
                    {
                        continue;
                    }

                    Type interfaceType = element.DataSourceId.InterfaceType;

                    // Not deleting references if the data is versioned and not all of the
                    // versions of the element are to be deleted
                    if (element is IVersioned)
                    {
                        var key      = element.GetUniqueKey();
                        var versions = DataFacade.TryGetDataVersionsByUniqueKey(interfaceType, key).ToList();

                        if (versions.Count > 1 &&
                            (dataset.Count() < versions.Count ||
                             !versions.All(v => dataPendingDeletion.Contains(v.DataSourceId))))
                        {
                            continue;
                        }
                    }

                    Verify.IsTrue(cascadeDeleteType != CascadeDeleteType.Disallow, "One of the given datas is referenced by one or more datas");

                    element.RemoveOptionalReferences();

                    IEnumerable <IData> referees;
                    using (new DataScope(element.DataSourceId.DataScopeIdentifier))
                    {
                        // For some weird reason, this line does not work.... /MRJ
                        // IEnumerable<IData> referees = dataset.GetRefereesRecursively();
                        referees = element.GetReferees(referencesFromAllScopes)
                                   .Where(reference => !dataPendingDeletion.Contains(reference.DataSourceId))
                                   .Evaluate();
                    }

                    foreach (IData referee in referees)
                    {
                        if (!referee.CascadeDeleteAllowed(interfaceType))
                        {
                            throw new InvalidOperationException("One of the given datas is referenced by one or more datas that does not allow cascade delete");
                        }
                    }

                    Delete <IData>(referees, suppressEventing, cascadeDeleteType, referencesFromAllScopes);
                }
            }


            Dictionary <string, Dictionary <Type, List <IData> > > sortedDatas = dataset.ToDataProviderAndInterfaceTypeSortedDictionary();

            foreach (KeyValuePair <string, Dictionary <Type, List <IData> > > providerPair in sortedDatas)
            {
                foreach (KeyValuePair <Type, List <IData> > interfaceTypePair in providerPair.Value)
                {
                    DataProviderPluginFacade.Delete(providerPair.Key, interfaceTypePair.Value.Select(d => d.DataSourceId));

                    if (DataCachingFacade.IsTypeCacheable(interfaceTypePair.Key))
                    {
                        DataCachingFacade.ClearCache(interfaceTypePair.Key, interfaceTypePair.Value.First().DataSourceId.DataScopeIdentifier);
                    }
                }
            }


            if (!suppressEventing)
            {
                foreach (IData element in dataset)
                {
                    DataEventSystemFacade.FireDataDeletedEvent(element.DataSourceId.InterfaceType, element);
                }
            }
        }
Ejemplo n.º 8
0
        public IQueryable <T> GetData <T>(bool useCaching, IEnumerable <string> providerNames)
            where T : class, IData
        {
            IQueryable <T> resultQueryable;

            if (DataProviderRegistry.AllInterfaces.Contains(typeof(T)))
            {
                if (useCaching && DataCachingFacade.IsDataAccessCacheEnabled(typeof(T)))
                {
                    resultQueryable = DataCachingFacade.GetDataFromCache <T>();
                }
                else
                {
                    if (providerNames == null)
                    {
                        providerNames = DataProviderRegistry.GetDataProviderNamesByInterfaceType(typeof(T));
                    }

                    List <IQueryable <T> > queryables = new List <IQueryable <T> >();
                    foreach (string providerName in providerNames)
                    {
                        IQueryable <T> queryable = DataProviderPluginFacade.GetData <T>(providerName);

                        queryables.Add(queryable);
                    }

                    bool resultIsCached = queryables.Count == 1 && queryables[0] is ICachedQuery;

                    if (resultIsCached)
                    {
                        resultQueryable = queryables[0];
                    }
                    else
                    {
                        var multipleSourceQueryable = new DataFacadeQueryable <T>(queryables);

                        resultQueryable = multipleSourceQueryable;
                    }
                }
            }
            else
            {
                DataProviderRegistry.CheckInitializationErrors(typeof(T));

                if (!typeof(T).GetCustomInterfaceAttributes <AutoUpdatebleAttribute>().Any())
                {
                    throw new ArgumentException(string.Format("The given interface type ({0}) is not supported by any data providers", typeof(T)));
                }

                resultQueryable = new List <T>().AsQueryable();
            }


            DataInterceptor dataInterceptor;

            this.DataInterceptors.TryGetValue(typeof(T), out dataInterceptor);
            if (dataInterceptor != null)
            {
                try
                {
                    resultQueryable = dataInterceptor.InterceptGetData <T>(resultQueryable);
                }
                catch (Exception ex)
                {
                    Log.LogError(LogTitle, "Calling data interceptor failed with the following exception");
                    Log.LogError(LogTitle, ex);
                }
            }

            return(resultQueryable);
        }