示例#1
0
 public Task Load() => TaskManager.StartNew(() =>
 {
     using (printer.Indent($"Loading side '{Definition.Name}':"))
     {
         var rr  = new List <LoadedItem>();
         var res = ((Side <TSource, TItem>)Definition).OnLoad(((Side <TSource, TItem>)Definition).Source)?.Cast <object>() ?? Enumerable.Empty <object>();
         foreach (var item in res)
         {
             var loadedItem = new LoadedItem
             {
                 Item  = item,
                 Sides = Enumerable.Empty <ISideRunner>().ToList()
             };
             if (SubSides != null)
             {
                 foreach (var subSide in SubSides)
                 {
                     var clon             = subSide.Clone();
                     clon.Source          = item;
                     clon.Definition.Name = $"{clon.Definition.Name.Replace("%sourceName%", GetItemName(item))}";
                     //clon.Definition.Name = $"{clon.Definition.Name} ({GetItemName(item)})";
                     printer.WriteLine($"Creating side-clon '{clon.Definition.Name}-{clon.Definition.IsMaster}' with source '{GetItemName(item)}'");
                     clon.Load().Wait();
                     loadedItem.Sides.Add(clon);
                 }
             }
             rr.Add(loadedItem);
         }
         Entries = rr;
     }
     printer.WriteLine($"Side '{Definition.Name}' loaded");
 });
示例#2
0
        public IComparatorResultInternal CompareItems(LoadedItem itemA, LoadedItem itemB, bool runInverted, IPrinter printer)
        {
            IComparatorResultInternal res = runInverted
                ? (IComparatorResultInternal) new ComparatorResult <TItemB, TItemA, TKey>()
            {
                Key = itemB?.Item != null?definition.OnSelectKeyB((TItemB)itemB.Item) : definition.OnSelectKeyA((TItemA)itemA.Item),
                          MasterItem = itemB?.Item != null ? (TItemB)itemB.Item : null,
                          SideItem   = itemA?.Item != null ? (TItemA)itemA.Item : null,
            }
                : new ComparatorResult <TItemA, TItemB, TKey>()
            {
                Key = itemA?.Item != null?definition.OnSelectKeyA((TItemA)itemA.Item) : definition.OnSelectKeyB((TItemB)itemB.Item),
                          MasterItem = itemA?.Item != null ? (TItemA)itemA.Item : null,
                          SideItem   = itemB?.Item != null ? (TItemB)itemB.Item : null,
            };

            if (itemA?.Item != null && itemB?.Item != null)
            {
                if (itemA?.Item != null && !(itemA?.Item is TItemA))
                {
                    throw new InvalidCastException($"Parameter '{nameof(itemA)}' must be of type '{typeof(TItemA).FullName}'");
                }
                if (itemB?.Item != null && !(itemB?.Item is TItemB))
                {
                    throw new InvalidCastException($"Parameter '{nameof(itemB)}' must be of type '{typeof(TItemB).FullName}'");
                }
                //if (definition.OnCompare == null && definition.PropertiesComparator == null) throw new ArgumentException($"You must define '{nameof(definition.OnCompare)}' or '{nameof(definition.PropertiesComparator)}' to make it works");
                //definition.OnCompare?.Invoke((TItemA)itemA.Item, (TItemB)itemB.Item, res);
                if (definition.PropertiesComparator != null)
                {
                    foreach (var pro in definition.PropertiesComparator.Compare((TItemA)itemA.Item, (TItemB)itemB.Item, runInverted))
                    {
                        res.Properties.Add(pro);
                    }
                }
            }
            var processSubSides = new Action <LoadedItem, LoadedItem>((a, b) =>
            {
                //if (a.Item == null || b.Item == null) return;
                foreach (var side in b.Sides)
                {
                    if (side.GetItemType() == side.Comparator.GetItemTypes().Item1)
                    {
                        var side2    = a.Sides.SingleOrDefault(s => s.GetItemType() == side.Comparator.GetItemTypes().Item2);
                        side.Results = side.Comparator.CompareSides(side, side2, true, printer);
                    }
                    else
                    {
                        var side2    = a.Sides.SingleOrDefault(s => s.GetItemType() == side.Comparator.GetItemTypes().Item1);
                        side.Results = side.Comparator.CompareSides(side2, side, false, printer);
                    }
                    res.SubSides.Add(side);
                }
            });

            if (runInverted)
            {
                //CompareItems(itemA, itemB, true);
                processSubSides(itemB, itemA);
            }
            else
            {
                //CompareItems(itemB, itemA, false);
                processSubSides(itemA, itemB);
            }
            return(res);
        }