public void interceptors_are_not_created_for_re_entrant_proxies()
        {
            var metadataFactory      = new AttributeMetadataFactory(new NullLogger());
            var proxyFactory         = new DynamicProxyFactory(metadataFactory, new NullLogger());
            var queryExecutorFactory = new Mock <IQueryExecutorFactory>();
            var repositoryFactory    = new RepositoryFactory(proxyFactory);
            var parentMapper         = new Mock <IMapper <Parent> >();

            repositoryFactory.Register <Parent>(x => parentMapper.Object);
            var childMapper = new Mock <IMapper <Child> >();

            repositoryFactory.Register <Child>(x => childMapper.Object);

            var stateTracker = new EntityStateCache();

            using (var session = new GraphSession(new UnitOfWork(stateTracker), new List <IListener>(), queryExecutorFactory.Object, repositoryFactory, stateTracker, proxyFactory))
            {
                var proxy = session.Create(new Parent
                {
                    Id = "1"
                });
                proxy.Child = session.Create(new Child
                {
                    Id = "1"
                });

                var metadata = metadataFactory.Create(proxy);
                var deleted  = ProxyUtils.Flush(proxy, metadata["Child"].Relationship);

                Assert.Equal(0, deleted.SelectMany(x => x.Flush().Cast <object>()).Count());

                var reentrant = session.Create(proxy);
                Assert.Equal(proxy, reentrant);

                metadata = metadataFactory.Create(reentrant);
                deleted  = ProxyUtils.Flush(reentrant, metadata["Child"].Relationship);
                Assert.Equal(0, deleted.SelectMany(x => x.Flush().Cast <object>()).Count());

                proxy.Child = session.Create(new Child
                {
                    Id = "2"
                });
                metadata = metadataFactory.Create(reentrant);
                deleted  = ProxyUtils.Flush(reentrant, metadata["Child"].Relationship);
                Assert.Equal(1, deleted.SelectMany(x => x.Flush().Cast <object>()).Count());
            }
        }
Beispiel #2
0
        private void CreateRelationship(object source, IEnumerable collection, RelationshipMetadata relationship)
        {
            if (relationship == null || relationship.IsReverse)
            {
                return;
            }

            foreach (var destination in collection)
            {
                CreateRelationship(source, destination, relationship);
            }

            if (!ProxyUtils.IsTrackable(collection))
            {
                return;
            }

            foreach (var destination in ProxyUtils.Flush(collection))
            {
                DeleteRelationship(source, destination, relationship);
            }
        }
Beispiel #3
0
        private void CreateRelationship(object source, RelationshipMetadata relationship)
        {
            if (relationship.IsReverse)
            {
                return;
            }

            foreach (var trackableRelationship in ProxyUtils.Flush(source, relationship))
            {
                foreach (var destination in trackableRelationship.Flush())
                {
                    DeleteRelationship(source, destination, relationship);
                }

                if (trackableRelationship.Current == null)
                {
                    continue;
                }

                CreateRelationship(source, trackableRelationship.Current, relationship);
            }
        }
        public void a_proxy_is_generated_for_trackable_relationships()
        {
            var metadataFactory      = new AttributeMetadataFactory(new NullLogger());
            var queryExecutorFactory = new Mock <IQueryExecutorFactory>();
            var repositoryFactory    = new Mock <IRepositoryFactory>();
            var stateTracker         = new EntityStateCache();
            var proxyFactory         = new DynamicProxyFactory(metadataFactory, new NullLogger());

            using (var session = new GraphSession(new UnitOfWork(stateTracker), new List <IListener>(), queryExecutorFactory.Object, repositoryFactory.Object, stateTracker, proxyFactory))
            {
                var person = new Person
                {
                    Id      = "1",
                    Name    = "Test",
                    Address = new Address
                    {
                        Id   = "1",
                        Name = "first address"
                    }
                };
                var proxy = proxyFactory.Create(person, session);
                proxy.Address = null;
                proxy.Address = proxyFactory.Create(new Address {
                    Id = "2"
                }, session);
                var metadata = metadataFactory.Create(proxy);
                var trackableRelationships = ProxyUtils.Flush(proxy, metadata["Address"].Relationship).ToList();
                Assert.Equal(1, trackableRelationships.Count);
                foreach (var trackableRelationship in trackableRelationships)
                {
                    var enumerable = trackableRelationship.Flush();
                    var results    = enumerable.Cast <object>().ToList();
                    Assert.Equal(1, results.Count);
                    Assert.Equal("1", ((Address)results[0]).Id);
                    Assert.Equal("2", proxy.Address.Id);
                }
            }
        }