Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="root"></param>
        /// <param name="parent"></param>
        /// <param name="current"></param>
        /// <param name="tip"></param>
        /// <param name="data"></param>
        /// <returns>当内部成员发生变化,返回true</returns>
        private bool UpdateAndCollectChangedValue(DomainObject root, DomainObject parent, DomainObject current, PropertyRepositoryAttribute tip, DynamicData data)
        {
            switch (tip.DomainPropertyType)
            {
            case DomainPropertyType.Primitive:
            {
                if (current.IsPropertyChanged(tip.Property))
                {
                    var value = GetPrimitivePropertyValue(current, tip);
                    data.Add(tip.PropertyName, value);
                    return(true);
                }
            }
            break;

            case DomainPropertyType.PrimitiveList:
            {
                if (current.IsPropertyChanged(tip.Property))
                {
                    //删除老数据
                    var child = GetChildTableByRuntime(this, tip);
                    child.DeleteMiddleByMaster(root, current);

                    var value = current.GetValue(tip.Property);
                    //仅存中间表
                    var values = GetValueListData(value);
                    child.InsertMiddle(root, current, values);
                    return(true);
                }
            }
            break;

            //case DomainPropertyType.ValueObject:
            //    {
            //        if (current.IsPropertyChanged(tip.Property))
            //        {
            //            //删除原始数据
            //            DeleteMemberByOriginalData(root, parent, current, tip);
            //            //新增数据
            //            InsertAndCollectValueObject(root, parent, current, tip, data);
            //            return true;
            //        }
            //    }
            //    break;
            case DomainPropertyType.AggregateRoot:
            {
                if (current.IsPropertyChanged(tip.Property))
                {
                    var    field = GetQuoteField(this, tip.PropertyName);
                    object obj   = current.GetValue(tip.Property);
                    var    id    = GetObjectId(obj);
                    data.Add(field.Name, id);
                    return(true);
                }
            }
            break;

            case DomainPropertyType.ValueObject:     //虽然值对象的成员不会变,但是成员的成员也许会改变
            case DomainPropertyType.EntityObject:
            {
                if (current.IsPropertyChanged(tip.Property))
                {
                    var obj = current.GetValue(tip.Property) as DomainObject;
                    if (tip.DomainPropertyType == DomainPropertyType.ValueObject)
                    {
                        (obj as IValueObject).TrySetId(Guid.NewGuid());
                    }

                    var id    = GetObjectId(obj);
                    var field = GetQuoteField(this, tip.PropertyName);
                    data.Add(field.Name, id);          //收集外键

                    //删除原始数据
                    DeleteMemberByOriginalData(root, parent, current, tip);

                    //保存引用数据
                    if (!obj.IsEmpty())
                    {
                        var child = GetRuntimeTable(this, tip.PropertyName, obj.ObjectType);
                        child.InsertMember(root, current, obj);
                    }
                    return(true);
                }
                else if (current.IsPropertyDirty(tip.Property))
                {
                    //如果引用的内聚成员是脏对象,那么需要修改
                    var obj = current.GetValue(tip.Property) as DomainObject;
                    if (!obj.IsEmpty())
                    {
                        //从衍生表中找到对象表
                        var child = GetRuntimeTable(this, tip.PropertyName, obj.ObjectType);
                        child.UpdateMember(root, parent, obj);
                    }
                    return(true);
                }
            }
            break;

            case DomainPropertyType.AggregateRootList:
            {
                if (current.IsPropertyChanged(tip.Property))
                {
                    //删除老数据
                    var child = GetChildTableByRuntime(this, tip);
                    child.Middle.DeleteMiddleByMaster(root, current);

                    //追加新数据
                    var objs = current.GetValue(tip.Property) as IEnumerable;
                    child.Middle.InsertMiddle(root, current, objs);
                    return(true);
                }
            }
            break;

            case DomainPropertyType.ValueObjectList:
            case DomainPropertyType.EntityObjectList:
            {
                if (current.IsPropertyChanged(tip.Property))
                {
                    //引用关系发生了变化,删除重新追加
                    //这里要注意,需要删除的是数据库的数据,所以要重新读取
                    //删除原始数据
                    DeleteMembersByOriginalData(root, parent, current, tip);

                    //加入新数据
                    InsertMembers(root, parent, current, tip);
                    return(true);
                }
                else if (current.IsPropertyDirty(tip.Property))
                {
                    //引用关系没变,只是数据脏了
                    UpdateMembers(root, parent, current, tip);
                    return(true);
                }
            }
            break;
            }
            return(false);
        }