Example #1
0
        public void TestFixtureSetup()
        {
            if (Directory.Exists("CustomerDataTests"))
            {
                Directory.Delete("CustomerDataTests", recursive: true);
            }

            _sessionFactory = SessionFactory.CreateTestSessionFactory(new ConsoleMapPath().MapPath("CustomerDataTests"));
            var doc    = XDocument.Load(Path.Combine("TestData", "TestData.xml"));
            var import = new XmlImport(doc, "http://tempuri.org/Database.xsd");

            using (var session = _sessionFactory.OpenSession())
            {
                import.Parse(new[] { typeof(Customer), typeof(Order), typeof(Product) },
                             (type, obj) =>
                {
                    Switch.On(obj)
                    .Case((Customer c) => session.Save(c))
                    .Case((Order o) => session.Save(o))
                    .Case((Product p) => session.Save(p))
                    .ElseFail();
                },
                             onIgnore: (type, property) =>
                {
                    Console.WriteLine("ignoring property {1} on {0}", type.Name, property.PropertyType.Name);
                });
                session.Commit();
            }
            using (var session = _sessionFactory.OpenSession())
            {
                int sequence = 0;
                import.ParseConnections("OrderProduct", "Product", "Order", (productId, orderId) =>
                {
                    var product = session.Get <Product>(productId);
                    var order   = session.Get <Order>(orderId);
                    session.Save(new OrderProduct {
                        OrderId = order.Id, ProductId = product.Id, Id = ++sequence
                    });
                });

                import.ParseIntProperty("Order", "Customer", (orderId, customerId) =>
                {
                    session.Get <Order>(orderId).Customer = session.Get <Customer>(customerId).Id;
                });
                session.Commit();
            }
        }
        public void TestFixtureSetup()
        {
            if (Directory.Exists("CustomerDataTests")) { Directory.Delete("CustomerDataTests", recursive: true); }

            _sessionFactory = SessionFactory.CreateTestSessionFactory(new ConsoleMapPath().MapPath("CustomerDataTests"));
            var doc = XDocument.Load(Path.Combine("TestData", "TestData.xml"));
            var import = new XmlImport(doc, "http://tempuri.org/Database.xsd");
            using (var session = _sessionFactory.OpenSession())
            {
                import.Parse(new[] { typeof(Customer), typeof(Order), typeof(Product) },
                    (type, obj) =>
                    {
                        Switch.On(obj)
                            .Case((Customer c) => session.Save(c))
                            .Case((Order o) => session.Save(o))
                            .Case((Product p) => session.Save(p))
                            .ElseFail();
                    },
                    onIgnore: (type, property) =>
                    {
                        Console.WriteLine("ignoring property {1} on {0}", type.Name, property.PropertyType.Name);
                    });
                session.Commit();
            }
            using (var session = _sessionFactory.OpenSession())
            {
                int sequence = 0;
                import.ParseConnections("OrderProduct", "Product", "Order", (productId, orderId) =>
                {
                    var product = session.Get<Product>(productId);
                    var order = session.Get<Order>(orderId);
                    session.Save(new OrderProduct { OrderId = order.Id, ProductId = product.Id, Id = ++sequence });
                });

                import.ParseIntProperty("Order", "Customer", (orderId, customerId) =>
                {
                    session.Get<Order>(orderId).Customer = session.Get<Customer>(customerId).Id;
                });
                session.Commit();
            }
        }