示例#1
0
        /// <summary>
        /// 尝试更新依赖指定实体的值的冗余属性的值。
        /// </summary>
        /// <param name="entity">变更了值属性的实体。依赖该实体的所有冗余属性都需要被更新。</param>
        /// <param name="repository">实体对应的仓库。</param>
        public void UpdateRedundancies(Entity entity, IRepository repository)
        {
            if (repository == null)
            {
                repository = entity.GetRepository();
            }

            //如果有一些在冗余属性路径中的属性的值改变了,则开始更新数据库的中的所有冗余字段的值。
            Entity dbEntity         = null;
            var    propertiesInPath = repository.GetPropertiesInRedundancyPath();

            for (int i = 0, c = propertiesInPath.Count; i < c; i++)
            {
                var property = propertiesInPath[i];

                //如果只有一个属性,那么就是它变更引起的更新
                //否则,需要从数据库获取原始值来对比检测具体哪些属性值变更,然后再发起冗余更新。
                bool isChanged = c == 1;

                var refProperty = property as IRefIdProperty;
                if (refProperty != null)
                {
                    if (!isChanged)
                    {
                        if (dbEntity == null)
                        {
                            dbEntity = ForceGetById(entity, repository);
                        }
                        var dbId  = dbEntity.GetRefId(refProperty);
                        var newId = entity.GetRefId(refProperty);
                        isChanged = !object.Equals(dbId, newId);
                    }

                    if (isChanged)
                    {
                        foreach (var path in property.InRedundantPathes)
                        {
                            //如果这条路径中是直接把引用属性的值作为值属性进行冗余,那么同样要进行值属性更新操作。
                            if (path.ValueProperty.Property == property)
                            {
                                this.UpdateRedundancyByRefValue(entity, path, refProperty);
                            }
                            //如果是引用变更了,并且只有一个 RefPath,则不需要处理。
                            //因为这个已经在属性刚变更时的处理函数中实时处理过了。
                            else if (path.RefPathes.Count > 1)
                            {
                                this.UpdateRedundancyByIntermidateRef(entity, path, refProperty);
                            }
                        }
                    }
                }
                else
                {
                    var newValue = entity.GetProperty(property);

                    if (!isChanged)
                    {
                        if (dbEntity == null)
                        {
                            dbEntity = ForceGetById(entity, repository);
                        }
                        var dbValue = dbEntity.GetProperty(property);
                        isChanged = !object.Equals(dbValue, newValue);
                    }

                    if (isChanged)
                    {
                        foreach (var path in property.InRedundantPathes)
                        {
                            UpdateRedundancyByValue(entity, path, newValue);
                        }
                    }
                }
            }

            entity.UpdateRedundancies = false;
        }