Ejemplo n.º 1
0
        protected int Insert(object data, ISchema schema, IDataInsertOptions options = null)
        {
            //确认是否可以执行该操作
            this.EnsureInsert(options);

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

            //构建数据操作的选项对象
            if (options == null)
            {
                options = new DataInsertOptions();
            }

            //进行授权验证
            this.Authorize(DataServiceMethod.Insert(), options);

            //将当前插入数据对象转换成数据字典
            var dictionary = DataDictionary.GetDictionary <TModel>(data);

            //修正数据模式对象
            schema = this.Schematic(schema, data.GetType());

            //验证待新增的数据
            this.OnValidate(DataServiceMethod.Insert(), schema, dictionary, options);

            return(this.OnInsert(dictionary, schema, options));
        }
        private static IEnumerable <IDataDictionary <T> > GetDataDictionaries <T>(this IDataMutateContextBase context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.Count < 1)
            {
                return(Array.Empty <IDataDictionary <T> >());
            }

            if (context.IsMultiple)
            {
                return(DataDictionary.GetDictionaries <T>((System.Collections.IEnumerable)context.Data));
            }

            return(new IDataDictionary <T>[] { DataDictionary.GetDictionary <T>(context.Data) });
        }
Ejemplo n.º 3
0
        protected int Update(object data, ICondition criteria, ISchema schema, IDataUpdateOptions options = null)
        {
            //确认是否可以执行该操作
            this.EnsureUpdate(options);

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

            //构建数据操作的选项对象
            if (options == null)
            {
                options = new DataUpdateOptions();
            }

            //进行授权验证
            this.Authorize(DataServiceMethod.Update(), options);

            //将当前更新数据对象转换成数据字典
            var dictionary = DataDictionary.GetDictionary <TModel>(data);

            //如果指定了更新条件,则尝试将条件中的主键值同步设置到数据字典中
            if (criteria != null)
            {
                //获取当前数据服务的实体主键集
                var keys = this.DataAccess.Metadata.Entities.Get(this.Name).Key;

                if (keys != null && keys.Length > 0)
                {
                    foreach (var key in keys)
                    {
                        criteria.Match(key.Name, c => dictionary.TrySetValue(c.Name, c.Value));
                    }
                }
            }

            //修整过滤条件
            criteria = this.OnValidate(DataServiceMethod.Update(), criteria ?? this.GetUpdateKey(dictionary), options.Filter, options);

            //修正数据模式对象
            schema = this.Schematic(schema, data.GetType());

            //验证待更新的数据
            this.OnValidate(DataServiceMethod.Update(), schema, dictionary, options);

            //如果缺少必须的更新条件则抛出异常
            if (criteria == null)
            {
                //再次从数据中获取主键条件
                criteria = this.GetUpdateKey(dictionary);

                if (criteria == null)
                {
                    throw new DataOperationException($"The update operation of the specified ‘{this.Name}’ entity missing required conditions.");
                }
            }

            //执行更新操作
            return(this.OnUpdate(dictionary, criteria, schema, options));
        }