Created
December 4, 2014 20:14
-
-
Save wayne-o/2ca7b2a56d73dd8fe679 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using FluentNHibernate.Mapping; | |
using System.Collections.Generic; | |
using NHibernate; | |
using FluentNHibernate.Cfg; | |
using FluentNHibernate.Automapping; | |
using FluentNHibernate.Data; | |
using FluentNHibernate.Cfg.Db; | |
using NHibernate.Tool.hbm2ddl; | |
namespace Model.Tests | |
{ | |
public class Employee | |
{ | |
public virtual int Id { get; protected set; } | |
public virtual string FirstName { get; set; } | |
public virtual string LastName { get; set; } | |
public virtual Store Store { get; set; } | |
} | |
public class Product | |
{ | |
public virtual int Id { get; protected set; } | |
public virtual string Name { get; set; } | |
public virtual double Price { get; set; } | |
public virtual IList<Store> StoresStockedIn { get; protected set; } | |
public Product() | |
{ | |
StoresStockedIn = new List<Store>(); | |
} | |
} | |
public class Store | |
{ | |
public virtual int Id { get; protected set; } | |
public virtual string Name { get; set; } | |
public virtual IList<Product> Products { get; set; } | |
public virtual IList<Employee> Staff { get; set; } | |
public Store() | |
{ | |
Products = new List<Product>(); | |
Staff = new List<Employee>(); | |
} | |
public virtual void AddProduct(Product product) | |
{ | |
product.StoresStockedIn.Add(this); | |
Products.Add(product); | |
} | |
public virtual void AddEmployee(Employee employee) | |
{ | |
employee.Store = this; | |
Staff.Add(employee); | |
} | |
} | |
public class EmployeeMap : ClassMap<Employee> | |
{ | |
public EmployeeMap() | |
{ | |
Id(x => x.Id); | |
Map(x => x.FirstName); | |
Map(x => x.LastName); | |
References(x => x.Store); | |
} | |
} | |
public class StoreMap : ClassMap<Store> | |
{ | |
public StoreMap() | |
{ | |
Id(x => x.Id); | |
Map(x => x.Name); | |
HasMany(x => x.Staff) | |
.Inverse() | |
.Cascade.All(); | |
HasManyToMany(x => x.Products) | |
.Cascade.All() | |
.Table("StoreProduct"); | |
} | |
} | |
public class ProductMap : ClassMap<Product> | |
{ | |
public ProductMap() | |
{ | |
Id(x => x.Id); | |
Map(x => x.Name); | |
Map(x => x.Price); | |
HasManyToMany(x => x.StoresStockedIn) | |
.Cascade.All() | |
.Inverse() | |
.Table("StoreProduct"); | |
} | |
} | |
public class Program | |
{ | |
static void Main() | |
{ | |
var sessionFactory = CreateSessionFactory(); | |
using (var session = sessionFactory.OpenSession()) | |
{ | |
using (var transaction = session.BeginTransaction()) | |
{ | |
// create a couple of Stores each with some Products and Employees | |
var barginBasin = new Store { Name = "Bargin Basin" }; | |
var superMart = new Store { Name = "SuperMart" }; | |
var potatoes = new Product { Name = "Potatoes", Price = 3.60 }; | |
var fish = new Product { Name = "Fish", Price = 4.49 }; | |
var milk = new Product { Name = "Milk", Price = 0.79 }; | |
var bread = new Product { Name = "Bread", Price = 1.29 }; | |
var cheese = new Product { Name = "Cheese", Price = 2.10 }; | |
var waffles = new Product { Name = "Waffles", Price = 2.41 }; | |
var daisy = new Employee { FirstName = "Daisy", LastName = "Harrison" }; | |
var jack = new Employee { FirstName = "Jack", LastName = "Torrance" }; | |
var sue = new Employee { FirstName = "Sue", LastName = "Walkters" }; | |
var bill = new Employee { FirstName = "Bill", LastName = "Taft" }; | |
var joan = new Employee { FirstName = "Joan", LastName = "Pope" }; | |
// add products to the stores, there's some crossover in the products in each | |
// store, because the store-product relationship is many-to-many | |
AddProductsToStore(barginBasin, potatoes, fish, milk, bread, cheese); | |
AddProductsToStore(superMart, bread, cheese, waffles); | |
// add employees to the stores, this relationship is a one-to-many, so one | |
// employee can only work at one store at a time | |
AddEmployeesToStore(barginBasin, daisy, jack, sue); | |
AddEmployeesToStore(superMart, bill, joan); | |
// save both stores, this saves everything else via cascading | |
session.SaveOrUpdate(barginBasin); | |
session.SaveOrUpdate(superMart); | |
transaction.Commit(); | |
} | |
// retreive all stores and display them | |
using (session.BeginTransaction()) | |
{ | |
var stores = session.CreateCriteria(typeof(Store)) | |
.List<Store>(); | |
foreach (var store in stores) | |
{ | |
WriteStorePretty(store); | |
} | |
} | |
Console.ReadKey(); | |
} | |
} | |
private static void WriteStorePretty(Store store) | |
{ | |
Console.WriteLine(store.Name); | |
Console.WriteLine(" Products:"); | |
foreach (var product in store.Products) | |
{ | |
Console.WriteLine(" " + product.Name); | |
} | |
Console.WriteLine(" Staff:"); | |
foreach (var employee in store.Staff) | |
{ | |
Console.WriteLine(" " + employee.FirstName + " " + employee.LastName); | |
} | |
Console.WriteLine(); | |
} | |
public static void AddProductsToStore(Store store, params Product[] products) | |
{ | |
foreach (var product in products) | |
{ | |
store.AddProduct(product); | |
} | |
} | |
public static void AddEmployeesToStore(Store store, params Employee[] employees) | |
{ | |
foreach (var employee in employees) | |
{ | |
store.AddEmployee(employee); | |
} | |
} | |
public static ISessionFactory CreateSessionFactory() | |
{ | |
var autoMap = AutoMap.AssemblyOf<Program>() | |
.Where(t => typeof(Entity).IsAssignableFrom(t)); | |
return Fluently.Configure() | |
.Database(PostgreSQLConfiguration.PostgreSQL82.ConnectionString(c => c.FromConnectionStringWithKey("ConnectionString")).AdoNetBatchSize(50)) | |
.Mappings(m => m.AutoMappings.Add(autoMap)) | |
.ExposeConfiguration(TreatConfiguration) | |
.BuildSessionFactory(); | |
} | |
protected static void TreatConfiguration(NHibernate.Cfg.Configuration configuration) | |
{ | |
configuration.SetProperty("generate_statistics", "true"); | |
var update = new SchemaUpdate(configuration); | |
update.Execute(false, true); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment