示例#1
0
        static void DoDelete(Entity entity, DeleteBehaviour behaviour)
        {
            // Raise deleting event
            if (!IsSet(behaviour, DeleteBehaviour.BypassDeleting))
            {
                var deletingArgs = new System.ComponentModel.CancelEventArgs();
                EntityManager.RaiseOnDeleting(entity, deletingArgs);

                if (deletingArgs.Cancel)
                {
                    Cache.Current.Remove(entity);
                    return;
                }
            }

            if (SoftDeleteAttribute.IsEnabled(entity.GetType()) && !SoftDeleteAttribute.Context.ShouldByPassSoftDelete())
            {
                // Soft delete:
                EntityManager.MarkSoftDeleted(entity);
                GetProvider(entity).Save(entity);
            }
            else
            {
                GetProvider(entity).Delete(entity);
            }
        }
        /// <summary>
        /// Returns a list of entities with the specified type.
        /// </summary>
        public static IEnumerable <T> GetList <T>(IEnumerable <ICriterion> criteria, params QueryOption[] options) where T : IEntity
        {
            List <T> result   = null;
            string   cacheKey = null;

            options = options ?? new QueryOption[0];

            var numberOfRecords = options.GetResultsToFetch();

            if (SoftDeleteAttribute.RequiresSoftdeleteQuery <T>())
            {
                criteria = criteria.Concat(new Criterion("IsMarkedSoftDeleted", false)).ToList();
            }

            var canCache = options.None() || (options.IsSingle() && numberOfRecords == 1);

            canCache &= criteria.OfType <DirectDatabaseCriterion>().All(x => x.IsCacheSafe);

            if (criteria.Except(typeof(DirectDatabaseCriterion)).Any(c => c.PropertyName.Contains(".")))
            {
                // This doesn't work with cache expiration rules.
                canCache = false;
            }

            if (canCache)
            {
                // Standard query, try the cache first:
                cacheKey = Cache.BuildQueryKey(typeof(T), criteria, numberOfRecords);

                if (result != null)
                {
                    return(result);
                }

                result = Cache.Current.GetList(typeof(T), cacheKey) as List <T>;
                if (result != null)
                {
                    return(result);
                }
            }

            result = GetConcreteList <T>(criteria, options);

            if (canCache)
            {
                // Do not cache a polymorphic call:
                if (!NeedsTypeResolution(typeof(T)))
                {
                    // Don't cache the result if it is fetched in a transaction.
                    if (!AnyOpenTransaction())
                    {
                        Cache.Current.AddList(typeof(T), cacheKey, result);
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Gets a list of entities of the given type from the database with the specified type matching the specified criteria.
        /// If no criteria is specified, the count of all instances will be returned.
        /// </summary>
        public static int Count <T>(params Criterion[] criteria) where T : IEntity
        {
            var criteriaItems = criteria.ToList();

            if (SoftDeleteAttribute.RequiresSoftdeleteQuery <T>())
            {
                criteriaItems.Add(new Criterion("IsMarkedSoftDeleted", false));
            }

            return(GetProvider(typeof(T)).Count(typeof(T), criteriaItems));
        }
示例#4
0
        /// <summary>
        /// Gets a list of entities of the given type from the database with the specified type matching the specified criteria.
        /// If no criteria is specified, the count of all instances will be returned.
        /// </summary>
        public TOutput?Aggregate <TProperty, TOutput>(AggregateFunction function, Expression <Func <TEntity, TProperty> > property,
                                                      params ICriterion[] criteria) where TOutput : struct
        {
            var criteriaItems = criteria.ToList();

            if (SoftDeleteAttribute.RequiresSoftdeleteQuery <TEntity>())
            {
                criteriaItems.Add(new Criterion("IsMarkedSoftDeleted", false));
            }

            var result = GetProvider(typeof(TEntity)).Aggregate(typeof(TEntity), function, property.GetPropertyName(), criteriaItems);

            return(result.ToStringOrEmpty().TryParseAs <TOutput>());
        }
示例#5
0
        /// <summary>
        /// Returns a list of entities with the specified type.
        /// </summary>
        public static IEnumerable <T> GetList <T>(IEnumerable <ICriterion> criteria, params QueryOption[] options) where T : IEntity
        {
            var optionsList = options?.ToList() ?? new List <QueryOption>();

            var criteriaList = criteria?.ToList() ?? new List <ICriterion>();

            if (SoftDeleteAttribute.RequiresSoftdeleteQuery <T>())
            {
                criteriaList.Add(new Criterion(nameof(Entity.IsMarkedSoftDeleted), false));
            }

            GettingList?.Invoke(typeof(T), criteriaList, optionsList);

            return(DoGetList <T>(criteriaList, optionsList));
        }
        /// <summary>
        /// Gets a list of entities of the given type from the database.
        /// </summary>
        public static int Count <T>(Expression <Func <T, bool> > criteria, params QueryOption[] options) where T : IEntity
        {
            var runner = ExpressionRunner <T> .CreateRunner(criteria);

            if (runner.DynamicCriteria == null)
            {
                var conditions = runner.Conditions.OfType <ICriterion>().ToList();

                if (SoftDeleteAttribute.RequiresSoftdeleteQuery <T>())
                {
                    conditions.Add(new Criterion("IsMarkedSoftDeleted", false));
                }

                return(GetProvider(typeof(T)).Count(typeof(T), conditions, options ?? new QueryOption[0]));
            }
            else
            {
                return(GetList <T>(criteria, options).Count());
            }
        }