Introduction
简介
本章中,除了最后的两个小节示例外,所有的示例开始时都遵照下面的步骤.这些步骤包含了映射模型,配置log4net和NHibernate的通用代码,此外还包含有必要的但是重复的管道代码.
1. 完成第一章中的Eg.Core项目.
2. 在解决方案中添加一个新的控制台程序.
3. 为项目添加App.config文件.
4. 依照第二章中的Configuring NHibenate with App.config和Configuring NHibernate Logging小节,在App.config中配置log4net和NHibernate.
5. 创建一个名为ExampleDataCreator 的类,代码如下:
View Code
public class ExampleDataCreator { private readonly ISessionFactory _sessionFactory; public ExampleDataCreator( ISessionFactory sessionFactory) { if (sessionFactory == null) throw new ArgumentNullException("sessionFactory"); _sessionFactory = sessionFactory; } public void SetUpDatabase() { using (var session = _sessionFactory.OpenSession()) using (var tx = session.BeginTransaction()) { ClearDatabase(session); CreateMovies(session); CreateBook(session); tx.Commit(); } } private static void ClearDatabase(ISession session) { session .CreateQuery("delete from ActorRole") .ExecuteUpdate(); session .CreateQuery("delete from Product") .ExecuteUpdate(); } private static void CreateMovies(ISession session) { session.Save( new Movie() { Name = "Raiders of the Lost Ark", Description = "Awesome", UnitPrice = 9.59M, Director = "Steven Spielberg", Actors = new List<ActorRole>() { new ActorRole() { Actor = "Harrison Ford", Role = "Indiana Jones" } } }); session.Save( new Movie() { Name = "The Bucket List", Description = "Good", UnitPrice = 15M, Director = "Rob Reiner", Actors = new List<ActorRole>() { new ActorRole() { Actor = "Jack Nicholson", Role = "Edward Cole" }, new ActorRole() { Actor = "Morgan Freeman", Role = "Carter Chambers" } } }); } private static void CreateBook(ISession session) { session.Save( new Book() { Name = "NHibernate 3.0 Cookbook", Description = "NHibernate examples", UnitPrice = 50M, Author = "Jason Dentler", ISBN = "978-1-849513-04-3" }); } }
6. 创建一个名为NameAndPrice的类,代码如下:
View Code
public class NameAndPrice { public NameAndPrice() { } public NameAndPrice(string name, decimal price) { Name = name; Price = price; } public string Name { get; set; } public decimal Price { get; set; } }
7. 创建一个名为Queries的类,代码如下:
View Code
public class Queries { private readonly ISession _session; public Queries(ISession session) { if (session == null) throw new ArgumentNullException("session"); _session = session; } }
8. 在Program.cs中, 为Program类添加下述方法:
View Code
static void RunQueries(ISession session) { } static void Show(string heading, IEnumerable<Movie> movies) { Console.WriteLine(heading); foreach (var m in movies) ShowMovie(m); Console.WriteLine(); } static void Show(string heading, Book book) { Console.WriteLine(heading); ShowBook(book); Console.WriteLine(); } static void Show(string heading, IEnumerable<Product> products) { Console.WriteLine(heading); foreach (var p in products) { if (p is Movie) { ShowMovie((Movie)p); } else if (p is Book) { ShowBook((Book)p); } else ShowProduct(p); } Console.WriteLine(); } static void Show(string heading, decimal moneyValue) { Console.WriteLine(heading); Console.WriteLine("{0:c}", moneyValue); Console.WriteLine(); } static void Show(string heading, IEnumerable<NameAndPrice> results) { Console.WriteLine(heading); foreach (var item in results) ShowNameAndPrice(item); Console.WriteLine(); } static void ShowNameAndPrice(NameAndPrice item) { Console.WriteLine("{0:c} {1}", item.Price, item.Name); } static void ShowProduct(Product p) { Console.WriteLine("{0:c} {1}", p.UnitPrice, p.Name); } static void ShowBook(Book b) { Console.WriteLine("{0:c} {1} (ISBN {2})", b.UnitPrice, b.Name, b.ISBN); } static void ShowMovie(Movie movie) { var star = movie.Actors .Select(actorRole => actorRole.Actor) .FirstOrDefault(); Console.WriteLine("{0:c} {1} starring {2}", movie.UnitPrice, movie.Name, star ?? "nobody"); }
9. 在Main方法中添加下述代码:
View Code
log4net.Config.XmlConfigurator.Configure(); var nhConfig = new Configuration().Configure(); var sessionFactory = nhConfig.BuildSessionFactory(); new ExampleDataCreator(sessionFactory) .SetUpDatabase(); using (var session = sessionFactory.OpenSession()) using (var tx = session.BeginTransaction()) { RunQueries(session); tx.Commit(); } Console.WriteLine("Press any key"); Console.ReadKey();
提示
本章中所用的这些SQL查询,嗯,实际上,整本书所用的这些SQL查询都是特定于Microsoft SQL Server 2008的,如果与你使用的RDBMS不同,那么这些示例中的SQL查询可能会略有不同.