Esempio n. 1
0
        public MappingInstruction(MappingInstruction parent,PropertyMap parentCollectionPropertyMap, DataMap dataMap, MappingDirection changeDirection, Object source,Object target)
        {
            this.Parent = parent;
            this.ParentCollectionPropertyMap = parentCollectionPropertyMap;
            this.DataMap = dataMap;
            this.ChangeDirection = changeDirection;
            this.Source = source;
            this.Target = target;

            this.Children = new MappingInstructionList();

            this.SetCommandType();
        }
        private MappingInstruction BuildCommand(MappingInstruction parent, DataMap dataMap, PropertyMap parentCollectionPropertyMap, MappingDirection direction, Object source, Object target)
        {
            //create a new command. This will figure out based on the direction and the source and target objects
            //whether we are doing an add, update, delete or nothing.
            MappingInstruction dataMapCommand = new MappingInstruction(
                parent, parentCollectionPropertyMap, dataMap, direction, source, target);

            //find all the collection properties.
            var collectionProperties = dataMap.DataMapCollectionList.Select(a=>a.PropertyMap).ToList();

            //loop through the collection properties
            foreach (var collectionProperty in collectionProperties)
            {
                var sourceList = collectionProperty.SourcePropertyInfo.TryExtractIDataMapperList(source);
                var targetList = collectionProperty.TargetPropertyInfo.TryExtractIDataMapperList(target);
                var dataMapForCollection = dataMap.FindDataMapForCollectionProperty(collectionProperty);
                var sourceKeyAndObjectList = this.BuildKeyAndObjectPairList(sourceList, dataMapForCollection, true);
                var targetKeyAndObjectList = this.BuildKeyAndObjectPairList(targetList, dataMapForCollection, false);

                //do the full outer join
                var sourceToTargetFullOuterJoin = sourceKeyAndObjectList.FullOuterJoin(
                    targetKeyAndObjectList,
                    x => x.Key,
                    x => x.Key,
                    (x1, x2) => new Tuple<KeyAndObjectPair<CompositeKey>, KeyAndObjectPair<CompositeKey>>(x1, x2))
                    .ToList();

                //at this point, we have all the things put together for us
                foreach (var item in sourceToTargetFullOuterJoin)
                {
                    //yummy recursion
                    dataMapCommand.Children.Add(
                        this.BuildCommand(
                        dataMapCommand,
                        dataMapForCollection,
                        collectionProperty,
                        direction,
                        item.Item1 == null ? null : item.Item1.Value,
                        item.Item2 == null ? null : item.Item2.Value));
                }
            }

            return dataMapCommand;
        }