示例#1
0
        /// <summary>
        /// 根据指定的条件将指定的实体集更新到数据源。
        /// </summary>
        /// <param name="name">指定的实体映射名。</param>
        /// <param name="items">要更新的数据集。</param>
        /// <param name="condition">要更新的条件子句,如果为空(null)则根据实体的主键进行更新。</param>
        /// <param name="scope">指定的要更新的和排除更新的属性名列表,如果指定的是多个属性则属性名之间使用逗号(,)分隔;要排除的属性以减号(-)打头,星号(*)表示所有属性,感叹号(!)表示排除所有属性;如果未指定该参数则默认只会更新所有单值属性而不会更新导航属性。</param>
        /// <returns>返回受影响的记录行数,执行成功返回大于零的整数,失败则返回负数。</returns>
        public int UpdateMany(string name, IEnumerable items, ICondition condition = null, string scope = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (items == null)
            {
                return(0);
            }

            //激发“ManyUpdating”事件
            var args = this.OnManyUpdating(name, items, condition, scope);

            if (args.Cancel)
            {
                return(args.Count);
            }

            //定义数据访问过滤器上下文
            var filterContext = new DataAccessFilterContext(this, DataAccessMethod.UpdateMany, args);

            //调用数据访问过滤器前事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuting(ctx));

            //执行数据更新操作
            args.Count = this.OnUpdateMany(name, args.Data, condition, scope);

            //调用数据访问过滤器后事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuted(ctx));

            //激发“ManyUpdated”事件
            return(this.OnManyUpdated(name, args.Data, args.Condition, args.Scope, args.Count));
        }
示例#2
0
        public long Decrement(string name, string member, ICondition condition, int interval = 1)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (string.IsNullOrWhiteSpace(member))
            {
                throw new ArgumentNullException(nameof(member));
            }

            //激发“Decrementing”事件
            var args = this.OnDecrementing(name, member, condition, interval);

            if (args.Cancel)
            {
                return(args.Result);
            }

            //定义数据访问过滤器上下文
            var filterContext = new DataAccessFilterContext(this, DataAccessMethod.Decrement, args);

            //调用数据访问过滤器前事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuting(ctx));

            //执行递减操作方法
            args.Result = this.OnDecrement(name, member, condition, interval);

            //调用数据访问过滤器后事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuted(ctx));

            //激发“Decremented”事件
            return(this.OnIncremented(name, args.Member, args.Condition, args.Interval, args.Result));
        }
示例#3
0
        public int Delete(string name, ICondition condition, params string[] cascades)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (cascades != null && cascades.Length == 1)
            {
                cascades = cascades[0].Split(',', ';');
            }

            //激发“Deleting”事件
            var args = this.OnDeleting(name, condition, cascades);

            if (args.Cancel)
            {
                return(args.Count);
            }

            //定义数据访问过滤器上下文
            var filterContext = new DataAccessFilterContext(this, DataAccessMethod.Delete, args);

            //调用数据访问过滤器前事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuting(ctx));

            //执行数据删除操作
            args.Count = this.OnDelete(name, condition, cascades);

            //调用数据访问过滤器后事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuted(ctx));

            //激发“Deleted”事件
            return(this.OnDeleted(name, args.Condition, args.Cascades, args.Count));
        }
示例#4
0
        public int Insert(string name, object data, string scope = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (data == null)
            {
                return(0);
            }

            //激发“Inserting”事件
            var args = this.OnInserting(name, data, scope);

            if (args.Cancel)
            {
                return(args.Count);
            }

            //定义数据访问过滤器上下文
            var filterContext = new DataAccessFilterContext(this, DataAccessMethod.Insert, args);

            //调用数据访问过滤器前事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuting(ctx));

            //执行数据插入操作
            args.Count = this.OnInsert(name, args.Data, scope);

            //调用数据访问过滤器后事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuted(ctx));

            //激发“Inserted”事件
            return(this.OnInserted(name, args.Data, args.Scope, args.Count));
        }
示例#5
0
        public IEnumerable <T> Select <T>(string name, ICondition condition, Grouping grouping, string scope, Paging paging, params Sorting[] sortings)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            //激发“Selecting”事件
            var args = this.OnSelecting(name, typeof(T), condition, grouping, scope, paging, sortings);

            if (args.Cancel)
            {
                return((args.Result as IEnumerable <T>) ?? Enumerable.Empty <T>());
            }

            //定义数据访问过滤器上下文
            var filterContext = new DataAccessFilterContext(this, DataAccessMethod.Select, args);

            //调用数据访问过滤器前事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuting(ctx));

            //执行数据查询操作
            args.Result = this.OnSelect <T>(name, condition, grouping, scope, paging, sortings);

            //调用数据访问过滤器后事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuted(ctx));

            //激发“Selected”事件
            return(this.OnSelected(name, typeof(T), args.Condition, args.Grouping, args.Scope, args.Paging, args.Sortings, (IEnumerable <T>)args.Result));
        }
示例#6
0
        public int Count(string name, ICondition condition, string includes = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            //激发“Counting”事件
            var args = this.OnCounting(name, condition, includes);

            if (args.Cancel)
            {
                return(args.Result);
            }

            //定义数据访问过滤器上下文
            var filterContext = new DataAccessFilterContext(this, DataAccessMethod.Count, args);

            //调用数据访问过滤器前事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuting(ctx));

            //执行计数操作方法
            args.Result = this.OnCount(name, condition, string.IsNullOrWhiteSpace(includes) ? (string[])null : includes.Split(',', ';'));

            //调用数据访问过滤器后事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuted(ctx));

            //激发“Counted”事件
            return(this.OnCounted(name, args.Condition, args.Includes, args.Result));
        }
示例#7
0
        public bool Exists(string name, ICondition condition)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            //激发“Existing”事件
            var args = this.OnExisting(name, condition, false);

            if (args.Cancel)
            {
                return(args.Result);
            }

            //定义数据访问过滤器上下文
            var filterContext = new DataAccessFilterContext(this, DataAccessMethod.Exists, args);

            //调用数据访问过滤器前事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuting(ctx));

            //执行存在操作方法
            args.Result = this.OnExists(name, condition);

            //调用数据访问过滤器后事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuted(ctx));

            //激发“Existed”事件
            return(this.OnExisted(name, args.Condition, args.Result));
        }
示例#8
0
        public object ExecuteScalar(string name, IDictionary <string, object> inParameters, out IDictionary <string, object> outParameters)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            //激发“Executing”事件
            var args = this.OnExecuting(name, typeof(object), inParameters, out outParameters);

            if (args.Cancel)
            {
                return(args.Result);
            }

            //定义数据访问过滤器上下文
            var filterContext = new DataAccessFilterContext(this, DataAccessMethod.ExecuteScalar, args);

            //调用数据访问过滤器前事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuting(ctx));

            //执行数据操作方法
            args.Result = this.OnExecuteScalar(name, inParameters, out outParameters);

            //调用数据访问过滤器后事件
            this.InvokeFilters(filterContext, (filter, ctx) => filter.OnExecuted(ctx));

            //激发“Executed”事件
            return(this.OnExecuted(name, typeof(object), args.InParameters, ref outParameters, args.Result));
        }
示例#9
0
        private void InvokeFilters(DataAccessFilterContext context, Action <IDataAccessFilter, DataAccessFilterContext> invoke)
        {
            foreach (var filter in _filters)
            {
                if (filter == null)
                {
                    continue;
                }

                var predication = filter as Zongsoft.Services.IPredication;

                if (predication == null || predication.Predicate(context))
                {
                    invoke(filter, context);
                }
            }
        }