fixing missing reference
[castle.git] / Samples / ActiveRecord / MoreComplexSample / App.cs
blob0483fbe3201c29feeedb57be62201ec9fbeeaf46
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace MoreComplexSample
17 using System;
18 using Castle.ActiveRecord;
19 using Castle.ActiveRecord.Framework.Config;
21 public class App
23 public static void Main()
25 // Old way
26 // Hashtable properties = new Hashtable();
28 // properties.Add("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
29 // properties.Add("hibernate.dialect", "NHibernate.Dialect.MsSql2000Dialect");
30 // properties.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
31 // properties.Add("hibernate.connection.connection_string",
32 // "Data Source=.;Initial Catalog=bookdb;Integrated Security=SSPI");
34 // InPlaceConfigurationSource source = new InPlaceConfigurationSource();
35 // source.Add(typeof(ActiveRecordBase), properties);
37 // New way
38 InPlaceConfigurationSource config = InPlaceConfigurationSource.BuildForMSSqlServer(".", "test");
40 ActiveRecordStarter.Initialize(config,
41 typeof(LineItem), typeof(Order),
42 typeof(Category), typeof(Product),
43 typeof(Customer));
45 // Framework started, let's create the schema
47 ActiveRecordStarter.CreateSchema();
49 // Now let's play
51 Customer invalid = new Customer();
52 invalid.Name = "john"; // Less than the minimum
53 invalid.Email = "someinvalidemail.com";
55 if (!invalid.IsValid())
57 foreach(String msg in invalid.ValidationErrorMessages)
59 Console.WriteLine(msg);
63 try
65 // This will fail
66 invalid.Create();
68 catch(Exception ex)
72 Order order;
73 Product product;
75 using(new SessionScope())
77 Category root = new Category("Petshot");
78 root.Create();
80 Category c1 = new Category("Dogs");
81 c1.Parent = root;
82 c1.Create();
84 product = new Product();
85 product.Name = "It";
86 product.Price = 10f;
87 product.Categories.Add(c1);
88 product.Create();
90 Customer customer = new Customer();
91 customer.Name = "another customer";
92 customer.Email = "foo@bar.com";
93 customer.Create();
95 order = new Order();
96 order.Customer = customer;
97 order.Total = 100f;
98 order.Create();
100 // Associate order and product (the hard way)
101 LineItem item = new LineItem(order, product);
102 item.Quantity = 10;
103 item.Create();
106 using(new SessionScope())
108 // Change just the association
109 LineItem item = LineItem.Find(order, product);
110 item.Quantity = 12;