Exemplo n.º 1
0
        public void EPT_BatchImport()
        {
            int size = EntityTest.BATCH_IMPORT_DATA_SIZE;

            var repo = RF.ResolveInstance <InvoiceRepository>();

            using (RF.TransactionScope(repo))
            {
                var list = new InvoiceList();
                for (int i = 0; i < size; i++)
                {
                    var item = new Invoice();
                    list.Add(item);
                }
                repo.CreateImporter().Save(list);

                list.Clear();
                repo.CreateImporter().Save(list);

                Assert.AreEqual(repo.CountAll(), 0, "幽灵状态的实体,应该无法通过正常的 API 查出。");

                using (PhantomContext.DontFilterPhantoms())
                {
                    Assert.AreEqual(repo.CountAll(), size, "幽灵状态的实体,可以使用特定 API 查出。");
                    var all2 = repo.GetAll();
                    Assert.AreEqual(all2.Count, size, "幽灵状态的实体,应该无法通过正常的 API 查出。");
                    Assert.AreEqual(EntityPhantomExtension.GetIsPhantom(all2[0]), true, "幽灵状态的实体,IsPhantom 值为 true。");
                }
            }
        }
Exemplo n.º 2
0
        public void EPT_Query()
        {
            var repo = RF.ResolveInstance <InvoiceRepository>();

            using (RF.TransactionScope(repo))
            {
                var Invoice = new Invoice();
                repo.Save(Invoice);
                Assert.AreEqual(repo.CountAll(), 1);

                Invoice.PersistenceStatus = PersistenceStatus.Deleted;
                repo.Save(Invoice);

                Assert.AreEqual(repo.CountAll(), 0, "幽灵状态的实体,应该无法通过正常的 API 查出。");
                var all = repo.GetAll();
                Assert.AreEqual(all.Count, 0, "幽灵状态的实体,应该无法通过正常的 API 查出。");

                using (PhantomContext.DontFilterPhantoms())
                {
                    Assert.AreEqual(repo.CountAll(), 1, "幽灵状态的实体,可以使用特定 API 查出。");
                    var all2 = repo.GetAll();
                    Assert.AreEqual(all2.Count, 1, "幽灵状态的实体,应该无法通过正常的 API 查出。");
                    Assert.AreEqual(EntityPhantomExtension.GetIsPhantom(all2[0]), true, "幽灵状态的实体,IsPhantom 值为 true。");
                }
            }
        }
Exemplo n.º 3
0
        public void EPT_Clear_Query()
        {
            var repo = RF.ResolveInstance <InvoiceRepository>();

            using (RF.TransactionScope(repo))
            {
                var InvoiceList = new InvoiceList
                {
                    new Invoice(),
                    new Invoice(),
                    new Invoice()
                };
                repo.Save(InvoiceList);
                Assert.AreEqual(repo.CountAll(), 3);

                InvoiceList.Clear();
                repo.Save(InvoiceList);
                Assert.AreEqual(repo.CountAll(), 0, "幽灵状态的实体,应该无法通过正常的 API 查出。");
                var all = repo.GetAll();
                Assert.AreEqual(all.Count, 0, "幽灵状态的实体,应该无法通过正常的 API 查出。");

                using (PhantomContext.DontFilterPhantoms())
                {
                    Assert.AreEqual(repo.CountAll(), 3, "幽灵状态的实体,可以使用特定 API 查出。");
                    var all2 = repo.GetAll();
                    Assert.AreEqual(all2.Count, 3, "幽灵状态的实体,应该无法通过正常的 API 查出。");
                    Assert.AreEqual(EntityPhantomExtension.GetIsPhantom(all2[0]), true, "幽灵状态的实体,IsPhantom 值为 true。");
                    Assert.AreEqual(EntityPhantomExtension.GetIsPhantom(all2[1]), true, "幽灵状态的实体,IsPhantom 值为 true。");
                    Assert.AreEqual(EntityPhantomExtension.GetIsPhantom(all2[2]), true, "幽灵状态的实体,IsPhantom 值为 true。");
                }
            }
        }
Exemplo n.º 4
0
        public void EPT_BatchImport_Aggt()
        {
            if (IsTestDbSQLite())
            {
                return;
            }

            int size = EntityTest.BATCH_IMPORT_DATA_SIZE;

            var repo     = RF.ResolveInstance <InvoiceRepository>();
            var itemRepo = RF.ResolveInstance <InvoiceItemRepository>();

            using (RF.TransactionScope(repo))
            {
                var invoices = new InvoiceList();
                for (int i = 0; i < size; i++)
                {
                    var Invoice = new Invoice
                    {
                        InvoiceItemList =
                        {
                            new InvoiceItem(),
                            new InvoiceItem(),
                        }
                    };
                    invoices.Add(Invoice);
                }

                var importer = repo.CreateImporter();
                importer.Save(invoices);

                Assert.AreEqual(size, repo.CountAll());
                Assert.AreEqual(size * 2, itemRepo.CountAll());

                invoices.Clear();
                importer.Save(invoices);

                Assert.AreEqual(repo.CountAll(), 0, "幽灵状态的实体,应该无法通过正常的 API 查出。");
                Assert.AreEqual(itemRepo.CountAll(), 0, "幽灵状态的实体,应该无法通过正常的 API 查出。");

                using (PhantomContext.DontFilterPhantoms())
                {
                    Assert.AreEqual(repo.CountAll(), size, "幽灵状态的实体,可以使用特定 API 查出。");
                    var roots = repo.GetAll();
                    Assert.AreEqual(roots.Count, size, "幽灵状态的实体,应该无法通过正常的 API 查出。");
                    Assert.AreEqual(EntityPhantomExtension.GetIsPhantom(roots[0]), true, "幽灵状态的实体,IsPhantom 值为 true。");

                    Assert.AreEqual(itemRepo.CountAll(), size * 2, "幽灵状态的实体,可以使用特定 API 查出。");
                    var items = itemRepo.GetAll();
                    Assert.AreEqual(items.Count, size * 2, "幽灵状态的实体,应该无法通过正常的 API 查出。");
                    Assert.AreEqual(EntityPhantomExtension.GetIsPhantom(items[0]), true, "幽灵状态的实体,IsPhantom 值为 true。");
                }
            }
        }
Exemplo n.º 5
0
        public void EPT_IsPhantom()
        {
            var repo = RF.ResolveInstance <InvoiceRepository>();

            using (RF.TransactionScope(repo))
            {
                var inv = new Invoice();
                Assert.AreEqual(EntityPhantomExtension.GetIsPhantom(inv), false, "所有实体的幽灵标识为 false。");

                repo.Save(inv);
                Assert.AreEqual(repo.CountAll(), 1);
            }
        }
Exemplo n.º 6
0
        public void EPT_Aggt()
        {
            var repo     = RF.ResolveInstance <InvoiceRepository>();
            var itemRepo = RF.ResolveInstance <InvoiceItemRepository>();

            using (RF.TransactionScope(repo))
            {
                var item = new Invoice
                {
                    InvoiceItemList =
                    {
                        new InvoiceItem(),
                        new InvoiceItem(),
                    }
                };

                repo.Save(item);

                Assert.AreEqual(1, repo.CountAll());
                Assert.AreEqual(2, itemRepo.CountAll());

                item.PersistenceStatus = PersistenceStatus.Deleted;
                repo.Save(item);

                Assert.AreEqual(repo.CountAll(), 0, "幽灵状态的实体,应该无法通过正常的 API 查出。");
                Assert.AreEqual(itemRepo.CountAll(), 0, "幽灵状态的实体,应该无法通过正常的 API 查出。");

                using (PhantomContext.DontFilterPhantoms())
                {
                    Assert.AreEqual(repo.CountAll(), 1, "幽灵状态的实体,可以使用特定 API 查出。");
                    var roots = repo.GetAll();
                    Assert.AreEqual(roots.Count, 1, "幽灵状态的实体,应该无法通过正常的 API 查出。");
                    Assert.AreEqual(EntityPhantomExtension.GetIsPhantom(roots[0]), true, "幽灵状态的实体,IsPhantom 值为 true。");

                    Assert.AreEqual(itemRepo.CountAll(), 2, "幽灵状态的实体,可以使用特定 API 查出。");
                    var items = itemRepo.GetAll();
                    Assert.AreEqual(items.Count, 2, "幽灵状态的实体,应该无法通过正常的 API 查出。");
                    Assert.AreEqual(EntityPhantomExtension.GetIsPhantom(items[0]), true, "幽灵状态的实体,IsPhantom 值为 true。");
                }
            }
        }
Exemplo n.º 7
0
        private static void RepositoryDataProvider_Deleting(object sender, EntityCUDEventArgs e)
        {
            var dp = sender as RepositoryDataProvider;

            if (dp.Repository.EntityMeta.IsPhantomEnabled)
            {
                var entity = e.Entity;

                //删除数据的主逻辑变为修改 IsPhantom
                EntityPhantomExtension.SetIsPhantom(entity, true);

                //然后调用 DataSaver 来保存数据。(不直接使用 Sql 语句,因为这里并不一定是使用 RDb 作为持久层。)
                dp.DataSaver.UpdateToPersistence(entity);

                //由于删除后的实体的状态会变为‘New’,所以需要把这个字段的值重置。
                entity.ResetProperty(EntityPhantomExtension.IsPhantomProperty);

                //不再使用默认的删除逻辑。
                e.Cancel = true;
            }
        }