Esempio n. 1
0
        public void UpdateCrawlers(ICollection <RssCrawler> crawlers)
        {
            var existingCrawlers = _rssCrawlerGateway.Select();
            var dataRecs         = Mapper.Map <ICollection <Data.Models.RssCrawler> >(crawlers);
            var comparer         = new Func <Data.Models.RssCrawler, Data.Models.RssCrawler, bool>((x, y) => x.RssCrawlerId == y.RssCrawlerId);

            CollectionMergeHelper.MergeNewAndOld(
                newItems: dataRecs,
                existingItems: existingCrawlers,
                comparer: comparer,
                create: (x) =>
            {
                int id          = _rssCrawlerGateway.Insert(x);
                var dataFilters = Mapper.Map <ICollection <Data.Models.RssCrawlerFilter> >(
                    crawlers.Single(y => x.RssCrawlerId == y.RssCrawlerId).Filters);
                foreach (var filter in dataFilters)
                {
                    filter.RssCrawlerId = id;
                    _rssCrawlerFilterGateway.Insert(filter);
                }
            },
                update: (x) =>
            {
                _rssCrawlerGateway.Update(x);
                var filters     = _rssCrawlerFilterGateway.SelectByRssCrawler(x.RssCrawlerId);
                var dataFilters = Mapper.Map <ICollection <Data.Models.RssCrawlerFilter> >(
                    crawlers.Single(y => x.RssCrawlerId == y.RssCrawlerId).Filters);
                foreach (var filter in filters)
                {
                    _rssCrawlerFilterGateway.Delete(filter.RssCrawlerFilterId);
                }
                foreach (var filter in dataFilters)
                {
                    filter.RssCrawlerId = x.RssCrawlerId;
                    _rssCrawlerFilterGateway.Insert(filter);
                }
                //вместо добавления и удаления можно сделать ещё один мёрдж
            },
                delete: (x) =>
            {
                _rssCrawlerGateway.Delete(x.RssCrawlerId);
                // фильтры выпилятся сами
                return(true);
            }
                );
        }
Esempio n. 2
0
        public void MergeTest()
        {
            var oldCollection = new List <TestClass>()
            {
                new TestClass()
                {
                    Id = 1, Data = "first"
                },
                new TestClass()
                {
                    Id = 2, Data = "second"
                },
                new TestClass()
                {
                    Id = 3, Data = "third"
                },
                new TestClass()
                {
                    Id = 10, Data = "tenth"
                },
                new TestClass()
                {
                    Id = 100, Data = "hundredth"
                },
            };
            var newCollection = new List <TestClass>()
            {
                new TestClass()
                {
                    Id = 1, Data = "firstNew"
                },
                new TestClass()
                {
                    Id = 3, Data = "thirdNew"
                },
                new TestClass()
                {
                    Id = 4, Data = "fourthNew"
                },
            };

            var resultCollection = oldCollection.ToList();

            //bool Comparer(TestClass x, TestClass y)
            //{
            //    return x.Id == y.Id;
            //}

            var comparer = new Func <TestClass, TestClass, bool>((x, y) => x.Id == y.Id);

            int inserts = 0;
            int updates = 0;
            int deletes = 0;

            CollectionMergeHelper.MergeNewAndOld(
                newItems: newCollection,
                existingItems: oldCollection,
                comparer: comparer,
                create: x =>
            {
                resultCollection.Add(x);
                inserts++;
            },
                update: (x) =>
            {
                resultCollection.Single(y => comparer(x, y)).Data = x.Data;
                updates++;
            },
                delete: @class =>
            {
                deletes++;
                return(resultCollection.Remove(@class));
            });

            Assert.AreEqual(resultCollection.Count, 3);
            Assert.AreEqual(resultCollection.Single(x => x.Id == 1).Data, "firstNew");
            Assert.IsNull(resultCollection.SingleOrDefault(x => x.Id == 2));
            Assert.AreEqual(resultCollection.Single(x => x.Id == 3).Data, "thirdNew");
            Assert.AreEqual(resultCollection.Single(x => x.Id == 4).Data, "fourthNew");
        }