/// <inheritdoc />
        public IEnumerable <TResource> BeforeUpdate <TResource>(IEnumerable <TResource> resources, ResourcePipeline pipeline) where TResource : class, IIdentifiable
        {
            if (GetHook(ResourceHook.BeforeUpdate, resources, out var container, out var node))
            {
                var relationships = node.RelationshipsToNextLayer.Select(p => p.Attribute).ToArray();
                var dbValues      = LoadDbValues(typeof(TResource), (IEnumerable <TResource>)node.UniqueResources, ResourceHook.BeforeUpdate, relationships);
                var diff          = new DiffableResourceHashSet <TResource>(node.UniqueResources, dbValues, node.LeftsToNextLayer(), _targetedFields);
                IEnumerable <TResource> updated = container.BeforeUpdate(diff, pipeline);
                node.UpdateUnique(updated);
                node.Reassign(_resourceFactory, resources);
            }

            FireNestedBeforeUpdateHooks(pipeline, _traversalHelper.CreateNextLayer(node));
            return(resources);
        }
Beispiel #2
0
        public void ResourceDiff_Loops_Over_Diffs()
        {
            // Arrange
            var dbResources = new HashSet <Dummy>(_allResources.Select(e => new Dummy {
                Id = e.Id
            }));
            DiffableResourceHashSet <Dummy> diffs = new DiffableResourceHashSet <Dummy>(_allResources, dbResources, _relationships, null);

            // Assert & act
            foreach (ResourceDiffPair <Dummy> diff in diffs.GetDiffs())
            {
                Assert.Equal(diff.Resource.Id, diff.DatabaseValue.Id);
                Assert.NotEqual(diff.Resource, diff.DatabaseValue);
                Assert.Contains(diff.Resource, _allResources);
                Assert.Contains(diff.DatabaseValue, dbResources);
            }
        }
Beispiel #3
0
        public void ResourceDiff_GetAffected_Relationships()
        {
            // Arrange
            var dbResources = new HashSet <Dummy>(_allResources.Select(e => new Dummy {
                Id = e.Id
            }));
            DiffableResourceHashSet <Dummy> diffs = new DiffableResourceHashSet <Dummy>(_allResources, dbResources, _relationships, null);

            // Act
            var affectedThroughFirstToOne  = diffs.GetAffected(d => d.FirstToOne).ToList();
            var affectedThroughSecondToOne = diffs.GetAffected(d => d.SecondToOne).ToList();
            var affectedThroughToMany      = diffs.GetAffected(d => d.ToManies).ToList();

            // Assert
            affectedThroughFirstToOne.ForEach(resource => Assert.Contains(resource, _firstToOnesResources));
            affectedThroughSecondToOne.ForEach(resource => Assert.Contains(resource, _secondToOnesResources));
            affectedThroughToMany.ForEach(resource => Assert.Contains(resource, _toManiesResources));
        }
Beispiel #4
0
        public void ResourceDiff_GetAffected_Attributes()
        {
            // Arrange
            var dbResources = new HashSet <Dummy>(_allResources.Select(e => new Dummy {
                Id = e.Id
            }));
            var updatedAttributes = new Dictionary <PropertyInfo, HashSet <Dummy> >
            {
                { typeof(Dummy).GetProperty(nameof(Dummy.SomeUpdatedProperty)) !, _allResources }
            };
            DiffableResourceHashSet <Dummy> diffs = new DiffableResourceHashSet <Dummy>(_allResources, dbResources, _relationships, updatedAttributes);

            // Act
            var affectedThroughSomeUpdatedProperty    = diffs.GetAffected(d => d.SomeUpdatedProperty);
            var affectedThroughSomeNotUpdatedProperty = diffs.GetAffected(d => d.SomeNotUpdatedProperty);

            // Assert
            Assert.NotEmpty(affectedThroughSomeUpdatedProperty);
            Assert.Empty(affectedThroughSomeNotUpdatedProperty);
        }
Beispiel #5
0
        /// <inheritdoc />
        public IEnumerable <TResource> BeforeUpdate <TResource>(IEnumerable <TResource> resources, ResourcePipeline pipeline)
            where TResource : class, IIdentifiable
        {
            GetHookResult <TResource> result = GetHook(ResourceHook.BeforeUpdate, resources);

            if (result.Succeeded)
            {
                RelationshipAttribute[] relationships = result.Node.RelationshipsToNextLayer.Select(proxy => proxy.Attribute).ToArray();

                IEnumerable dbValues = LoadDbValues(typeof(TResource), (IEnumerable <TResource>)result.Node.UniqueResources, ResourceHook.BeforeUpdate,
                                                    relationships);

                var diff = new DiffableResourceHashSet <TResource>(result.Node.UniqueResources, dbValues, result.Node.LeftsToNextLayer(), _targetedFields);
                IEnumerable <TResource> updated = result.Container.BeforeUpdate(diff, pipeline);
                result.Node.UpdateUnique(updated);
                result.Node.Reassign(resources);
            }

            FireNestedBeforeUpdateHooks(pipeline, _nodeNavigator.CreateNextLayer(result.Node));
            return(resources);
        }
        public void ResourceDiff_GetByRelationships()
        {
            // Arrange
            var dbResources = new HashSet <Dummy>(_allResources.Select(resource => new Dummy
            {
                Id = resource.Id
            }).ToList());

            var diffs = new DiffableResourceHashSet <Dummy>(_allResources, dbResources, _relationships, null);

            // Act
            IDictionary <RelationshipAttribute, HashSet <Dummy> > toOnes           = diffs.GetByRelationship <ToOne>();
            IDictionary <RelationshipAttribute, HashSet <Dummy> > toManies         = diffs.GetByRelationship <ToMany>();
            IDictionary <RelationshipAttribute, HashSet <Dummy> > notTargeted      = diffs.GetByRelationship <NotTargeted>();
            IDictionary <RelationshipAttribute, HashSet <Dummy> > allRelationships = diffs.AffectedRelationships;

            // Assert
            AssertRelationshipDictionaryGetters(allRelationships, toOnes, toManies, notTargeted);
            List <Dummy> allResourcesWithAffectedRelationships = allRelationships.SelectMany(pair => pair.Value).ToList();

            _noRelationshipsResources.ToList().ForEach(resource =>
            {
                Assert.DoesNotContain(resource, allResourcesWithAffectedRelationships);
            });

            DiffableResourceHashSet <Dummy> requestResourcesFromDiff = diffs;

            requestResourcesFromDiff.ToList().ForEach(resource =>
            {
                Assert.Contains(resource, _allResources);
            });

            IEnumerable <Dummy> databaseResourcesFromDiff = diffs.GetDiffs().Select(pair => pair.DatabaseValue);

            databaseResourcesFromDiff.ToList().ForEach(resource =>
            {
                Assert.Contains(resource, dbResources);
            });
        }
        public void ResourceDiff_GetByRelationships()
        {
            // Arrange
            var dbResources = new HashSet <Dummy>(AllResources.Select(e => new Dummy {
                Id = e.Id
            }).ToList());
            DiffableResourceHashSet <Dummy> diffs = new DiffableResourceHashSet <Dummy>(AllResources, dbResources, Relationships, null);

            // Act
            Dictionary <RelationshipAttribute, HashSet <Dummy> > toOnes           = diffs.GetByRelationship <ToOne>();
            Dictionary <RelationshipAttribute, HashSet <Dummy> > toManies         = diffs.GetByRelationship <ToMany>();
            Dictionary <RelationshipAttribute, HashSet <Dummy> > notTargeted      = diffs.GetByRelationship <NotTargeted>();
            Dictionary <RelationshipAttribute, HashSet <Dummy> > allRelationships = diffs.AffectedRelationships;

            // Assert
            AssertRelationshipDictionaryGetters(allRelationships, toOnes, toManies, notTargeted);
            var allResourcesWithAffectedRelationships = allRelationships.SelectMany(kvp => kvp.Value).ToList();

            NoRelationshipsResources.ToList().ForEach(e =>
            {
                Assert.DoesNotContain(e, allResourcesWithAffectedRelationships);
            });

            var requestResourcesFromDiff = diffs;

            requestResourcesFromDiff.ToList().ForEach(e =>
            {
                Assert.Contains(e, AllResources);
            });
            var databaseResourcesFromDiff = diffs.GetDiffs().Select(d => d.DatabaseValue);

            databaseResourcesFromDiff.ToList().ForEach(e =>
            {
                Assert.Contains(e, dbResources);
            });
        }