Esempio n. 1
0
        public void UpdateItemTest()
        {
            var         customerKey = CreateCustomer();
            VersionInfo customerVersion;

            using (var session = Domain.OpenSession()) {
                using (var transactionScope = session.OpenTransaction()) {
                    var customer = session.Query.Single <Customer>(customerKey);
                    customerVersion = customer.VersionInfo;
                    transactionScope.Complete();
                }
            }

            Func <Key, VersionInfo> versionGetter = key => {
                if (key == customerKey)
                {
                    return(customerVersion);
                }
                else
                {
                    throw new ArgumentException();
                }
            };

            using (var session = Domain.OpenSession()) {
                using (VersionValidator.Attach(session, versionGetter)) {
                    using (var transactionScope = session.OpenTransaction()) {
                        var customer = session.Query.Single <Customer>(customerKey);
                        customer.Name = "Customer3";
                        transactionScope.Complete();
                    }
                }
            }
        }
Esempio n. 2
0
        public void VersionValidatorTest()
        {
            var domain = GetDomain();

            using (var session = domain.OpenSession())
                using (var scope = session.Activate()) {
                    var versions = new VersionSet();

                    Person alex;
                    Person dmitri;
                    using (var tx = session.OpenTransaction()) {
                        alex   = new Person(session, "Yakunin, Alex");
                        dmitri = new Person(session, "Maximov, Dmitri");
                        tx.Complete();
                    }

                    using (VersionCapturer.Attach(session, versions))
                        using (var tx = session.OpenTransaction()) {
                            // Simulating entity displaying @ web page
                            // By default this leads to their addition to VersionSet
                            Dump(alex);
                            Dump(dmitri);
                            tx.Complete();
                        }

                    // Let's clone VersionSet (actually - serialize & deserialize)
                    versions = Cloner.Clone(versions);
                    // And dump it
                    Dump(versions);

                    using (VersionValidator.Attach(session, versions))
                        using (var tx = session.OpenTransaction()) {
                            alex.Name = "Edited Alex";  // Passes
                            alex.Name = "Edited again"; // Passes, because this is not the very first modification
                            tx.Complete();
                        }

                    AssertEx.Throws <VersionConflictException>(() => {
                        using (VersionValidator.Attach(session, versions))
                            using (var tx = session.OpenTransaction()) {
                                alex.Name = "And again";
                                tx.Complete();
                            }// Version check fails on Session.Persist() here
                    });

                    using (var tx = session.OpenTransaction())
                        versions.Add(alex, true); // Overwriting versions
                    Dump(versions);

                    using (VersionValidator.Attach(session, versions))
                        using (var tx = session.OpenTransaction()) {
                            alex.Name = "Edited again"; // Passes now
                            tx.Complete();
                        }
                }
        }
        public virtual void OptimisticUpdate <T>(Key key, VersionInfo expectedVersion, Action <T> updater)
            where T : class, IEntity
        {
            var expectedVersions = new VersionSet(
                new List <KeyValuePair <Key, VersionInfo> > {
                new KeyValuePair <Key, VersionInfo>(key, expectedVersion),
            });

            var session = Session.Demand();

            using (VersionValidator.Attach(session, expectedVersions)) {
                using (var tx = session.OpenTransaction()) {
                    var entity = session.Query.Single <T>(key);
                    updater.Invoke(entity);
                    session.Validate();
                    tx.Complete();
                }
            }
        }
Esempio n. 4
0
        public void RemoveChangedItemTest()
        {
            var         customerKey = CreateCustomer();
            VersionInfo customerVersion;

            using (var session = Domain.OpenSession()) {
                using (var transactionScope = session.OpenTransaction()) {
                    var customer = session.Query.Single <Customer>(customerKey);
                    customerVersion = customer.VersionInfo;
                    transactionScope.Complete();
                }
            }

            RenameCustomer(customerKey);

            Func <Key, VersionInfo> versionGetter = key => {
                if (key == customerKey)
                {
                    return(customerVersion);
                }
                else
                {
                    throw new ArgumentException();
                }
            };

            using (var session = Domain.OpenSession()) {
                using (VersionValidator.Attach(session, versionGetter)) {
                    AssertEx.Throws <VersionConflictException>(() => {
                        using (var transactionScope = session.OpenTransaction()) {
                            var customer = session.Query.Single <Customer>(customerKey);
                            customer.Remove();
                            transactionScope.Complete();
                        }
                    });
                }
            }
        }
        public void SkipTest()
        {
            var config = DomainConfigurationFactory.Create();

            config.Types.Register(typeof(Base));
            config.Types.Register(typeof(Skip));
            config.Types.Register(typeof(VersionBehavior.Model.Version));
            config.Types.Register(typeof(HasVersion));
            config.Types.Register(typeof(HasSkipVersion));
            var domain                 = Domain.Build(config);
            var skipTypeInfo           = domain.Model.Types[typeof(Skip)];
            var hasVersionTypeInfo     = domain.Model.Types[typeof(HasVersion)];
            var hasSkipVersionTypeInfo = domain.Model.Types[typeof(HasSkipVersion)];

            Assert.AreEqual(2, skipTypeInfo.GetVersionColumns().Count);
            Assert.AreEqual(2, hasVersionTypeInfo.GetVersionColumns().Count);
            Assert.AreEqual(2, hasSkipVersionTypeInfo.GetVersionColumns().Count);
            using (var session = domain.OpenSession()) {
                var        versions        = new VersionSet();
                var        updatedVersions = new VersionSet();
                Skip       skip;
                HasVersion hasVersion;
                using (VersionCapturer.Attach(session, versions))
                    using (var t = session.OpenTransaction()) {
                        skip = new Skip()
                        {
                            Content = "Content", Tag = "Tag", Description = "Desription", NotVersion = "NotVersion"
                        };
                        hasVersion = new HasVersion {
                            Content = "Content",
                            Tag     = "Tag",
                            Version = { Major = 10, Minor = 100, Meta = 1000 }
                        };
                        t.Complete();
                    }
                using (VersionCapturer.Attach(session, updatedVersions))
                    using (VersionValidator.Attach(session, versions))
                        using (var t = session.OpenTransaction()) {
                            skip.Content       = "AnotherContent";
                            skip.Content       = "AnotherContetnCorrect";
                            hasVersion.Content = "AnotherContent";
                            hasVersion.Content = "AnotherContetnCorrect";
                            t.Complete();
                        }
                AssertEx.Throws <VersionConflictException>(() => {
                    using (VersionValidator.Attach(session, versions))
                        using (var t = session.OpenTransaction()) {
                            skip.Tag       = "AnotherTag";
                            hasVersion.Tag = "AnotherTag";
                            t.Complete();
                        }
                });

                using (VersionValidator.Attach(session, updatedVersions))
                    using (var t = session.OpenTransaction()) {
                        skip.Tag       = "AnotherTag";
                        hasVersion.Tag = "AnotherTag";
                        t.Complete();
                    }
            }
            var allVersions = new VersionSet();

            using (var session = domain.OpenSession())
                using (VersionCapturer.Attach(session, allVersions))
                    using (VersionValidator.Attach(session, allVersions)) {
                        Skip       skip;
                        HasVersion hasVersion;
                        using (var t = session.OpenTransaction()) {
                            skip = new Skip()
                            {
                                Content = "Content", Tag = "Tag", Description = "Desription", NotVersion = "NotVersion"
                            };
                            hasVersion = new HasVersion {
                                Content = "Content",
                                Tag     = "Tag",
                                Version = { Major = 10, Minor = 100, Meta = 1000 }
                            };
                            t.Complete();
                        }
                        using (var t = session.OpenTransaction()) {
                            skip.Content       = "AnotherContent";
                            skip.Content       = "AnotherContetnCorrect";
                            hasVersion.Content = "AnotherContent";
                            hasVersion.Content = "AnotherContetnCorrect";
                            t.Complete();
                        }
                        using (var t = session.OpenTransaction()) {
                            skip.Tag       = "AnotherTag";
                            hasVersion.Tag = "AnotherTag";
                            t.Complete();
                        }
                    }
        }
        public void AutoTest()
        {
            var config = DomainConfigurationFactory.Create();

            config.Types.Register(typeof(Base));
            config.Types.Register(typeof(Auto));
            config.Types.Register(typeof(AutoInheritor));
            var domain                = Domain.Build(config);
            var autoTypeInfo          = domain.Model.Types[typeof(Auto)];
            var autoInheritorTypeInfo = domain.Model.Types[typeof(AutoInheritor)];

            Assert.AreEqual(1, autoTypeInfo.GetVersionColumns().Count);
            Assert.AreEqual(2, autoInheritorTypeInfo.GetVersionColumns().Count);
            using (var session = domain.OpenSession()) {
                var           versions        = new VersionSet();
                var           updatedVersions = new VersionSet();
                Auto          auto;
                AutoInheritor autoInheritor;
                using (VersionCapturer.Attach(session, versions))
                    using (var t = session.OpenTransaction()) {
                        auto = new Auto()
                        {
                            Content = "Content", Tag = "Tag"
                        };
                        autoInheritor = new AutoInheritor()
                        {
                            Content = "Content", Tag = "Tag"
                        };
                        t.Complete();
                    }
                using (VersionCapturer.Attach(session, updatedVersions))
                    using (VersionValidator.Attach(session, versions))
                        using (var t = session.OpenTransaction()) {
                            auto.Content          = "AnotherContent";
                            auto.Content          = "AnotherContetnCorrect";
                            autoInheritor.Content = "AnotherContent";
                            autoInheritor.Content = "AnotherContetnCorrect";
                            t.Complete();
                        }
                AssertEx.Throws <VersionConflictException>(() => {
                    using (VersionValidator.Attach(session, versions))
                        using (var t = session.OpenTransaction()) {
                            auto.Tag          = "AnotherTag";
                            autoInheritor.Tag = "AnotherTag";
                            t.Complete();
                        }
                });

                using (VersionValidator.Attach(session, updatedVersions))
                    using (var t = session.OpenTransaction()) {
                        auto.Tag          = "AnotherTag";
                        autoInheritor.Tag = "AnotherTag";
                        t.Complete();
                    }
            }
            var allVersions = new VersionSet();

            using (var session = domain.OpenSession())
                using (VersionCapturer.Attach(session, allVersions))
                    using (VersionValidator.Attach(session, allVersions)) {
                        Auto          auto;
                        AutoInheritor autoInheritor;
                        using (var t = session.OpenTransaction()) {
                            auto = new Auto()
                            {
                                Content = "Content", Tag = "Tag"
                            };
                            autoInheritor = new AutoInheritor()
                            {
                                Content = "Content", Tag = "Tag"
                            };
                            t.Complete();
                        }
                        using (var t = session.OpenTransaction()) {
                            auto.Content          = "AnotherContent";
                            auto.Content          = "AnotherContetnCorrect";
                            autoInheritor.Content = "AnotherContent";
                            autoInheritor.Content = "AnotherContetnCorrect";
                            t.Complete();
                        }

                        using (var t = session.OpenTransaction()) {
                            auto.Tag          = "AnotherTag";
                            autoInheritor.Tag = "AnotherTag";
                            t.Complete();
                        }
                    }
        }
        public void ManualTest()
        {
            var config = DomainConfigurationFactory.Create();

            config.Types.Register(typeof(Base));
            config.Types.Register(typeof(Manual));
            config.Types.Register(typeof(AnotherManual));
            config.Types.Register(typeof(ManualInheritor));
            config.Types.Register(typeof(AnotherManualInheritor));
            var domain                         = Domain.Build(config);
            var manualTypeInfo                 = domain.Model.Types[typeof(Manual)];
            var anotherManualTypeInfo          = domain.Model.Types[typeof(AnotherManual)];
            var manualInheritorTypeInfo        = domain.Model.Types[typeof(ManualInheritor)];
            var anotherManualInheritorTypeInfo = domain.Model.Types[typeof(AnotherManualInheritor)];

            Assert.AreEqual(1, manualTypeInfo.GetVersionColumns().Count);
            Assert.AreEqual(2, anotherManualTypeInfo.GetVersionColumns().Count);
            Assert.AreEqual(1, manualInheritorTypeInfo.GetVersionColumns().Count);
            Assert.AreEqual(2, anotherManualInheritorTypeInfo.GetVersionColumns().Count);
            using (var session = domain.OpenSession()) {
                var                    versions        = new VersionSet();
                var                    updatedVersions = new VersionSet();
                Manual                 manual;
                ManualInheritor        manualInheritor;
                AnotherManual          anotherManual;
                AnotherManualInheritor anotherManualInheritor;
                using (VersionCapturer.Attach(session, versions))
                    using (var t = session.OpenTransaction()) {
                        manual = new Manual()
                        {
                            Tag = "Tag", Content = "Content", Version = 1
                        };
                        manualInheritor = new ManualInheritor()
                        {
                            Tag = "Tag", Content = "Content", Version = 1, Name = "Name"
                        };
                        anotherManual = new AnotherManual()
                        {
                            Tag = "Tag", Content = "Content", Version = 1, SubVersion = 100
                        };
                        anotherManualInheritor = new AnotherManualInheritor()
                        {
                            Tag = "Tag", Content = "Content", Version = 1, SubVersion = 100, Name = "Name"
                        };
                        t.Complete();
                    }
                using (VersionCapturer.Attach(session, updatedVersions))
                    using (VersionValidator.Attach(session, versions))
                        using (var t = session.OpenTransaction()) {
                            manual.Version                    = 2;
                            manual.Tag                        = "AnotherTag";
                            manual.Tag                        = "AnotherTagCorrect";
                            manualInheritor.Name              = "AnotherName";
                            manualInheritor.Version           = 2;
                            anotherManual.Tag                 = "AnotherTag";
                            anotherManual.SubVersion          = 200;
                            anotherManualInheritor.Name       = "AnotherName";
                            anotherManualInheritor.SubVersion = 200;
                            t.Complete();
                        }
                AssertEx.Throws <VersionConflictException>(() => {
                    using (VersionValidator.Attach(session, versions))
                        using (var t = session.OpenTransaction()) {
                            manual.Tag                  = "YetAnotherTag";
                            anotherManual.Tag           = "YetAnotherTag";
                            anotherManual.Version       = 2;
                            manualInheritor.Name        = "YetAnotherName";
                            anotherManualInheritor.Name = "YetAnotherName";
                            t.Complete();
                        }
                });

                using (VersionValidator.Attach(session, updatedVersions))
                    using (var t = session.OpenTransaction()) {
                        manual.Tag                  = "YetAnotherTag";
                        anotherManual.Tag           = "YetAnotherTag";
                        anotherManual.Version       = 2;
                        manualInheritor.Name        = "YetAnotherName";
                        anotherManualInheritor.Name = "YetAnotherName";
                        t.Complete();
                    }
            }
        }
        public void DefaultTest()
        {
            var config = DomainConfigurationFactory.Create();

            config.Types.Register(typeof(Base));
            config.Types.Register(typeof(Default));
            config.Types.Register(typeof(DefaultInheritor));
            var domain                   = Domain.Build(config);
            var defaultTypeInfo          = domain.Model.Types[typeof(Default)];
            var defaultInheritorTypeInfo = domain.Model.Types[typeof(DefaultInheritor)];

            Assert.AreEqual(3, defaultTypeInfo.GetVersionColumns().Count);
            Assert.AreEqual(4, defaultInheritorTypeInfo.GetVersionColumns().Count);
            using (var session = domain.OpenSession()) {
                var     versions        = new VersionSet();
                var     updatedVersions = new VersionSet();
                Default @default;
                using (VersionCapturer.Attach(session, versions))
                    using (var t = session.OpenTransaction()) {
                        @default = new Default()
                        {
                            Name = "Name", Tag = "Tag", Version = 1
                        };
                        t.Complete();
                    }
                using (VersionCapturer.Attach(session, updatedVersions))
                    using (VersionValidator.Attach(session, versions))
                        using (var t = session.OpenTransaction()) {
                            @default.Version = 2;
                            @default.Name    = "AnotherName";
                            @default.Name    = "AnotherNameCorrect";
                            t.Complete();
                        }
                AssertEx.Throws <VersionConflictException>(() => {
                    using (VersionValidator.Attach(session, versions))
                        using (var t = session.OpenTransaction()) {
                            @default.Tag = "AnotherTag";
                            t.Complete();
                        }
                });

                using (VersionValidator.Attach(session, updatedVersions))
                    using (var t = session.OpenTransaction()) {
                        @default.Tag = "AnotherTag";
                        t.Complete();
                    }
            }
            var allVersions = new VersionSet();

            using (var session = domain.OpenSession())
                using (VersionCapturer.Attach(session, allVersions))
                    using (VersionValidator.Attach(session, allVersions)) {
                        Default @default;
                        using (var t = session.OpenTransaction()) {
                            @default = new Default()
                            {
                                Name = "Name", Tag = "Tag", Version = 1
                            };
                            t.Complete();
                        }

                        using (var t = session.OpenTransaction()) {
                            @default.Version = 2;
                            @default.Name    = "AnotherName";
                            @default.Name    = "AnotherNameCorrect";
                            t.Complete();
                        }

                        using (var t = session.OpenTransaction()) {
                            @default.Tag = "AnotherTag";
                            t.Complete();
                        }
                    }
        }