Example of adding reference types to a POCO that: - Doesn't persist as complex type blob in OrmLite. - Doesn't impact other queries using the POCO. - Can save and load references independently from itself. - Loaded references are serialized in Text serializers.
Esempio n. 1
0
        public void Can_create_and_populate_tables_without_quotes()
        {
            using (var db = OpenDbConnection())
            {
                ((PostgreSqlDialectProvider)DialectProvider).Normalize = true;

                CustomerOrdersUseCase.DropTables(db); //Has conflicting 'Order' table
                db.DropTable <Order>();
                db.DropTable <CustomerAddress>();
                db.DropTable <Customer>();

                db.CreateTable <Customer>();
                db.CreateTable <CustomerAddress>();
                db.CreateTable <Order>();

                db.GetLastSql().Print();

                var customer = new Customer
                {
                    Name           = "Customer 1",
                    PrimaryAddress = new CustomerAddress
                    {
                        AddressLine1 = "1 Humpty Street",
                        City         = "Humpty Doo",
                        State        = "Northern Territory",
                        Country      = "Australia"
                    },
                    Orders = new[] {
                        new Order {
                            LineItem = "Line 1", Qty = 1, Cost = 1.99m
                        },
                        new Order {
                            LineItem = "Line 2", Qty = 2, Cost = 2.99m
                        },
                    }.ToList(),
                };

                db.Save(customer, references: true);
                db.GetLastSql().Print();

                var dbCustomer = db.SingleById <Customer>(customer.Id);
                Assert.That(dbCustomer.Name, Is.EqualTo(customer.Name));
                dbCustomer = db.SqlList <Customer>("select * from Customer where Id = @Id", new { customer.Id })[0];
                Assert.That(dbCustomer.Name, Is.EqualTo(customer.Name));

                var address = db.Single <CustomerAddress>(x => x.CustomerId == customer.Id && x.Id == customer.PrimaryAddress.Id);
                Assert.That(address.Country, Is.EqualTo("Australia"));

                var orders   = db.Select <Order>(x => x.CustomerId == customer.Id);
                var totalQty = orders.Sum(x => x.Qty);
                Assert.That(totalQty, Is.EqualTo(3));

                //PostgreSqlDialectProvider.Instance.Normalize = false;
            }
        }
        public void Can_hijack_References_Apis()
        {
            var customer = new Customer
            {
                Id = 1,
                Name = "Customer 1",
                PrimaryAddress = new CustomerAddress
                {
                    AddressLine1 = "1 Humpty Street",
                    City = "Humpty Doo",
                    State = "Northern Territory",
                    Country = "Australia"
                },
                Orders = new[] { 
                    new Order { LineItem = "Line 1", Qty = 1, Cost = 1.99m },
                    new Order { LineItem = "Line 2", Qty = 2, Cost = 2.99m },
                }.ToList(),
            };

            var sqlStatements = new List<string>();
            var sqlCommandStatements = new List<SqlCommandDetails>();
            using (new OrmLiteResultsFilter
            {
                    SqlFilter = sql => sqlStatements.Add(sql),
                SqlCommandFilter = sql => sqlCommandStatements.Add(new SqlCommandDetails(sql)),
                    SingleResult = customer,
                    RefSingleResultFn = (dbCmd, refType) => customer.PrimaryAddress,
                    RefResultsFn = (dbCmd, refType) => customer.Orders,
                })
            {
                int i = 0;

                i += 2; db.Save(customer);
                Assert.That(sqlStatements.Count, Is.EqualTo(i));

                SuppressIfOracle("This seems wrong here as the save actually goes through to the database in Oracle to get the next number from the sequence");

                i += 1; db.SaveReferences(customer, customer.PrimaryAddress);
                Assert.That(sqlStatements.Count, Is.EqualTo(i));
                Assert.That(sqlCommandStatements.Count, Is.EqualTo(i));

                i += 2; db.SaveReferences(customer, customer.Orders);
                Assert.That(sqlStatements.Count, Is.EqualTo(i));
                Assert.That(sqlCommandStatements.Count, Is.EqualTo(i));

                i += 3; var dbCustomer = db.LoadSingleById<Customer>(customer.Id);
                Assert.That(sqlStatements.Count, Is.EqualTo(i));
                Assert.That(sqlCommandStatements.Count, Is.EqualTo(i));

                sqlStatements.Each(x => x.Print());
                sqlCommandStatements.Each(x => x.PrintDump());
            }
        }
        public void Can_hijack_References_Apis()
        {
            var customer = new Customer
            {
                Id = 1,
                Name = "Customer 1",
                PrimaryAddress = new CustomerAddress
                {
                    AddressLine1 = "1 Humpty Street",
                    City = "Humpty Doo",
                    State = "Northern Territory",
                    Country = "Australia"
                },
                Orders = new[] { 
                    new Order { LineItem = "Line 1", Qty = 1, Cost = 1.99m },
                    new Order { LineItem = "Line 2", Qty = 2, Cost = 2.99m },
                }.ToList(),
            };

            var sqlStatements = new List<string>();
            using (new ResultsFilter
                {
                    SqlFilter = sql => sqlStatements.Add(sql),
                    SingleResult = customer,
                    RefSingleResultFn = (dbCmd, refType) => customer.PrimaryAddress,
                    RefResultsFn = (dbCmd, refType) => customer.Orders,
                })
            {
                int i = 0;

                i += 2; db.Save(customer);
                Assert.That(sqlStatements.Count, Is.EqualTo(i));

                i += 1; db.SaveReferences(customer, customer.PrimaryAddress);
                Assert.That(sqlStatements.Count, Is.EqualTo(i));

                i += 2; db.SaveReferences(customer, customer.Orders);
                Assert.That(sqlStatements.Count, Is.EqualTo(i));

                i += 3; var dbCustomer = db.LoadSingleById<Customer>(customer.Id);
                Assert.That(sqlStatements.Count, Is.EqualTo(i));

                sqlStatements.Each(x => x.Print());
            }
        }
        public void Can_Save_and_Load_References()
        {
            var customer = new Customer
            {
                Name = "Customer 1",
                PrimaryAddress = new CustomerAddress
                {
                    AddressLine1 = "1 Humpty Street",
                    City = "Humpty Doo",
                    State = "Northern Territory",
                    Country = "Australia"
                },
                Orders = new[] { 
                    new Order { LineItem = "Line 1", Qty = 1, Cost = 1.99m },
                    new Order { LineItem = "Line 2", Qty = 2, Cost = 2.99m },
                }.ToList(),
            };

            db.Save(customer);

            Assert.That(customer.Id, Is.GreaterThan(0));
            Assert.That(customer.PrimaryAddress.CustomerId, Is.EqualTo(0));

            db.SaveReferences(customer, customer.PrimaryAddress);
            Assert.That(customer.PrimaryAddress.CustomerId, Is.EqualTo(customer.Id));

            db.SaveReferences(customer, customer.Orders);
            Assert.That(customer.Orders.All(x => x.CustomerId == customer.Id));

            var dbCustomer = db.LoadSingleById<Customer>(customer.Id);

            dbCustomer.PrintDump();

            Assert.That(dbCustomer.PrimaryAddress, Is.Not.Null);
            Assert.That(dbCustomer.Orders.Count, Is.EqualTo(2));
        }